-
-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from pmndrs/test-atom-with-storage
add test for atomWithStorage
- Loading branch information
Showing
2 changed files
with
49 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,47 @@ | ||
import React, { Fragment } from 'react' | ||
import { fireEvent, render } from '@testing-library/react' | ||
import { Provider as ProviderOrig, useAtom } from '../../src/index' | ||
import { atomWithStorage } from '../../src/utils' | ||
|
||
const Provider = process.env.PROVIDER_LESS_MODE ? Fragment : ProviderOrig | ||
|
||
const storageData: Record<string, number> = { | ||
count: 10, | ||
} | ||
const dummyStorage = { | ||
getItem: (key: string) => { | ||
if (!(key in storageData)) { | ||
throw new Error('no value stored') | ||
} | ||
return storageData[key] | ||
}, | ||
setItem: (key: string, newValue: number) => { | ||
storageData[key] = newValue | ||
}, | ||
} | ||
|
||
it('simple count', async () => { | ||
const countAtom = atomWithStorage('count', 1, dummyStorage) | ||
|
||
const Counter: React.FC = () => { | ||
const [count, setCount] = useAtom(countAtom) | ||
return ( | ||
<> | ||
<div>count: {count}</div> | ||
<button onClick={() => setCount((c) => c + 1)}>button</button> | ||
</> | ||
) | ||
} | ||
|
||
const { findByText, getByText } = render( | ||
<Provider> | ||
<Counter /> | ||
</Provider> | ||
) | ||
|
||
await findByText('count: 10') | ||
|
||
fireEvent.click(getByText('button')) | ||
await findByText('count: 11') | ||
expect(storageData.count).toBe(11) | ||
}) |