-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(atomFamily): supports instanceof
- Loading branch information
Showing
2 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { expect, it, vi } from 'vitest' | ||
import { Atom, atom, createStore } from 'jotai/vanilla' | ||
Check failure on line 2 in tests/vanilla/utils/atomFamily.test.ts GitHub Actions / lint
Check failure on line 2 in tests/vanilla/utils/atomFamily.test.ts GitHub Actions / test_matrix (5.4.5)
Check failure on line 2 in tests/vanilla/utils/atomFamily.test.ts GitHub Actions / test_matrix (5.3.3)
Check failure on line 2 in tests/vanilla/utils/atomFamily.test.ts GitHub Actions / test_matrix (5.2.2)
Check failure on line 2 in tests/vanilla/utils/atomFamily.test.ts GitHub Actions / test_matrix (5.1.6)
|
||
import { atomFamily } from 'jotai/vanilla/utils' | ||
|
||
it('should create atoms with different params', () => { | ||
const store = createStore() | ||
const aFamily = atomFamily((param: number) => atom(param)) | ||
|
||
expect(store.get(aFamily(1))).toEqual(1) | ||
expect(store.get(aFamily(2))).toEqual(2) | ||
}) | ||
|
||
it('should remove atoms', () => { | ||
const store = createStore() | ||
const initializeAtom = vi.fn((param: number) => atom(param)) | ||
const aFamily = atomFamily(initializeAtom) | ||
|
||
expect(store.get(aFamily(1))).toEqual(1) | ||
expect(store.get(aFamily(2))).toEqual(2) | ||
aFamily.remove(2) | ||
initializeAtom.mockClear() | ||
expect(store.get(aFamily(1))).toEqual(1) | ||
expect(initializeAtom).toHaveBeenCalledTimes(0) | ||
expect(store.get(aFamily(2))).toEqual(2) | ||
expect(initializeAtom).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should remove atoms with custom comparator', () => { | ||
const store = createStore() | ||
const initializeAtom = vi.fn((param: number) => atom(param)) | ||
const aFamily = atomFamily(initializeAtom, (a, b) => a === b) | ||
|
||
expect(store.get(aFamily(1))).toEqual(1) | ||
expect(store.get(aFamily(2))).toEqual(2) | ||
expect(store.get(aFamily(3))).toEqual(3) | ||
aFamily.remove(2) | ||
initializeAtom.mockClear() | ||
expect(store.get(aFamily(1))).toEqual(1) | ||
expect(initializeAtom).toHaveBeenCalledTimes(0) | ||
expect(store.get(aFamily(2))).toEqual(2) | ||
expect(initializeAtom).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should remove atoms with custom shouldRemove', () => { | ||
const store = createStore() | ||
const initializeAtom = vi.fn((param: number) => atom(param)) | ||
const aFamily = atomFamily<number, Atom<number>>(initializeAtom) | ||
expect(store.get(aFamily(1))).toEqual(1) | ||
expect(store.get(aFamily(2))).toEqual(2) | ||
expect(store.get(aFamily(3))).toEqual(3) | ||
aFamily.setShouldRemove((_createdAt, param) => param % 2 === 0) | ||
initializeAtom.mockClear() | ||
expect(store.get(aFamily(1))).toEqual(1) | ||
expect(initializeAtom).toHaveBeenCalledTimes(0) | ||
expect(store.get(aFamily(2))).toEqual(2) | ||
expect(initializeAtom).toHaveBeenCalledTimes(1) | ||
expect(store.get(aFamily(3))).toEqual(3) | ||
expect(initializeAtom).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should support instanceof', () => { | ||
const aFamilyA = atomFamily((param: number) => atom(param)) | ||
const aFamilyB = atomFamily((param: number) => atom(param)) | ||
expect(aFamilyA(1) instanceof aFamilyA).toEqual(true) | ||
expect(aFamilyA(2) instanceof aFamilyA).toEqual(true) | ||
expect(aFamilyA(1) instanceof aFamilyB).toEqual(false) | ||
expect(aFamilyA(1) instanceof atom).toEqual(false) | ||
expect(atom(2) instanceof aFamilyA).toEqual(false) | ||
}) |