-
-
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 1 commit
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,48 @@ | ||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { RESET, atomWithReset } from 'jotai/vanilla/utils' | ||
|
||
vi.mock('jotai/vanilla', async (importOriginal) => { | ||
Check failure on line 4 in tests/vanilla/utils/atomWithReset.test.ts GitHub Actions / test_matrix (4.0.5)
Check failure on line 4 in tests/vanilla/utils/atomWithReset.test.ts GitHub Actions / test_matrix (3.9.7)
|
||
const actual = await importOriginal<typeof import('jotai/vanilla')>() | ||
Check failure on line 5 in tests/vanilla/utils/atomWithReset.test.ts GitHub Actions / test_matrix (4.0.5)
Check failure on line 5 in tests/vanilla/utils/atomWithReset.test.ts GitHub Actions / test_matrix (3.9.7)
|
||
return { | ||
...actual, | ||
atom: vi.fn(actual.atom), | ||
} | ||
}) | ||
|
||
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) | ||
}) | ||
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. 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 commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
|
||
it('should reset to initial value using RESET', () => { | ||
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 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 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.
we haven't don this before, but it looks nice.
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.
I have remove it, it is useless in this case.
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