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

Conversation

vangie
Copy link
Contributor

@vangie vangie commented Sep 27, 2024

Summary

Add unit test for src/vanilla/utils/atomWithReset.ts

Check List

  • pnpm run prettier for formatting code and docs

Copy link

vercel bot commented Sep 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 27, 2024 0:07am

Copy link

codesandbox-ci bot commented Sep 27, 2024

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.

Copy link
Member

@dai-shi dai-shi left a 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.

Comment on lines 4 to 10
vi.mock('jotai/vanilla', async (importOriginal) => {
const actual = await importOriginal<typeof import('jotai/vanilla')>()
return {
...actual,
atom: vi.fn(actual.atom),
}
})
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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)
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Copy link

github-actions bot commented Sep 27, 2024

LiveCodes Preview in LiveCodes

Latest commit: d26dc23
Last updated: Sep 27, 2024 12:07pm (UTC)

Playground Link
React demo https://livecodes.io?x=id/85KHDAFAN

See documentations for usage instructions.

Copy link
Member

@dai-shi dai-shi left a 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.

Comment on lines 15 to 18
it('should create an atom with initial value', () => {
const { init } = testAtom
expect(init).toBe(initialValue)
})
Copy link
Member

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.

Copy link
Contributor Author

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)
Copy link
Member

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.

Copy link
Member

@dai-shi dai-shi left a 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.

@dai-shi dai-shi merged commit 9bd8ed7 into pmndrs:main Sep 27, 2024
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants