Skip to content

Commit

Permalink
move test to async2 and cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
David Maskasky committed Oct 23, 2023
1 parent 29ad942 commit 9e10342
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 85 deletions.
80 changes: 4 additions & 76 deletions tests/react/async.test.tsx
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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<void>((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) => (
<Provider store={store}>{children}</Provider>
)
render(<TestComponent />, { wrapper: Wrapper })

const setCount = (arg: SetStateAction<number>) => 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
})
77 changes: 73 additions & 4 deletions tests/react/async2.test.tsx
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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<void>((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) => (
<Provider store={store}>{children}</Provider>
)
render(<TestComponent />, { wrapper: Wrapper })

const setCount = (arg: SetStateAction<number>) => 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
})
7 changes: 2 additions & 5 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
},
() => {}
Expand Down

0 comments on commit 9e10342

Please sign in to comment.