From 3533652da0976406557d72cb68991fc857bd4bc9 Mon Sep 17 00:00:00 2001 From: David Maskasky Date: Thu, 5 Oct 2023 09:33:07 -0700 Subject: [PATCH] add test for supporting synchronous updates --- __tests__/mutableAtom.test.tsx | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/__tests__/mutableAtom.test.tsx b/__tests__/mutableAtom.test.tsx index d53c1c0..171538e 100644 --- a/__tests__/mutableAtom.test.tsx +++ b/__tests__/mutableAtom.test.tsx @@ -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)