From 9bd8ed7b0e523af5366f6b762104241a0bb71203 Mon Sep 17 00:00:00 2001 From: Vangie Du Date: Fri, 27 Sep 2024 20:24:59 +0800 Subject: [PATCH] :white_check_mark: test: add unit test for atomWithReset utility (#2753) * :white_check_mark: test: add unit test for atomWithReset utility * apply review comments. * apply review comments. --- tests/vanilla/utils/atomWithReset.test.ts | 35 +++++++++++++++++++++++ 1 file changed, 35 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..7943bfcfb1 --- /dev/null +++ b/tests/vanilla/utils/atomWithReset.test.ts @@ -0,0 +1,35 @@ +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 reset to initial value using RESET', () => { + const store = createStore() + store.set(testAtom, 123) + store.set(testAtom, RESET) + expect(store.get(testAtom)).toBe(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) + }) + + 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) + }) +})