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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions tests/vanilla/utils/atomWithReset.test.ts
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

View workflow job for this annotation

GitHub Actions / test_matrix (4.0.5)

Parameter 'importOriginal' implicitly has an 'any' type.

Check failure on line 4 in tests/vanilla/utils/atomWithReset.test.ts

View workflow job for this annotation

GitHub Actions / test_matrix (3.9.7)

Parameter 'importOriginal' implicitly has an 'any' type.

Check failure on line 4 in tests/vanilla/utils/atomWithReset.test.ts

View workflow job for this annotation

GitHub Actions / test_matrix (3.8.3)

Parameter 'importOriginal' implicitly has an 'any' type.
const actual = await importOriginal<typeof import('jotai/vanilla')>()

Check failure on line 5 in tests/vanilla/utils/atomWithReset.test.ts

View workflow job for this annotation

GitHub Actions / test_matrix (4.0.5)

Untyped function calls may not accept type arguments.

Check failure on line 5 in tests/vanilla/utils/atomWithReset.test.ts

View workflow job for this annotation

GitHub Actions / test_matrix (3.9.7)

Untyped function calls may not accept type arguments.

Check failure on line 5 in tests/vanilla/utils/atomWithReset.test.ts

View workflow job for this annotation

GitHub Actions / test_matrix (3.8.3)

Untyped function calls may not accept type arguments.
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


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

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

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