-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
140 additions
and
12 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,56 @@ | ||
import { atom } from 'jotai' | ||
import { render, waitFor } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import { useEffect, useRef } from 'react' | ||
|
||
import { useAtomValueWithSchedule, useSetAtomWithSchedule } from '../src' | ||
|
||
const useCommitCount = () => { | ||
const commitCountRef = useRef(1) | ||
useEffect(() => { | ||
commitCountRef.current += 1 | ||
}) | ||
return commitCountRef.current | ||
} | ||
|
||
describe('the same behavior as jotai if the `priority` field is not passed', () => { | ||
it('trigger state updates correctly', async () => { | ||
const anAtom = atom(0) | ||
function Control() { | ||
const dispatch = useSetAtomWithSchedule(anAtom) | ||
return ( | ||
<> | ||
<button type="button" onClick={() => dispatch((num) => num + 1)}> | ||
button | ||
</button> | ||
<div>Control commits: {useCommitCount()}</div> | ||
</> | ||
) | ||
} | ||
function Display() { | ||
const num = useAtomValueWithSchedule(anAtom) | ||
return ( | ||
<> | ||
<div>number: {num}</div> | ||
<div>Display commits: {useCommitCount()}</div> | ||
</> | ||
) | ||
} | ||
function App() { | ||
return ( | ||
<> | ||
<Display /> | ||
<Control /> | ||
</> | ||
) | ||
} | ||
const { findByText, getByText } = render(<App />) | ||
await findByText('number: 0') | ||
await userEvent.click(getByText('button')) | ||
await waitFor(() => { | ||
findByText('number: 1') | ||
findByText('Display commits: 1') | ||
findByText('Control commits: 2') | ||
}) | ||
}) | ||
}) |
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,67 @@ | ||
import { atom } from 'jotai' | ||
import { render, waitFor } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
|
||
import { | ||
IMMEDIATE_PRIORITY, | ||
LOW_PRIORITY, | ||
NORMAL_PRIORITY, | ||
useAtomValueWithSchedule, | ||
useSetAtomWithSchedule, | ||
} from '../src' | ||
|
||
it('batching rendering if we pass different `priority`', async () => { | ||
const anAtom = atom(0) | ||
function ImmediatePriorityCpn() { | ||
const num = useAtomValueWithSchedule(anAtom, { | ||
priority: IMMEDIATE_PRIORITY, | ||
}) | ||
return <div>number of ImmediatePriorityCpn: {num}</div> | ||
} | ||
function NormalPriorityCpn() { | ||
const num = useAtomValueWithSchedule(anAtom, { | ||
priority: NORMAL_PRIORITY, | ||
}) | ||
return <div>number of NormalPriorityCpn: {num}</div> | ||
} | ||
function LowPriorityCpn() { | ||
const num = useAtomValueWithSchedule(anAtom, { | ||
priority: LOW_PRIORITY, | ||
}) | ||
return <div>number of LowPriorityCpn: {num}</div> | ||
} | ||
function App() { | ||
const dispatch = useSetAtomWithSchedule(anAtom) | ||
return ( | ||
<> | ||
<ImmediatePriorityCpn /> | ||
<NormalPriorityCpn /> | ||
<LowPriorityCpn /> | ||
<button type="button" onClick={() => dispatch((num) => num + 1)}> | ||
button | ||
</button> | ||
</> | ||
) | ||
} | ||
const { findByText, getByText } = render(<App />) | ||
|
||
await userEvent.click(getByText('button')) | ||
|
||
await waitFor(() => { | ||
findByText('number of ImmediatePriorityCpn: 1') | ||
findByText('number of NormalPriorityCpn: 0') | ||
findByText('number of LowPriorityCpn: 0') | ||
}) | ||
|
||
await waitFor(() => { | ||
findByText('number of ImmediatePriorityCpn: 1') | ||
findByText('number of NormalPriorityCpn: 1') | ||
findByText('number of LowPriorityCpn: 0') | ||
}) | ||
|
||
await waitFor(() => { | ||
findByText('number of ImmediatePriorityCpn: 1') | ||
findByText('number of NormalPriorityCpn: 1') | ||
findByText('number of LowPriorityCpn: 1') | ||
}) | ||
}) |
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
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