Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): update atom value with conditional dependencies (regression with #2363) #2534

Merged
merged 9 commits into from
May 21, 2024
37 changes: 37 additions & 0 deletions tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,40 @@ it('keeps atoms mounted between recalculations', async () => {
unmounted: 0,
})
})

it.skipIf(!import.meta.env?.USE_STORE2)(
'should not provide stale values to conditional dependents',
() => {
const dataAtom = atom<number[]>([])
const hasFilterAtom = atom(false)
const filteredAtom = atom((get) => {
const data = get(dataAtom)
const hasFilter = get(hasFilterAtom)
if (hasFilter) {
return []
} else {
return data
}
})
const stageAtom = atom((get) => {
const hasFilter = get(hasFilterAtom)
if (hasFilter) {
const filtered = get(filteredAtom)
return filtered.length === 0 ? 'is-empty' : 'has-data'
} else {
return 'no-filter'
}
})

const store = createStore()
store.sub(filteredAtom, () => undefined)
store.sub(stageAtom, () => undefined)

store.set(dataAtom, [100])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this here, or can use simply initialize the data atom with [100]?
If we need it here, please add a comment for clarification.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was important, but yeah the test still works without it.

expect(store.get(stageAtom), 'should start without filter').to.equal(
'no-filter',
)
store.set(hasFilterAtom, true)
expect(store.get(stageAtom), 'should update').to.equal('is-empty')
},
)
Loading