Skip to content

Commit

Permalink
add failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Jan 4, 2025
1 parent 4be4deb commit f7b8170
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1005,3 +1005,64 @@ it('should process all atom listeners even if some of them throw errors', () =>
expect(listenerB).toHaveBeenCalledTimes(1)
expect(listenerC).toHaveBeenCalledTimes(1)
})

it.only('runs recomputeDependents on atoms in the correct order', async () => {
const store = createStore().unstable_derive((getAtomState, ...storeArgs) => [
(a) => Object.assign(getAtomState(a), { label: a.debugLabel }),
...storeArgs,
])

Check failure on line 1013 in tests/vanilla/store.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.8.3)

Type '(<Value>(atom: Atom<Value>, get: Getter, options: { readonly signal: AbortSignal; readonly setSelf: never; }) => Value) | (<Value, Args extends unknown[], Result>(atom: WritableAtom<Value, Args, Result>, ...params: Parameters<...>) => Result) | (<Value, Args extends unknown[], Result>(atom: WritableAtom<...>, setAto...' is not assignable to type '<Value>(atom: Atom<Value>, get: Getter, options: { readonly signal: AbortSignal; readonly setSelf: never; }) => Value'.
let i = 0
function createHistoryAtoms<T>(initialValue: T) {
const historyStackAtom = atom<T[]>([initialValue])
historyStackAtom.debugLabel = `${i}:historyStackAtom`
const historyIndexAtom = atom(0)
historyIndexAtom.debugLabel = `${i}:historyIndexAtom`
const valueAtom = atom(
(get) => get(historyStackAtom)[get(historyIndexAtom)]!,
)
valueAtom.debugLabel = `${i}:valueAtom`
const resetAtom = atom(null, (_, set, value: T) => {
set(historyStackAtom, [value])
set(historyIndexAtom, 0)
})
resetAtom.debugLabel = `${i}:resetAtom`
i++
return { valueAtom, resetAtom }
}

const val1Atoms = createHistoryAtoms('foo')
const val2Atoms = createHistoryAtoms<string | null>(null)

const initAtom = atom(null, (_get, set) => {
// if comment out this line, the test will pass
console.log('initAtom write val2Atoms')
set(val2Atoms.resetAtom, null)
console.log('initAtom write val1Atoms')
set(val1Atoms.resetAtom, 'bar')
})
initAtom.debugLabel = 'initAtom'

const computedValAtom = atom((get) => {
const v2Value = get(val2Atoms.valueAtom)
if (v2Value !== null) {
console.log('computedValAtom read val2Atoms', v2Value)
return v2Value
}
const v1Value = get(val1Atoms.valueAtom)
console.log('computedValAtom read val2Atoms', v1Value)
return v1Value
})
computedValAtom.debugLabel = 'computedValAtom'

const asyncInitAtom = atom(null, async (_get, set) => {
// if comment out this line, the test will pass [DOES NOT WORK]
await new Promise((resolve) => setTimeout(resolve, 0))

set(initAtom)
})
store.sub(computedValAtom, () => {})
console.log('set asyncInitAtom')
await store.set(asyncInitAtom)
const result = store.get(computedValAtom)
expect(result).toBe('bar')
})

0 comments on commit f7b8170

Please sign in to comment.