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

✅ test: add unit test for atomWithReset utility #2753

Merged
merged 3 commits into from
Sep 27, 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
Prev Previous commit
apply review comments.
  • Loading branch information
vangie committed Sep 27, 2024
commit d26dc2314589eb37e6ef1dd672a15800d243cb90
21 changes: 0 additions & 21 deletions tests/vanilla/utils/atomWithReset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,24 @@ describe('atomWithReset', () => {
testAtom = atomWithReset(initialValue)
})

it('should create an atom with initial value', () => {
const { init } = testAtom
expect(init).toBe(initialValue)
})

it('should reset to initial value using RESET', () => {
const store = createStore()
store.set(testAtom, 123)
store.set(testAtom, RESET)
expect(store.get(testAtom)).toBe(initialValue)

const set = vi.fn()
const get = vi.fn(() => 20)
testAtom.write(get, set, RESET)
expect(set).toHaveBeenCalledWith(testAtom, initialValue)
})

it('should update atom with a new value', () => {
const store = createStore()
store.set(testAtom, 123)
store.set(testAtom, 30)
expect(store.get(testAtom)).toBe(30)

const set = vi.fn()
const get = vi.fn(() => 20)
testAtom.write(get, set, 30)
expect(set).toHaveBeenCalledWith(testAtom, 30)
})

it('should update atom using a function', () => {
const store = createStore()
store.set(testAtom, 123)
store.set(testAtom, (prev: number) => prev + 10)
expect(store.get(testAtom)).toBe(133)

const set = vi.fn()
const get = vi.fn(() => 20)
const updateFn = (prev: number) => prev + 10
testAtom.write(get, set, updateFn)
expect(set).toHaveBeenCalledWith(testAtom, 30)
})
})