Skip to content

Commit

Permalink
add failing test for #2682
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Aug 7, 2024
1 parent 9228a88 commit 6be02b0
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions tests/react/async2.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,103 @@ describe('infinite pending', () => {
await findByText('count: 3')
})
})

describe('write to async atom twice', async () => {
it('no wait', async () => {
const asyncAtom = atom(Promise.resolve(2))
const writer = atom(null, async (get, set) => {
set(asyncAtom, async (c) => (await c) + 1)
set(asyncAtom, async (c) => (await c) + 1)
return get(asyncAtom)
})

const Component = () => {
const count = useAtomValue(asyncAtom)
const write = useSetAtom(writer)
return (
<>
<div>count: {count}</div>
<button onClick={write}>button</button>
</>
)
}

const { findByText, getByText } = render(
<StrictMode>
<Suspense fallback="loading">
<Component />
</Suspense>
</StrictMode>,
)

await findByText('count: 2')
await userEvent.click(getByText('button'))
await findByText('count: 4')
})

it('wait Promise.resolve()', async () => {
const asyncAtom = atom(Promise.resolve(2))
const writer = atom(null, async (get, set) => {
set(asyncAtom, async (c) => (await c) + 1)
await Promise.resolve()
set(asyncAtom, async (c) => (await c) + 1)
return get(asyncAtom)
})

const Component = () => {
const count = useAtomValue(asyncAtom)
const write = useSetAtom(writer)
return (
<>
<div>count: {count}</div>
<button onClick={write}>button</button>
</>
)
}

const { findByText, getByText } = render(
<StrictMode>
<Suspense fallback="loading">
<Component />
</Suspense>
</StrictMode>,
)

await findByText('count: 2')
await userEvent.click(getByText('button'))
await findByText('count: 4')
})

it('wait setTimeout()', async () => {
const asyncAtom = atom(Promise.resolve(2))
const writer = atom(null, async (get, set) => {
set(asyncAtom, async (c) => (await c) + 1)
await new Promise((r) => setTimeout(r))
set(asyncAtom, async (c) => (await c) + 1)
return get(asyncAtom)
})

const Component = () => {
const count = useAtomValue(asyncAtom)
const write = useSetAtom(writer)
return (
<>
<div>count: {count}</div>
<button onClick={write}>button</button>
</>
)
}

const { findByText, getByText } = render(
<StrictMode>
<Suspense fallback="loading">
<Component />
</Suspense>
</StrictMode>,
)

await findByText('count: 2')
await userEvent.click(getByText('button'))
await findByText('count: 4')
})
})

0 comments on commit 6be02b0

Please sign in to comment.