Skip to content

Commit

Permalink
add test for supporting synchronous updates
Browse files Browse the repository at this point in the history
  • Loading branch information
David Maskasky committed Oct 5, 2023
1 parent 1051003 commit 3533652
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions __tests__/mutableAtom.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,32 @@ it('should correctly handle updates via writable atom', async () => {
})
})

it('should perform synchronous update', async () => {
expect.assertions(2)
const mutableCountAtom = mutableAtom(0)
const countIsNotZeroAtom = atom((get) => get(mutableCountAtom).value > 0)
const incrementCountAtom = atom(null, (get) => {
const countProxy = get(mutableCountAtom)
expect(get(countIsNotZeroAtom)).toBe(false)
countProxy.value++
expect(get(countIsNotZeroAtom)).toBe(true)
})
let isMounted = false
incrementCountAtom.onMount = () => {
isMounted = true
}
function useTest() {
useAtom(countIsNotZeroAtom)
const [, incrementCount] = useAtom(incrementCountAtom)
return { incrementCount }
}
const { result } = renderHook(useTest)
await waitFor(() => assert(isMounted))
await act(async () => {
result.current.incrementCount()
})
})

// TODO: fix this infinite loop
it.skip('should not infinite loop when updating the proxy value in the read function', async () => {
expect.assertions(2)
Expand Down

0 comments on commit 3533652

Please sign in to comment.