Skip to content

Commit

Permalink
add more observe tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Jan 25, 2025
1 parent 0eff05b commit 8e08f14
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/observe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,49 @@ describe('observe', () => {

unsubscribe2()
})

it('should allow reobserving', async () => {
const store = createStore()
const countAtom = atom(0)
let runCount = 0
function effect(get: Getter) {
++runCount
get(countAtom)
}
const unobserve = observe(effect, store)
await delay(0)
expect(runCount).toBe(1)
store.set(countAtom, increment)
await delay(0)
expect(runCount).toBe(2)
const reobserve = unobserve()
store.set(countAtom, increment)
await delay(0)
expect(runCount).toBe(2)
reobserve()
store.set(countAtom, increment)
await delay(0)
expect(runCount).toBe(3)
})

it('should return stable unobserve and reobserve functions', async () => {
const store = createStore()
const effect = () => {}
const unobserve1 = observe(effect, store)
const unobserve2 = observe(effect, store)
expect(unobserve1).toBe(unobserve2)
const reobserve1 = unobserve1()
const reobserve2 = unobserve2()
expect(reobserve1).toBe(reobserve2)
const unobserve3 = reobserve1()
const unobserve4 = reobserve2()
expect(unobserve3).not.toBe(unobserve1)
expect(unobserve3).toBe(unobserve4)
const reobserve3 = unobserve3()
const reobserve4 = unobserve4()
expect(reobserve3).not.toBe(reobserve1)
expect(reobserve3).toBe(reobserve4)
const unobserve5 = observe(effect, store)
expect(unobserve5).not.toBe(unobserve1)
})
})

0 comments on commit 8e08f14

Please sign in to comment.