Skip to content

Commit

Permalink
add failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Jan 15, 2025
1 parent 7addd25 commit cad3b8b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
24 changes: 24 additions & 0 deletions tests/testUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createStore } from 'jotai'

type Store = ReturnType<typeof createStore>
type GetAtomState = Parameters<Parameters<Store['unstable_derive']>[0]>[0]
type DebugStore = Store & { getAtomState: GetAtomState }

export function createDebugStore() {
let getAtomState: GetAtomState
const store = createStore().unstable_derive((...storeArgs) => {
;[getAtomState] = storeArgs
const [, setAtomState] = storeArgs
storeArgs[1] = (atom, atomState) => {
return setAtomState(
atom,
Object.assign(atomState, { label: atom.debugLabel }),
)
}
return storeArgs
})
if (getAtomState! === undefined) {
throw new Error('failed to create debug store')
}
return Object.assign(store, { getAtomState }) as DebugStore
}
9 changes: 8 additions & 1 deletion tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, it, vi } from 'vitest'
import { atom, createStore } from 'jotai/vanilla'
import { createDebugStore } from '../../tests/testUtils'

it('can propagate updates with async atom chains', async () => {
const store = createStore()
Expand Down Expand Up @@ -429,6 +430,7 @@ it('batches sync writes', () => {
it('mounts and unmounts sync and async dependencies correctly', async () => {
const mounted = [0, 0, 0, 0, 0] as [number, number, number, number, number]
const a = atom(0)
a.debugLabel = 'a'
a.onMount = () => {
++mounted[0]
return () => {
Expand All @@ -437,6 +439,7 @@ it('mounts and unmounts sync and async dependencies correctly', async () => {
}

const b = atom(0)
b.debugLabel = 'b'
b.onMount = () => {
++mounted[1]
return () => {
Expand All @@ -445,6 +448,7 @@ it('mounts and unmounts sync and async dependencies correctly', async () => {
}

const c = atom(0)
c.debugLabel = 'c'
c.onMount = () => {
++mounted[2]
return () => {
Expand All @@ -453,6 +457,7 @@ it('mounts and unmounts sync and async dependencies correctly', async () => {
}

const d = atom(0)
d.debugLabel = 'd'
d.onMount = () => {
++mounted[3]
return () => {
Expand All @@ -461,6 +466,7 @@ it('mounts and unmounts sync and async dependencies correctly', async () => {
}

const e = atom(0)
e.debugLabel = 'e'
e.onMount = () => {
++mounted[4]
return () => {
Expand Down Expand Up @@ -489,8 +495,9 @@ it('mounts and unmounts sync and async dependencies correctly', async () => {
})
return promise
})
f.debugLabel = 'f'

const store = createStore()
const store = createDebugStore()
// mount a, b synchronously
const unsub = store.sub(f, () => {})
expect(mounted).toEqual([1, 1, 0, 0, 0])
Expand Down

0 comments on commit cad3b8b

Please sign in to comment.