Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for atomWithEffectsFromRecoilValue #36

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions test/Recoil__test.res
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,56 @@ testWithReact("Recoil.atomWithEffects can run effects", container => {
isTrue(value->Option.isSome)
})

module UseRecoilAtomWithEffectFromRecoilValueConfig = {
// The callback value is later set to a React.useState setter.
let callback = ref(None)

let atom = Recoil.atom({key: "Atom", default: 0})
let atomWithEffect = Recoil.atomWithEffectsFromRecoilValue({
key: "AtomWithEffectFromRecoilValue",
default: atom,
effects_UNSTABLE: [
({onSet}) => {
onSet((~newValue, ~oldValue as _, ~isReset as _) => {
callback.contents->Belt.Option.map(cb => cb(newValue))->ignore
})
None
},
],
})

@react.component
let make = () => {
let (atomWithEffectValue, setAtomWithEffectValue) = React.useState(_ => 0)
let setAtomWithEffect = Recoil.useSetRecoilState(atomWithEffect)

React.useEffect1(() => {
callback.contents = Some(newValue => setAtomWithEffectValue(_ => newValue))
None
}, [setAtomWithEffectValue])

React.useEffect1(() => {
setAtomWithEffect(_ => 1)
None
}, [setAtomWithEffect])

<strong> {(atomWithEffectValue == 1 ? "OK" : "NOK")->React.string} </strong>
}
}

testWithReact("Recoil.atomWithEffectsFromRecoilValueConfig", container => {
act(() =>
ReactDOM.render(
<Recoil.RecoilRoot> <UseRecoilAtomWithEffectFromRecoilValueConfig /> </Recoil.RecoilRoot>,
container,
)
)

let value = container->DOM.findBySelectorAndTextContent("strong", "OK")

isTrue(value->Option.isSome)
})

@module("recoil") @new
external makeDefaultValue: unit => 'a = "DefaultValue"

Expand Down