From 3842038da218775a242771b1cac139da555f96c0 Mon Sep 17 00:00:00 2001 From: Vangie Du Date: Fri, 27 Sep 2024 14:42:52 +0800 Subject: [PATCH 1/3] :white_check_mark: test: add unit test for atomWithReset utility --- tests/vanilla/utils/atomWithReset.test.ts | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/vanilla/utils/atomWithReset.test.ts diff --git a/tests/vanilla/utils/atomWithReset.test.ts b/tests/vanilla/utils/atomWithReset.test.ts new file mode 100644 index 0000000000..12f133a2fb --- /dev/null +++ b/tests/vanilla/utils/atomWithReset.test.ts @@ -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) => { + const actual = await importOriginal() + return { + ...actual, + atom: vi.fn(actual.atom), + } +}) + +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) + }) +}) From 22f08e7eb608256396dc0c302f9b0d838151785f Mon Sep 17 00:00:00 2001 From: Vangie Du Date: Fri, 27 Sep 2024 16:37:30 +0800 Subject: [PATCH 2/3] apply review comments. --- tests/vanilla/utils/atomWithReset.test.ts | 24 +++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/vanilla/utils/atomWithReset.test.ts b/tests/vanilla/utils/atomWithReset.test.ts index 12f133a2fb..ce47faa6b1 100644 --- a/tests/vanilla/utils/atomWithReset.test.ts +++ b/tests/vanilla/utils/atomWithReset.test.ts @@ -1,14 +1,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' +import { createStore } from 'jotai/vanilla' import { RESET, atomWithReset } from 'jotai/vanilla/utils' -vi.mock('jotai/vanilla', async (importOriginal) => { - const actual = await importOriginal() - return { - ...actual, - atom: vi.fn(actual.atom), - } -}) - describe('atomWithReset', () => { let initialValue: number let testAtom: any @@ -25,6 +18,11 @@ describe('atomWithReset', () => { }) 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) @@ -32,6 +30,11 @@ describe('atomWithReset', () => { }) 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) @@ -39,6 +42,11 @@ describe('atomWithReset', () => { }) 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 From d26dc2314589eb37e6ef1dd672a15800d243cb90 Mon Sep 17 00:00:00 2001 From: Vangie Du Date: Fri, 27 Sep 2024 20:06:57 +0800 Subject: [PATCH 3/3] apply review comments. --- tests/vanilla/utils/atomWithReset.test.ts | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/tests/vanilla/utils/atomWithReset.test.ts b/tests/vanilla/utils/atomWithReset.test.ts index ce47faa6b1..7943bfcfb1 100644 --- a/tests/vanilla/utils/atomWithReset.test.ts +++ b/tests/vanilla/utils/atomWithReset.test.ts @@ -12,21 +12,11 @@ describe('atomWithReset', () => { 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 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) - expect(set).toHaveBeenCalledWith(testAtom, initialValue) }) it('should update atom with a new value', () => { @@ -34,11 +24,6 @@ describe('atomWithReset', () => { 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', () => { @@ -46,11 +31,5 @@ describe('atomWithReset', () => { 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) }) })