Skip to content

Commit

Permalink
add failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Sep 19, 2024
1 parent b415826 commit d0a67ac
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, it } from 'vitest'
import { expect, it, vi } from 'vitest'
import { atom, createStore } from 'jotai/vanilla'

it('can propagate updates with async atom chains', async () => {
Expand Down Expand Up @@ -288,3 +288,35 @@ it('refreshes deps for each async read', async () => {
resolve.splice(0).forEach((fn) => fn())
expect(values).toEqual([0, 1])
})

it('should not re-evaluate stable derived atom values in situations where dependencies are re-ordered (#2738)', () => {
const callCounter = vi.fn()
const rootAtom = atom(false)
const stableDep = atom((get) => {
get(rootAtom)
return 1
})
const stableDepDep = atom((get) => {
get(stableDep)
callCounter()
return 2
})

const newAtom = atom((get) => {
if (get(rootAtom)) {
return get(stableDepDep)
}

return get(stableDep)
})

const store = createStore()
store.sub(stableDepDep, () => {})
store.sub(newAtom, () => {})
expect(store.get(stableDepDep)).toBe(2)
expect(callCounter).toHaveBeenCalledTimes(1)

store.set(rootAtom, true)
expect(store.get(newAtom)).toBe(2)
expect(callCounter).toHaveBeenCalledTimes(1)
})

0 comments on commit d0a67ac

Please sign in to comment.