-
-
Notifications
You must be signed in to change notification settings - Fork 622
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
const store = createStore();
store.set(testAtom, RESET); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dai-shi I have already added the test code for using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's testing the internal behavior, which isn't guaranteed to work as it is now (it's likely though), so let's remove it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
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) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing the internal behavior. While it's correct, let's remove from the atomWithReset test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK