From 9e10342bf0d0528a368cf39a7e5d9b8a628f2ec7 Mon Sep 17 00:00:00 2001 From: David Maskasky Date: Sun, 22 Oct 2023 18:29:47 -0700 Subject: [PATCH] move test to async2 and cleanup tests --- tests/react/async.test.tsx | 80 ++---------------------------------- tests/react/async2.test.tsx | 77 ++++++++++++++++++++++++++++++++-- tests/vanilla/store.test.tsx | 7 +--- 3 files changed, 79 insertions(+), 85 deletions(-) diff --git a/tests/react/async.test.tsx b/tests/react/async.test.tsx index b3a64e4cb1..fd3c4f7daf 100644 --- a/tests/react/async.test.tsx +++ b/tests/react/async.test.tsx @@ -1,10 +1,10 @@ import { StrictMode, Suspense, useEffect, useRef } from 'react' import { act, fireEvent, render, waitFor } from '@testing-library/react' import userEvent from '@testing-library/user-event' -import { assert, expect, it } from 'vitest' -import { Provider, useAtom } from 'jotai/react' -import { atom, createStore } from 'jotai/vanilla' -import type { Atom, SetStateAction } from 'jotai/vanilla' +import { expect, it } from 'vitest' +import { useAtom } from 'jotai/react' +import { atom } from 'jotai/vanilla' +import type { Atom } from 'jotai/vanilla' const useCommitCount = () => { const commitCountRef = useRef(1) @@ -1143,75 +1143,3 @@ it('multiple derived atoms with dependency chaining and async write (#813)', asy getByText('bName: beta') }) }) - -it('[render] resolves dependencies reliably after a delay', async () => { - expect.assertions(1) - const countAtom = atom(0) - - let result: number | null = null - const resolve: (() => void)[] = [] - const asyncAtom = atom(async (get) => { - const count = get(countAtom) - await new Promise((r: { (): void; count?: number }) => { - r.count = count - resolve.push(r) - }) - console.log(`resolved (${count})`) - return count - }) - - const derivedAtom = atom( - async (get, { setSelf }) => { - const count = get(countAtom) - await Promise.resolve() - console.log(`derived (${count})`) - const resultCount = await get(asyncAtom) - console.log(`derived (${count})`, resultCount) - result = resultCount - if (resultCount === 2) setSelf() // <-- necessary - }, - () => {} - ) - - const derivedSyncAtom = atom((get) => { - get(derivedAtom) - }) - - function useTest() { - useAtom(derivedSyncAtom) - useAtom(countAtom) - } - function TestComponent() { - useTest() - return null - } - const store = createStore() - - const Wrapper = ({ children }: React.PropsWithChildren) => ( - {children} - ) - render(, { wrapper: Wrapper }) - - const setCount = (arg: SetStateAction) => store.set(countAtom, arg) - const increment = (c: number) => c + 1 - await waitFor(() => assert(resolve.length === 1)) - - resolve[0]!() - - await act(() => setCount(increment)) - await act(() => setCount(increment)) - - resolve[1]!() - resolve[2]!() - - await waitFor(() => assert(result === 2)) - - await act(() => setCount(increment)) - await act(() => setCount(increment)) - - resolve[3]!() - resolve[4]!() - - await waitFor(() => assert(store.get(countAtom) === 4)) - expect(result).toBe(4) // 3 -}) diff --git a/tests/react/async2.test.tsx b/tests/react/async2.test.tsx index 44cc2fefa4..f6fc2c9d89 100644 --- a/tests/react/async2.test.tsx +++ b/tests/react/async2.test.tsx @@ -1,8 +1,8 @@ import { StrictMode, Suspense } from 'react' -import { fireEvent, render } from '@testing-library/react' -import { describe, it } from 'vitest' -import { useAtomValue, useSetAtom } from 'jotai/react' -import { atom } from 'jotai/vanilla' +import { act, fireEvent, render, waitFor } from '@testing-library/react' +import { assert, describe, expect, it } from 'vitest' +import { Provider, useAtom, useAtomValue, useSetAtom } from 'jotai/react' +import { SetStateAction, atom, createStore } from 'jotai/vanilla' describe('useAtom delay option test', () => { it('suspend for Promise.resovle without delay option', async () => { @@ -141,3 +141,72 @@ describe('atom read function setSelf option test', () => { await findByText('text: hello1') }) }) + +it('resolves dependencies reliably after a delay', async () => { + expect.assertions(1) + const countAtom = atom(0) + + let result: number | null = null + const resolve: (() => void)[] = [] + const asyncAtom = atom(async (get) => { + const count = get(countAtom) + await new Promise((r: { (): void; count?: number }) => { + r.count = count + resolve.push(r) + }) + return count + }) + + const derivedAtom = atom( + async (get, { setSelf }) => { + get(countAtom) + await Promise.resolve() + const resultCount = await get(asyncAtom) + result = resultCount + if (resultCount === 2) setSelf() // <-- necessary + }, + () => {} + ) + + const derivedSyncAtom = atom((get) => { + get(derivedAtom) + }) + + function useTest() { + useAtom(derivedSyncAtom) + useAtom(countAtom) + } + function TestComponent() { + useTest() + return null + } + const store = createStore() + + const Wrapper = ({ children }: React.PropsWithChildren) => ( + {children} + ) + render(, { wrapper: Wrapper }) + + const setCount = (arg: SetStateAction) => store.set(countAtom, arg) + const increment = (c: number) => c + 1 + await waitFor(() => assert(resolve.length === 1)) + + resolve[0]!() + + await act(() => setCount(increment)) + await act(() => setCount(increment)) + + resolve[1]!() + resolve[2]!() + + await waitFor(() => assert(result === 2)) + + await act(() => setCount(increment)) + await act(() => setCount(increment)) + + resolve[3]!() + resolve[4]!() + + await waitFor(() => assert(store.get(countAtom) === 4)) + expect(result).toBe(4) // 3 +}) diff --git a/tests/vanilla/store.test.tsx b/tests/vanilla/store.test.tsx index 2411b31822..cb5fc06e8d 100644 --- a/tests/vanilla/store.test.tsx +++ b/tests/vanilla/store.test.tsx @@ -425,7 +425,7 @@ it('should update derived atoms during write (#2107)', async () => { expect(store.get(countAtom)).toBe(2) }) -it.only('[unit] resolves dependencies reliably after a delay', async () => { +it.only('resolves dependencies reliably after a delay', async () => { expect.assertions(1) const countAtom = atom(0) let result: number | null = null @@ -437,17 +437,14 @@ it.only('[unit] resolves dependencies reliably after a delay', async () => { r.count = count resolve.push(r) }) - console.log(`resolved (${count})`) return count }) const derivedAtom = atom( async (get, { setSelf }) => { - const count = get(countAtom) + get(countAtom) await Promise.resolve() - console.log(`derived (${count})`) result = await get(asyncAtom) - console.log(`derived (${count})`, result) if (result === 2) setSelf() // <-- necessary }, () => {}