-
-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): improve re-render behavior with useTransition (#1139)
* add test * Removing flushPending from suspensePromise * conditional transition test Co-authored-by: riemon <99447053+riemonyamada@users.noreply.github.com>
- Loading branch information
1 parent
17de495
commit c0e1b09
Showing
2 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Suspense, useEffect, useTransition } from 'react' | ||
import { fireEvent, render } from '@testing-library/react' | ||
import { atom, useAtomValue, useSetAtom } from 'jotai' | ||
import { getTestProvider } from './testUtils' | ||
|
||
const Provider = getTestProvider() | ||
|
||
jest.mock('../src/core/useDebugState.ts') | ||
|
||
const describeWithUseTransition = | ||
typeof useTransition === 'function' ? describe : describe.skip | ||
|
||
describeWithUseTransition('useTransition', () => { | ||
it('no extra commit with useTransition (#1125)', async () => { | ||
const countAtom = atom(0) | ||
const delayedAtom = atom(async (get) => { | ||
await new Promise((r) => setTimeout(r, 100)) | ||
return get(countAtom) | ||
}) | ||
|
||
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 { getByText, findByText } = render( | ||
<Provider> | ||
<Suspense fallback="loading"> | ||
<Counter /> | ||
</Suspense> | ||
</Provider> | ||
) | ||
|
||
await findByText('delayed: 0') | ||
|
||
fireEvent.click(getByText('button')) | ||
await findByText('delayed: 1') | ||
|
||
expect(commited).toEqual([ | ||
{ pending: false, delayed: 0 }, | ||
{ pending: true, delayed: 0 }, | ||
{ pending: false, delayed: 1 }, | ||
]) | ||
}) | ||
}) |
c0e1b09
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
jotai – ./
jotai.org
jotai-pmndrs.vercel.app
jotai-website.vercel.app
jotai.vercel.app
jotai-git-main-pmndrs.vercel.app
jotai.pmnd.rs
www.jotai.org