Skip to content

Commit

Permalink
add another failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jan 15, 2025
1 parent 4615df7 commit 031cfaa
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/react/vanilla-utils/atomWithStorage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -745,3 +745,41 @@ describe('with subscribe method in string storage', () => {
// expect(storageData.count).toBe('11')
})
})

describe('with custom async storage', () => {
it('does not infinite loop (#2931)', async () => {
let storedValue = 0
const counterAtom = atomWithStorage('counter', 0, {
async getItem(_key: string, _initialValue: number) {
return await Promise.resolve(storedValue)
},
async setItem(_key, newValue) {
storedValue = await new Promise((resolve) => resolve(newValue))
},
async removeItem() {},
})
const Component = () => {
const [count, setCount] = useAtom(counterAtom)
return (
<>
<div>count: {count}</div>
<button onClick={() => setCount(async (c) => (await c) + 1)}>
button
</button>
</>
)
}
await act(async () => {
render(
<StrictMode>
<Suspense fallback="loading">
<Component />
</Suspense>
</StrictMode>,
)
})
await screen.findByText('count: 0')
await userEvent.click(screen.getByText('button'))
await screen.findByText('count: 1')
})
})

0 comments on commit 031cfaa

Please sign in to comment.