Skip to content

Commit

Permalink
move unit test to store.test
Browse files Browse the repository at this point in the history
  • Loading branch information
David Maskasky committed Oct 22, 2023
1 parent 6e3c5b8 commit 29ad942
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 78 deletions.
87 changes: 10 additions & 77 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 { expect, it } from 'vitest'
import { assert, expect, it } from 'vitest'
import { Provider, useAtom } from 'jotai/react'
import { atom, createStore } from 'jotai/vanilla'
import type { Atom, SetStateAction, Setter } from 'jotai/vanilla'
import type { Atom, SetStateAction } from 'jotai/vanilla'

const useCommitCount = () => {
const commitCountRef = useRef(1)
Expand Down Expand Up @@ -1145,10 +1145,10 @@ it('multiple derived atoms with dependency chaining and async write (#813)', asy
})

it('[render] resolves dependencies reliably after a delay', async () => {
expect.assertions(2)
expect.assertions(1)
const countAtom = atom(0)
const resultAtom = atom<number | null>(null)

let result: number | null = null
const resolve: (() => void)[] = []
const asyncAtom = atom(async (get) => {
const count = get(countAtom)
Expand All @@ -1167,9 +1167,10 @@ it('[render] resolves dependencies reliably after a delay', async () => {
console.log(`derived (${count})`)
const resultCount = await get(asyncAtom)
console.log(`derived (${count})`, resultCount)
setSelf(resultAtom, resultCount)
result = resultCount
if (resultCount === 2) setSelf() // <-- necessary
},
(_get, set, ...args: Parameters<Setter>) => set(...args)
() => {}
)

const derivedSyncAtom = atom((get) => {
Expand All @@ -1192,7 +1193,7 @@ it('[render] resolves dependencies reliably after a delay', async () => {
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]!()
Expand All @@ -1203,8 +1204,7 @@ it('[render] resolves dependencies reliably after a delay', async () => {
resolve[1]!()
resolve[2]!()

await waitFor(() => assert(store.get(countAtom) === 2))
expect(store.get(resultAtom)).toBe(2)
await waitFor(() => assert(result === 2))

await act(() => setCount(increment))
await act(() => setCount(increment))
Expand All @@ -1213,72 +1213,5 @@ it('[render] resolves dependencies reliably after a delay', async () => {
resolve[4]!()

await waitFor(() => assert(store.get(countAtom) === 4))
expect(store.get(resultAtom)).toBe(4) // 3
expect(result).toBe(4) // 3
})

it('[unit] resolves dependencies reliably after a delay', async () => {
const countAtom = atom(0)
const resultAtom = atom<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 asyncCount = await get(asyncAtom)
console.log(`derived (${count})`, asyncCount)
setSelf(resultAtom, asyncCount)
},
(_get, set, ...args: Parameters<Setter>) => set(...args)
)

const store = createStore()
store.sub(derivedAtom, () => {})

await waitFor(() => assert(resolve.length === 1))

resolve[0]!()

store.set(countAtom, increment)
store.set(countAtom, increment)

await waitFor(() => assert(resolve.length === 3))

resolve[1]!()
resolve[2]!()

await waitFor(() => assert(store.get(countAtom) === 2))
expect(store.get(resultAtom)).toBe(2)

store.set(countAtom, increment)
store.set(countAtom, increment)

await waitFor(() => assert(resolve.length === 5))

resolve[3]!()
resolve[4]!()

await waitFor(() => assert(store.get(countAtom) === 4))
expect(store.get(resultAtom)).toBe(4) // 3
})

function assert(condition: boolean): asserts condition {
if (!condition) {
throw new Error('assertion failed')
}
}

function increment(c: number) {
return c + 1
}
61 changes: 60 additions & 1 deletion tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from 'vitest'
import { waitFor } from '@testing-library/dom'
import { assert, describe, expect, it, vi } from 'vitest'
import { atom, createStore } from 'jotai/vanilla'
import type { Getter } from 'jotai/vanilla'

Expand Down Expand Up @@ -423,3 +424,61 @@ it('should update derived atoms during write (#2107)', async () => {
store.set(countAtom, 2)
expect(store.get(countAtom)).toBe(2)
})

it.only('[unit] 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})`)
result = await get(asyncAtom)
console.log(`derived (${count})`, result)
if (result === 2) setSelf() // <-- necessary
},
() => {}
)

const store = createStore()
store.sub(derivedAtom, () => {})

await waitFor(() => assert(resolve.length === 1))

resolve[0]!()
const increment = (c: number) => c + 1
store.set(countAtom, increment)
store.set(countAtom, increment)

await waitFor(() => assert(resolve.length === 3))

resolve[1]!()
resolve[2]!()
await waitFor(() => assert(result === 2))

store.set(countAtom, increment)
store.set(countAtom, increment)

await waitFor(() => assert(resolve.length === 5))

resolve[3]!()
resolve[4]!()

await new Promise(setImmediate)
await waitFor(() => assert(store.get(countAtom) === 4))

expect(result).toBe(4) // 3
})

0 comments on commit 29ad942

Please sign in to comment.