Skip to content

Commit

Permalink
store test from #2609
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky authored and dai-shi committed Jul 10, 2024
1 parent 1c35229 commit e92f1c1
Showing 1 changed file with 156 additions and 0 deletions.
156 changes: 156 additions & 0 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,159 @@ describe('aborting atoms', () => {
expect(callAfterAbort).toHaveBeenCalledTimes(1)
})
})

describe('unstable_resolve resolves the correct value for', () => {
function nextTask() {
return new Promise((resolve) => setTimeout(resolve))
}

it('primitive atom', async () => {
const store = createStore()
store.unstable_resolve = (atom) => {
if (atom === (pseudo as Atom<any>)) {
return a as unknown as typeof atom
}
return atom
}

const pseudo = atom('pseudo') as typeof a
pseudo.debugLabel = 'pseudo'
pseudo.onMount = (setSelf) => setSelf((v) => v + ':pseudo-mounted')
const a = atom('a')
a.debugLabel = 'a'
a.onMount = (setSelf) => setSelf((v) => v + ':a-mounted')

expect(store.get(pseudo)).toBe('a')
const callback = vi.fn()
store.sub(pseudo, callback)
expect(store.get(pseudo)).toBe('a:a-mounted')
store.set(pseudo, (v) => v + ':a-updated')
expect(store.get(pseudo)).toBe('a:a-mounted:a-updated')
await nextTask()
expect(store.get(pseudo)).toBe('a:a-mounted:a-updated')
})

it('derived atom', async () => {
const store = createStore()
store.unstable_resolve = (atom) => {
if (atom === (pseudo as Atom<any>)) {
return a as unknown as typeof atom
}
return atom
}

const pseudo = atom('pseudo') as typeof a
pseudo.debugLabel = 'pseudo'
pseudo.onMount = (setSelf) => setSelf((v) => v + ':pseudo-mounted')
const a = atom('a')
a.debugLabel = 'a'
a.onMount = (setSelf) => setSelf((v) => v + ':a-mounted')
const b = atom('b')
b.debugLabel = 'b'
const c = atom((get) => get(pseudo) + get(b))
c.debugLabel = 'c'
expect(store.get(c)).toBe('ab')
const d = atom(
(_get, { setSelf }) => setTimeout(setSelf, 0, 'd'),
(get, set, v) => set(pseudo, get(pseudo) + v),
)
store.get(d)
await nextTask()
expect(store.get(a)).toBe('ad')
expect(store.get(c)).toBe('adb')
expect(store.get(pseudo)).toEqual('ad')
const callback = vi.fn()
store.sub(c, callback)
expect(store.get(pseudo)).toBe('ad:a-mounted')
delete store.unstable_resolve
await nextTask()
expect(store.get(pseudo)).toEqual('pseudo')
store.sub(pseudo, callback)
expect(store.get(pseudo)).toEqual('pseudo:pseudo-mounted')
})

it('writable atom', async () => {
const store = createStore()
store.unstable_resolve = (atom) => {
if (atom === (pseudo as Atom<any>)) {
return a as unknown as typeof atom
}
return atom
}

const pseudoWriteFn = vi.fn()
const pseudo = atom('pseudo', pseudoWriteFn) as unknown as typeof a
pseudo.debugLabel = 'pseudo'
pseudo.onMount = (setSelf) => setSelf('pseudo-mounted')
const a = atom('a', (get, set, value: string) => {
set(pseudo, get(pseudo) + ':' + value)
return () => set(pseudo, get(pseudo) + ':a-unmounted')
})
a.debugLabel = 'a'
a.onMount = (setSelf) => setSelf('a-mounted')
expect(store.get(pseudo)).toBe('a')
const callback = vi.fn()
const unsub = store.sub(pseudo, callback)
await nextTask()
expect(store.get(pseudo)).toBe('a:a-mounted')
const value = store.set(pseudo, 'a-updated')
expect(pseudoWriteFn).not.toHaveBeenCalled()
expect(typeof value).toBe('function')
expect(store.get(pseudo)).toBe('a:a-mounted:a-updated')
unsub()
await nextTask()
expect(store.get(pseudo)).toBe('a:a-mounted:a-updated:a-unmounted')
})

it('this in read and write', async () => {
const store = createStore()
store.unstable_resolve = (atom) => {
if (atom === (pseudo as Atom<any>)) {
return this_read as unknown as typeof atom
}
return atom
}

const pseudo = atom('pseudo') as typeof this_read
pseudo.debugLabel = 'pseudo'
let i = 0
const this_read = atom(function read(this: any, get) {
return i++ % 2 ? get(this) : 'this_read'
})
this_read.debugLabel = 'this_read'
expect(store.get(pseudo)).toBe('this_read')

store.unstable_resolve = (atom) => {
if (atom === (pseudo as Atom<any>)) {
return this_write as unknown as typeof atom
}
return atom
}

const this_write = atom(
'this',
function write(this: any, get, set, value: string) {
set(this, get(this) + ':' + value)
return () => set(this, get(this) + ':this_write-unmounted')
},
)
this_write.debugLabel = 'this_write'
this_write.onMount = (setSelf) => setSelf('this_write-mounted')
expect(store.get(pseudo)).toBe('this')
const callback = vi.fn()
const unsub = store.sub(pseudo, callback)
await nextTask()
expect(store.get(pseudo)).toBe('this:this_write-mounted')
expect(callback).not.toHaveBeenCalledOnce()
store.set(this_write, 'this_write-updated')
expect(store.get(pseudo)).toBe('this:this_write-mounted:this_write-updated')
await nextTask()
unsub()
await nextTask()
expect(store.get(pseudo)).toBe(
'this:this_write-mounted:this_write-updated:this_write-unmounted',
)
delete store.unstable_resolve
expect(store.get(pseudo)).toBe('pseudo')
})
})

0 comments on commit e92f1c1

Please sign in to comment.