Skip to content

Commit

Permalink
test: it should not recompute derived atom when the async getter valu…
Browse files Browse the repository at this point in the history
…e is the same
  • Loading branch information
dmaskasky committed Mar 3, 2024
1 parent 3ca1d43 commit ebe8d57
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,24 @@ it('keeps atoms mounted between recalculations', async () => {
unmounted: 0,
})
})

it('should not recompute derived atom when the async getter value is the same', async () => {
const baseAtom = atom({ id: 0, value: 0 })
const derivedAsyncAtom = atom(async (get) => get(baseAtom).id)

const store = createStore()
async function updateValue() {
const { id, value } = await store.get(baseAtom)
store.set(baseAtom, { id, value: value + 1 })
}

let recomputeCount = 0
store.sub(derivedAsyncAtom, () => {
recomputeCount++
})
expect(recomputeCount).toBe(0)
await updateValue()
expect(recomputeCount).toBe(1)
await updateValue()
expect(recomputeCount).toBe(1)
})

0 comments on commit ebe8d57

Please sign in to comment.