-
-
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
✅ test: add unit test for atomWithReset utility #2753
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
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.
Thanks for working on adding tests. That would a great contribution.
Please see comments.
vi.mock('jotai/vanilla', async (importOriginal) => { | ||
const actual = await importOriginal<typeof import('jotai/vanilla')>() | ||
return { | ||
...actual, | ||
atom: vi.fn(actual.atom), | ||
} | ||
}) |
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
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 comment
The reason will be displayed to describe this comment to others. Learn more.
write
is an implementation detail, and we would like to test with store api.
can you try this?
const store = createStore();
store.set(testAtom, RESET);
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.
@dai-shi I have already added the test code for using createStore
and kept the previous test code, as it can isolate the dependency on createStore
.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
OK
Preview in LiveCodesLatest commit: d26dc23
See documentations for usage instructions. |
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 don't test the internal behavior.
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 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
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks for your contribution! Feel free to send more PRs to improve test coverage.
Summary
Add unit test for src/vanilla/utils/atomWithReset.ts
Check List
pnpm run prettier
for formatting code and docs