Skip to content

Commit

Permalink
refactor(test): Implement skipIf for conditional test skipping
Browse files Browse the repository at this point in the history
Use skipIf to conditionally skip tests based on the availability of useTransition and use features, enhancing test suite maintainability and readability.
  • Loading branch information
NaamuKim committed Oct 2, 2023
1 parent d6afb77 commit 0ef8369
Showing 1 changed file with 47 additions and 49 deletions.
96 changes: 47 additions & 49 deletions tests/react/transition.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,63 @@ import { atom } from 'jotai/vanilla'

const { use, useTransition } = ReactExports

const describeWithUseTransition =
typeof useTransition === 'function' ? describe : describe.skip

// FIXME some tests are failing with react@experimental
const itWithoutUse = typeof use === 'function' ? it.skip : it

describeWithUseTransition('useTransition', () => {
itWithoutUse('no extra commit with useTransition (#1125)', async () => {
const countAtom = atom(0)
let resolve = () => {}
const delayedAtom = atom(async (get) => {
await new Promise<void>((r) => (resolve = r))
return get(countAtom)
})
describe.skipIf(typeof useTransition !== 'function')('useTransition', () => {
it.skipIf(typeof use === 'function')(
'no extra commit with useTransition (#1125)',
async () => {
const countAtom = atom(0)
let resolve = () => {}
const delayedAtom = atom(async (get) => {
await new Promise<void>((r) => (resolve = r))
return get(countAtom)
})

const commited: { pending: boolean; delayed: number }[] = []
const commited: { pending: boolean; delayed: number }[] = []

const Counter = () => {
const setCount = useSetAtom(countAtom)
const delayed = useAtomValue(delayedAtom)
const [pending, startTransition] = useTransition()
useEffect(() => {
commited.push({ pending, delayed })
})
return (
<>
<div>delayed: {delayed}</div>
<button
onClick={() => startTransition(() => setCount((c) => c + 1))}>
button
</button>
</>
)
}

const Counter = () => {
const setCount = useSetAtom(countAtom)
const delayed = useAtomValue(delayedAtom)
const [pending, startTransition] = useTransition()
useEffect(() => {
commited.push({ pending, delayed })
})
return (
const { getByText } = render(
<>
<div>delayed: {delayed}</div>
<button onClick={() => startTransition(() => setCount((c) => c + 1))}>
button
</button>
<Suspense fallback="loading">
<Counter />
</Suspense>
</>
)
}

const { getByText } = render(
<>
<Suspense fallback="loading">
<Counter />
</Suspense>
</>
)

resolve()
await waitFor(() => expect(getByText('delayed: 0')).toBeTruthy())
resolve()
await waitFor(() => expect(getByText('delayed: 0')).toBeTruthy())

await userEvent.click(getByText('button'))
await userEvent.click(getByText('button'))

act(() => {
resolve()
})
act(() => {
resolve()
})

await waitFor(() => expect(getByText('delayed: 1')).toBeTruthy())
await waitFor(() => expect(getByText('delayed: 1')).toBeTruthy())

expect(commited).toEqual([
{ pending: false, delayed: 0 },
{ pending: true, delayed: 0 },
{ pending: false, delayed: 1 },
])
})
expect(commited).toEqual([
{ pending: false, delayed: 0 },
{ pending: true, delayed: 0 },
{ pending: false, delayed: 1 },
])
}
)

it('can update normal atom with useTransition (#1151)', async () => {
const countAtom = atom(0)
Expand Down

0 comments on commit 0ef8369

Please sign in to comment.