-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: loading mutation does not call refresh on unmount (#38)
* failing test * fix * fix * lint * remove extra line * as unknown as never * Update src/atomsWithMutation.ts --------- Co-authored-by: Daishi Kato <[email protected]>
- Loading branch information
1 parent
f640dbf
commit 8beec78
Showing
2 changed files
with
64 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { useState } from 'react' | ||
import { fireEvent, render } from '@testing-library/react' | ||
import { useAtom } from 'jotai/react' | ||
import { atomsWithMutation } from '../src/index' | ||
|
||
it('atomsWithMutation should be refreshed on unmount (#2060)', async () => { | ||
let resolve: (() => void) | undefined | ||
const [, testAtom] = atomsWithMutation<number, number, number, number>( | ||
() => ({ | ||
mutationKey: ['test-atom'], | ||
mutationFn: async (a) => { | ||
await new Promise<void>((r) => { | ||
resolve = r | ||
}) | ||
return a | ||
}, | ||
}) | ||
) | ||
|
||
function App() { | ||
const [mount, setMount] = useState<boolean>(true) | ||
return ( | ||
<div> | ||
<button onClick={() => setMount(false)}>unmount</button> | ||
<button onClick={() => setMount(true)}>mount</button> | ||
{mount && <TestView />} | ||
</div> | ||
) | ||
} | ||
|
||
function TestView() { | ||
const [state, mutate] = useAtom(testAtom) | ||
return ( | ||
<div> | ||
<p>status: {state.status}</p> | ||
<button disabled={state.isLoading} onClick={() => mutate([1])}> | ||
mutate | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
const { findByText, getByText } = render(<App />) | ||
|
||
await findByText('status: idle') | ||
|
||
fireEvent.click(getByText('mutate')) | ||
await findByText('status: loading') | ||
resolve?.() | ||
await findByText('status: success') | ||
|
||
fireEvent.click(getByText('mutate')) | ||
await findByText('status: loading') | ||
fireEvent.click(getByText('unmount')) | ||
fireEvent.click(getByText('mount')) | ||
await findByText('status: idle') | ||
resolve?.() | ||
}) |
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