Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(test): Implement skipIf for conditional test skipping #2161

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
97 changes: 48 additions & 49 deletions tests/react/transition.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,64 @@ 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', () => {
// FIXME some tests are failing with react@experimental
it.skipIf(typeof use === 'function')(
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
'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