Skip to content

Commit

Permalink
✅ test: add unit test for atomWithReset utility (#2753)
Browse files Browse the repository at this point in the history
* ✅ test: add unit test for atomWithReset utility

* apply review comments.

* apply review comments.
  • Loading branch information
vangie authored Sep 27, 2024
1 parent 609fe75 commit 9bd8ed7
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/vanilla/utils/atomWithReset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createStore } from 'jotai/vanilla'
import { RESET, atomWithReset } from 'jotai/vanilla/utils'

describe('atomWithReset', () => {
let initialValue: number
let testAtom: any

beforeEach(() => {
vi.clearAllMocks()
initialValue = 10
testAtom = atomWithReset(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)
})

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)
})

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)
})
})

0 comments on commit 9bd8ed7

Please sign in to comment.