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/atomFamily.ts #6

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
61 changes: 61 additions & 0 deletions src/vanilla/utils/atomFamily.gru.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* 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 { type Atom } from '../../vanilla.ts'
import { atomFamily } from './atomFamily.ts'

describe('atomFamily', () => {
let mockAtom: Atom<unknown>

beforeEach(() => {
mockAtom = { read: vi.fn(), write: vi.fn() } as any
})

it('should create an atom', () => {
const family = atomFamily(() => mockAtom)
const atom = family('param1')
expect(atom).toBe(mockAtom)
})

it('should return the same atom for the same parameter', () => {
const family = atomFamily(() => mockAtom)
const atom1 = family('param1')
const atom2 = family('param1')
expect(atom1).toBe(atom2)
})

it('should remove an atom', () => {
const family = atomFamily(() => mockAtom)
family('param1')
family.remove('param1')
const atom = family('param1')
expect(atom).toBe(mockAtom)
})

it('should notify listeners on atom creation', () => {
const family = atomFamily(() => mockAtom)
const listener = vi.fn()
family.unstable_listen(listener)
family('param1')
expect(listener).toHaveBeenCalledWith({
type: 'CREATE',
param: 'param1',
atom: mockAtom,
})
})

it('should notify listeners on atom removal', () => {
const family = atomFamily(() => mockAtom)
const listener = vi.fn()
family.unstable_listen(listener)
family('param1')
family.remove('param1')
expect(listener).toHaveBeenCalledWith({
type: 'REMOVE',
param: 'param1',
atom: mockAtom,
})
})
})