This repository has been archived by the owner on Dec 31, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0a3661
commit ce77e7e
Showing
1 changed file
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import React, { useState, useEffect } from "react" | ||
import { observer, Observer, useLocalStore, useAsObservableSource } from "../src" | ||
import { sleepHelper } from "." | ||
import renderer, { act } from "react-test-renderer" | ||
|
||
test("computed properties react to props when using hooks", async () => { | ||
const seen = [] | ||
|
||
const Child = ({ x }) => { | ||
const props = useAsObservableSource({ x }) | ||
const store = useLocalStore(() => ({ | ||
get getPropX() { | ||
return props.x | ||
} | ||
})) | ||
|
||
return <Observer>{() => (seen.push(store.getPropX), <div>{store.getPropX}</div>)}</Observer> | ||
} | ||
|
||
const Parent = () => { | ||
const [state, setState] = useState({ x: 0 }) | ||
seen.push("parent") | ||
useEffect(() => { | ||
setTimeout(() => { | ||
act(() => { | ||
setState({ x: 2 }) | ||
}) | ||
}, 100) | ||
}, []) | ||
return <Child x={state.x} /> | ||
} | ||
|
||
let wrapper | ||
act(() => { | ||
wrapper = renderer.create(<Parent />) | ||
}) | ||
expect(wrapper.toJSON()).toMatchInlineSnapshot(` | ||
<div> | ||
0 | ||
</div> | ||
`) | ||
|
||
await sleepHelper(400) | ||
expect(seen).toEqual(["parent", 0, "parent", 2]) | ||
expect(wrapper.toJSON()).toMatchInlineSnapshot(` | ||
<div> | ||
2 | ||
</div> | ||
`) | ||
}) | ||
|
||
test("computed properties result in double render when using observer instead of Observer", async () => { | ||
const seen = [] | ||
|
||
const Child = observer(({ x }) => { | ||
const props = useAsObservableSource({ x }) | ||
const store = useLocalStore(() => ({ | ||
get getPropX() { | ||
return props.x | ||
} | ||
})) | ||
|
||
seen.push(store.getPropX) | ||
return <div>{store.getPropX}</div> | ||
}) | ||
|
||
const Parent = () => { | ||
const [state, setState] = useState({ x: 0 }) | ||
seen.push("parent") | ||
useEffect(() => { | ||
setTimeout(() => { | ||
act(() => { | ||
setState({ x: 2 }) | ||
}) | ||
}, 100) | ||
}, []) | ||
return <Child x={state.x} /> | ||
} | ||
|
||
let wrapper | ||
act(() => { | ||
wrapper = renderer.create(<Parent />) | ||
}) | ||
expect(wrapper.toJSON()).toMatchInlineSnapshot(` | ||
<div> | ||
0 | ||
</div> | ||
`) | ||
|
||
await sleepHelper(400) | ||
expect(seen).toEqual([ | ||
"parent", | ||
0, | ||
"parent", | ||
2, | ||
2 // should contain "2" only once! But with hooks, one update is scheduled based the fact that props change, the other because the observable source changed. | ||
]) | ||
expect(wrapper.toJSON()).toMatchInlineSnapshot(` | ||
<div> | ||
2 | ||
</div> | ||
`) | ||
}) |