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 2 commits
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
56 changes: 56 additions & 0 deletions tests/vanilla/utils/atomWithReset.test.ts
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)
})
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 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)
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 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)
})
})