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

add tests: can read sync derived atom in write without initializing #2824

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions 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 type { Getter } from 'jotai/vanilla'
dai-shi marked this conversation as resolved.
Show resolved Hide resolved

it('can propagate updates with async atom chains', async () => {
const store = createStore()
Expand Down Expand Up @@ -334,3 +335,15 @@ it('handles complex dependency chains', async () => {
resolve()
expect(await promise2).toBe(10)
})

it('can read sync derived atom in write without initializing', () => {
const store = createStore()
const a = atom(0)
const b = atom((get) => get(a) + 1)
const c = atom(null, (get, set) => set(a, get(b)))
store.set(c)
expect(store.get(a)).toBe(1)
store.set(c)
// note: this is why write get needs to update deps
expect(store.get(a)).toBe(2)
})