subscribe.test.tsx
This file
Path
tests/subscribe.test.tsx
136 lines · 4.6 KiB · TypeScript
Last changed by
test: expand React subscribe test coverage (#3442)Mahmoud Hamdybefore adoption6330044
History of this fileTake it
The walk stopped after 200 commits: lines older than f85898f point at it.
test: expand React subscribe test coverage (#3442)Mahmoud Hamdybefore adoption6330044
line 1why, one step back1import { describe, expect, it, vi } from 'vitest'chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 2why, one step back2import { create } from 'zustand'test: expand React subscribe test coverage (#3442)Mahmoud Hamdybefore adoption6330044
line 3why, one step back3import { subscribeWithSelector } from 'zustand/middleware'chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 4–8why, one step back45describe('subscribe()', () => {6 it('should correctly have access to subscribe', () => {7 const { subscribe } = create(() => ({ value: 1 }))8 expect(typeof subscribe).toBe('function')test: expand React subscribe test coverage (#3442)Mahmoud Hamdybefore adoption6330044
lines 9–134why, one step back9 })1011 it('should not be called if new state identity is the same', () => {12 const spy = vi.fn()13 const initialState = { value: 1, other: 'a' }14 const useBoundStore = create(() => initialState)1516 useBoundStore.subscribe(spy)17 useBoundStore.setState(initialState)18 expect(spy).not.toHaveBeenCalled()19 })2021 it('should be called if new state identity is different', () => {22 const spy = vi.fn()23 const initialState = { value: 1, other: 'a' }24 const useBoundStore = create(() => initialState)2526 useBoundStore.subscribe(spy)27 useBoundStore.setState({ ...useBoundStore.getState() })28 expect(spy).toHaveBeenCalledWith(initialState, initialState)29 })3031 it('should not be called when state slice is the same', () => {32 const spy = vi.fn()33 const initialState = { value: 1, other: 'a' }34 const useBoundStore = create(subscribeWithSelector(() => initialState))3536 useBoundStore.subscribe((s) => s.value, spy)37 useBoundStore.setState({ other: 'b' })38 expect(spy).not.toHaveBeenCalled()39 })4041 it('should be called when state slice changes', () => {42 const spy = vi.fn()43 const initialState = { value: 1, other: 'a' }44 const useBoundStore = create(subscribeWithSelector(() => initialState))4546 useBoundStore.subscribe((s) => s.value, spy)47 useBoundStore.setState({ value: initialState.value + 1 })48 expect(spy).toHaveBeenCalledTimes(1)49 expect(spy).toHaveBeenCalledWith(initialState.value + 1, initialState.value)50 })5152 it('should not be called when equality checker returns true', () => {53 const spy = vi.fn()54 const initialState = { value: 1, other: 'a' }55 const useBoundStore = create(subscribeWithSelector(() => initialState))5657 useBoundStore.subscribe((s) => s, spy, { equalityFn: () => true })58 useBoundStore.setState({ value: initialState.value + 2 })59 expect(spy).not.toHaveBeenCalled()60 })6162 it('should be called when equality checker returns false', () => {63 const spy = vi.fn()64 const initialState = { value: 1, other: 'a' }65 const useBoundStore = create(subscribeWithSelector(() => initialState))6667 useBoundStore.subscribe((s) => s.value, spy, { equalityFn: () => false })68 useBoundStore.setState({ value: initialState.value + 2 })69 expect(spy).toHaveBeenCalledTimes(1)70 expect(spy).toHaveBeenCalledWith(initialState.value + 2, initialState.value)71 })7273 it('should unsubscribe correctly', () => {74 const spy = vi.fn()75 const initialState = { value: 1, other: 'a' }76 const useBoundStore = create(subscribeWithSelector(() => initialState))7778 const unsub = useBoundStore.subscribe((s) => s.value, spy)7980 useBoundStore.setState({ value: initialState.value + 1 })81 unsub()82 useBoundStore.setState({ value: initialState.value + 2 })8384 expect(spy).toHaveBeenCalledTimes(1)85 expect(spy).toHaveBeenCalledWith(initialState.value + 1, initialState.value)86 })8788 it('should call listener immediately when fireImmediately is true', () => {89 const spy = vi.fn()90 const initialState = { value: 1 }91 const useBoundStore = create(subscribeWithSelector(() => initialState))9293 useBoundStore.subscribe((s) => s.value, spy, { fireImmediately: true })9495 expect(spy).toHaveBeenCalledTimes(1)96 expect(spy).toHaveBeenCalledWith(1, 1)97 })9899 it('should pass previous and current state to listener', () => {100 const spy = vi.fn()101 const useBoundStore = create(() => ({ count: 0 }))102103 useBoundStore.subscribe(spy)104 useBoundStore.setState({ count: 1 })105106 expect(spy).toHaveBeenCalledWith({ count: 1 }, { count: 0 })107 })108109 it('should handle multiple subscribers', () => {110 const spy1 = vi.fn()111 const spy2 = vi.fn()112 const useBoundStore = create(() => ({ count: 0 }))113114 useBoundStore.subscribe(spy1)115 useBoundStore.subscribe(spy2)116 useBoundStore.setState({ count: 1 })117118 expect(spy1).toHaveBeenCalledTimes(1)119 expect(spy2).toHaveBeenCalledTimes(1)120 })121122 it('should not notify unsubscribed listener while other listeners remain', () => {123 const spy1 = vi.fn()124 const spy2 = vi.fn()125 const useBoundStore = create(() => ({ count: 0 }))126127 const unsub1 = useBoundStore.subscribe(spy1)128 useBoundStore.subscribe(spy2)129130 unsub1()131 useBoundStore.setState({ count: 1 })132133 expect(spy1).not.toHaveBeenCalled()134 expect(spy2).toHaveBeenCalledTimes(1)chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 135–136why, one step back135 })136})