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

[Gru Tester] Add unit test for src/vanilla/utils/atomWithReset.ts #11

Open
wants to merge 1 commit into
base: gru-unit-test
Choose a base branch
from
Open
Changes from all 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
54 changes: 54 additions & 0 deletions src/vanilla/utils/atomWithReset.gru.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { atom } from '../../vanilla.ts'
import { atomWithReset } from './atomWithReset.ts'
import { RESET } from './constants.ts'

vi.mock('../../vanilla.ts', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../vanilla.ts')>()
return {
...actual,
atom: vi.fn(actual.atom),
}
})
Comment on lines +1 to +16
Copy link

Choose a reason for hiding this comment

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

Suggested change
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { atom } from '../../vanilla.ts'
import { atomWithReset } from './atomWithReset.ts'
import { RESET } from './constants.ts'
vi.mock('../../vanilla.ts', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../vanilla.ts')>()
return {
...actual,
atom: vi.fn(actual.atom),
}
})
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { RESET, atomWithReset } from 'jotai/vanilla/utils'
vi.mock('jotai/vanilla', async (importOriginal) => {
const actual = await importOriginal<typeof import('jotai/vanilla')>()
return {
...actual,
atom: vi.fn(actual.atom),
}
})

最终过 lint 的代码是这样的。我们在这个项目里 lint 似乎没有开启。


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

it('should reset to initial value using RESET', () => {
const set = vi.fn()
const get = vi.fn(() => 20)
testAtom.write(get, set, RESET)
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)
})
})