Skip to content
Myr

basic.test.ts

This file

Path

tests/vanilla/basic.test.ts

147 lines · 3.2 KiB · TypeScript

Last changed by

test(vanilla/basic): add test for 'getInitialState' (#3130)Wonsuk Choibefore adoptionad32cdf

History of this file

Take it

The walk stopped after 200 commits: lines older than f85898f point at it.

chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f

lines 1–72why, one step back
1import { afterEach, expect, it, vi } from 'vitest'
2import { createStore } from 'zustand/vanilla'3import type { StoreApi } from 'zustand/vanilla'45// To avoid include react deps on vanilla version6vi.mock('react', () => ({}))78const consoleError = console.error9afterEach(() => {10 console.error = consoleError11})1213it('create a store', () => {14 let params15 const result = createStore((...args) => {16 params = args17 return { value: null }18 })19 expect({ params, result }).toMatchInlineSnapshot(`20 {21 "params": [22 [Function],23 [Function],24 {25 "getInitialState": [Function],26 "getState": [Function],27 "setState": [Function],28 "subscribe": [Function],29 },30 ],31 "result": {32 "getInitialState": [Function],33 "getState": [Function],34 "setState": [Function],35 "subscribe": [Function],36 },37 }38 `)39})4041type CounterState = {42 count: number43 inc: () => void44}4546it('uses the store', async () => {47 const store = createStore<CounterState>((set) => ({48 count: 0,49 inc: () => set((state) => ({ count: state.count + 1 })),50 }))51 store.getState().inc()5253 expect(store.getState().count).toBe(1)54})5556it('can get the store', async () => {57 type State = {58 value: number59 getState1: () => State60 getState2: () => State61 }6263 const store = createStore<State>((_, get) => ({64 value: 1,65 getState1: () => get(),66 getState2: (): State => store.getState(),67 }))6869 expect(store.getState().getState1().value).toBe(1)70 expect(store.getState().getState2().value).toBe(1)71})72

test(vanilla/basic): add test for 'getInitialState' (#3130)Wonsuk Choibefore adoptionad32cdf

lines 73–79why, one step back
73it('can get the initial state', () => {
74 const initial = { value: 1 }75 const store = createStore(() => initial)76 store.setState({ value: 2 })77 expect(store.getInitialState()).toBe(initial)78})79

chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f

lines 80–147why, one step back
80it('can set the store', async () => {
81 type State = {82 value: number83 setState1: StoreApi<State>['setState']84 setState2: StoreApi<State>['setState']85 }8687 const store = createStore<State>((set) => ({88 value: 1,89 setState1: (v) => set(v),90 setState2: (v): void => store.setState(v),91 }))9293 store.getState().setState1({ value: 2 })94 expect(store.getState().value).toBe(2)95 store.getState().setState2({ value: 3 })96 expect(store.getState().value).toBe(3)97})9899it('both NaN should not update', () => {100 const store = createStore<number>(() => NaN)101 const fn = vi.fn()102103 store.subscribe(fn)104 store.setState(NaN)105106 expect(fn).not.toBeCalled()107})108109it('can set the store without merging', () => {110 const { setState, getState } = createStore<{ a: number } | { b: number }>(111 (_set) => ({112 a: 1,113 }),114 )115116 // Should override the state instead of merging.117 setState({ b: 2 }, true)118119 expect(getState()).toEqual({ b: 2 })120})121122it('can set the object store to null', () => {123 const { setState, getState } = createStore<{ a: number } | null>(() => ({124 a: 1,125 }))126127 setState(null)128129 expect(getState()).toEqual(null)130})131132it('can set the non-object store to null', () => {133 const { setState, getState } = createStore<string | null>(() => 'value')134135 setState(null)136137 expect(getState()).toEqual(null)138})139140it('works with non-object state', () => {141 const store = createStore<number>(() => 1)142 const inc = () => store.setState((c) => c + 1)143144 inc()145146 expect(store.getState()).toBe(2)147})