Skip to content
Myr

shallow.test.tsx

This file

Path

tests/shallow.test.tsx

190 lines · 5.4 KiB · TypeScript

Last changed by

chore(deps): update dev dependencies (#3560)Daishi Katobefore adoption1f531ba

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–4why, one step back
1import { useState } from 'react'
2import { act, fireEvent, render, screen } from '@testing-library/react'3import { beforeEach, describe, expect, it, vi } from 'vitest'4import { create } from 'zustand'

test(shallow): update 'shallow' and 'useShallow' imports to use 'zustand/shallow' (#3157)Wonsuk Choibefore adoptiona6143e0

line 5why, one step back
5import { shallow, useShallow } from 'zustand/shallow'

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

lines 6–122why, one step back
6import { createWithEqualityFn } from 'zustand/traditional'
78describe('types', () => {9 it('works with useBoundStore and array selector (#1107)', () => {10 const useBoundStore = createWithEqualityFn(() => ({11 villages: [] as { name: string }[],12 }))13 const Component = () => {14 const villages = useBoundStore((state) => state.villages, shallow)15 return <>{villages.length}</>16 }17 expect(Component).toBeDefined()18 })1920 it('works with useBoundStore and string selector (#1107)', () => {21 const useBoundStore = createWithEqualityFn(() => ({22 refetchTimestamp: '',23 }))24 const Component = () => {25 const refetchTimestamp = useBoundStore(26 (state) => state.refetchTimestamp,27 shallow,28 )29 return <>{refetchTimestamp.toUpperCase()}</>30 }31 expect(Component).toBeDefined()32 })33})3435describe('useShallow', () => {36 const testUseShallowSimpleCallback = vi.fn()37 const TestUseShallowSimple = ({38 selector,39 state,40 }: {41 state: Record<string, unknown>42 selector: (state: Record<string, unknown>) => string[]43 }) => {44 const selectorOutput = selector(state)45 const useShallowOutput = useShallow(selector)(state)4647 return (48 <div49 data-testid="test-shallow"50 onClick={() =>51 testUseShallowSimpleCallback({ selectorOutput, useShallowOutput })52 }53 />54 )55 }5657 beforeEach(() => {58 testUseShallowSimpleCallback.mockClear()59 })6061 it('input and output selectors always return shallow equal values', () => {62 const { rerender } = render(63 <TestUseShallowSimple state={{ a: 1, b: 2 }} selector={Object.keys} />,64 )6566 expect(testUseShallowSimpleCallback).toHaveBeenCalledTimes(0)67 fireEvent.click(screen.getByTestId('test-shallow'))6869 const firstRender = testUseShallowSimpleCallback.mock.lastCall?.[0]7071 expect(testUseShallowSimpleCallback).toHaveBeenCalledTimes(1)72 expect(firstRender).toBeTruthy()73 expect(firstRender?.selectorOutput).toEqual(firstRender?.useShallowOutput)7475 rerender(76 <TestUseShallowSimple77 state={{ a: 1, b: 2, c: 3 }}78 selector={Object.keys}79 />,80 )8182 fireEvent.click(screen.getByTestId('test-shallow'))83 expect(testUseShallowSimpleCallback).toHaveBeenCalledTimes(2)8485 const secondRender = testUseShallowSimpleCallback.mock.lastCall?.[0]8687 expect(secondRender).toBeTruthy()88 expect(secondRender?.selectorOutput).toEqual(secondRender?.useShallowOutput)89 })9091 it('returns the previously computed instance when possible', () => {92 const state = { a: 1, b: 2 }93 const { rerender } = render(94 <TestUseShallowSimple state={state} selector={Object.keys} />,95 )9697 fireEvent.click(screen.getByTestId('test-shallow'))98 expect(testUseShallowSimpleCallback).toHaveBeenCalledTimes(1)99 const output1 =100 testUseShallowSimpleCallback.mock.lastCall?.[0]?.useShallowOutput101 expect(output1).toBeTruthy()102103 // Change selector, same output104 rerender(105 <TestUseShallowSimple106 state={state}107 selector={(state) => Object.keys(state)}108 />,109 )110111 fireEvent.click(screen.getByTestId('test-shallow'))112 expect(testUseShallowSimpleCallback).toHaveBeenCalledTimes(2)113114 const output2 =115 testUseShallowSimpleCallback.mock.lastCall?.[0]?.useShallowOutput116 expect(output2).toBeTruthy()117118 expect(output2).toBe(output1)119 })120121 it('only re-renders if selector output has changed according to shallow', () => {122 let countRenders = 0

chore(deps): update dev dependencies (#3560)Daishi Katobefore adoption1f531ba

lines 123–127why, one step back
123 const useMyStore = create((): Record<string, unknown> => ({
124 a: 1,125 b: 2,126 c: 3,127 }))

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

lines 128–160why, one step back
128 const TestShallow = ({
129 selector = (state) => Object.keys(state).sort(),130 }: {131 selector?: (state: Record<string, unknown>) => string[]132 }) => {133 const output = useMyStore(useShallow(selector))134135 ++countRenders136137 return <div data-testid="test-shallow">{output.join(',')}</div>138 }139140 expect(countRenders).toBe(0)141 render(<TestShallow />)142143 expect(countRenders).toBe(1)144 expect(screen.getByTestId('test-shallow')).toHaveTextContent('a,b,c')145146 act(() => {147 useMyStore.setState({ a: 4 }) // This will not cause a re-render.148 })149150 expect(countRenders).toBe(1)151152 act(() => {153 useMyStore.setState({ d: 10 }) // This will cause a re-render.154 })155156 expect(countRenders).toBe(2)157 expect(screen.getByTestId('test-shallow')).toHaveTextContent('a,b,c,d')158 })159160 it('does not cause stale closure issues', () => {

chore(deps): update dev dependencies (#3560)Daishi Katobefore adoption1f531ba

lines 161–165why, one step back
161 const useMyStore = create((): Record<string, unknown> => ({
162 a: 1,163 b: 2,164 c: 3,165 }))

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

lines 166–190why, one step back
166 const TestShallowWithState = () => {
167 const [count, setCount] = useState(0)168 const output = useMyStore(169 useShallow((state) => Object.keys(state).concat([count.toString()])),170 )171172 return (173 <div174 data-testid="test-shallow"175 onClick={() => setCount((prev) => ++prev)}176 >177 {output.join(',')}178 </div>179 )180 }181182 render(<TestShallowWithState />)183184 expect(screen.getByTestId('test-shallow')).toHaveTextContent('a,b,c,0')185186 fireEvent.click(screen.getByTestId('test-shallow'))187188 expect(screen.getByTestId('test-shallow')).toHaveTextContent('a,b,c,1')189 })190})