Skip to content
Myr

basic.test.tsx

This file

Path

tests/basic.test.tsx

744 lines · 17.1 KiB · TypeScript

Last changed by

chore(deps): update dev dependencies (#3486)Daishi Katobefore adoption8ec2169

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–16why, one step back
1import {
2 Component as ClassComponent,3 StrictMode,4 useEffect,5 useLayoutEffect,6 useState,7} from 'react'8import type { ReactNode } from 'react'9import { act, fireEvent, render, screen } from '@testing-library/react'10import ReactDOM from 'react-dom'11import { afterEach, expect, it, vi } from 'vitest'12import { create } from 'zustand'13import type { StoreApi } from 'zustand'14import { createWithEqualityFn } from 'zustand/traditional'1516const consoleError = console.error

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 17why, one step back
17

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

lines 18–49why, one step back
18afterEach(() => {
19 console.error = consoleError20})2122it('creates a store hook and api object', () => {23 let params24 const result = create((...args) => {25 params = args26 return { value: null }27 })28 expect({ params, result }).toMatchInlineSnapshot(`29 {30 "params": [31 [Function],32 [Function],33 {34 "getInitialState": [Function],35 "getState": [Function],36 "setState": [Function],37 "subscribe": [Function],38 },39 ],40 "result": [Function],41 }42 `)43})4445type CounterState = {46 count: number47 inc: () => void48}49

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 50why, one step back
50it('uses the store with no args', () => {

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

lines 51–67why, one step back
51 const useBoundStore = create<CounterState>((set) => ({
52 count: 0,53 inc: () => set((state) => ({ count: state.count + 1 })),54 }))5556 function Counter() {57 const { count, inc } = useBoundStore()58 useEffect(inc, [inc])59 return <div>count: {count}</div>60 }6162 render(63 <>64 <Counter />65 </>,66 )67

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 68why, one step back
68 expect(screen.getByText('count: 1')).toBeInTheDocument()

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

lines 69–70why, one step back
69})
70

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 71why, one step back
71it('uses the store with selectors', () => {

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

lines 72–89why, one step back
72 const useBoundStore = create<CounterState>((set) => ({
73 count: 0,74 inc: () => set((state) => ({ count: state.count + 1 })),75 }))7677 function Counter() {78 const count = useBoundStore((s) => s.count)79 const inc = useBoundStore((s) => s.inc)80 useEffect(inc, [inc])81 return <div>count: {count}</div>82 }8384 render(85 <>86 <Counter />87 </>,88 )89

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 90why, one step back
90 expect(screen.getByText('count: 1')).toBeInTheDocument()

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

lines 91–92why, one step back
91})
92

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 93why, one step back
93it('uses the store with a selector and equality checker', () => {

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

lines 94–119why, one step back
94 const useBoundStore = createWithEqualityFn(
95 () => ({ item: { value: 0 } }),96 Object.is,97 )98 const { setState } = useBoundStore99 let renderCount = 0100101 function Component() {102 // Prevent re-render if new value === 1.103 const item = useBoundStore(104 (s) => s.item,105 (_, newItem) => newItem.value === 1,106 )107 return (108 <div>109 renderCount: {++renderCount}, value: {item.value}110 </div>111 )112 }113114 render(115 <>116 <Component />117 </>,118 )119

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 120why, one step back
120 expect(screen.getByText('renderCount: 1, value: 0')).toBeInTheDocument()

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

lines 121–123why, one step back
121
122 // This will not cause a re-render.123 act(() => setState({ item: { value: 1 } }))

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 124why, one step back
124 expect(screen.getByText('renderCount: 1, value: 0')).toBeInTheDocument()

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

lines 125–127why, one step back
125
126 // This will cause a re-render.127 act(() => setState({ item: { value: 2 } }))

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 128why, one step back
128 expect(screen.getByText('renderCount: 2, value: 2')).toBeInTheDocument()

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

lines 129–130why, one step back
129})
130

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 131why, one step back
131it('only re-renders if selected state has changed', () => {

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

lines 132–159why, one step back
132 const useBoundStore = create<CounterState>((set) => ({
133 count: 0,134 inc: () => set((state) => ({ count: state.count + 1 })),135 }))136 let counterRenderCount = 0137 let controlRenderCount = 0138139 function Counter() {140 const count = useBoundStore((state) => state.count)141 counterRenderCount++142 return <div>count: {count}</div>143 }144145 function Control() {146 const inc = useBoundStore((state) => state.inc)147 controlRenderCount++148 return <button onClick={inc}>button</button>149 }150151 render(152 <>153 <Counter />154 <Control />155 </>,156 )157158 fireEvent.click(screen.getByText('button'))159

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 160why, one step back
160 expect(screen.getByText('count: 1')).toBeInTheDocument()

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

lines 161–165why, one step back
161
162 expect(counterRenderCount).toBe(2)163 expect(controlRenderCount).toBe(1)164})165

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 166why, one step back
166it('can batch updates', () => {

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

lines 167–188why, one step back
167 const useBoundStore = create<CounterState>((set) => ({
168 count: 0,169 inc: () => set((state) => ({ count: state.count + 1 })),170 }))171172 function Counter() {173 const { count, inc } = useBoundStore()174 useEffect(() => {175 ReactDOM.unstable_batchedUpdates(() => {176 inc()177 inc()178 })179 }, [inc])180 return <div>count: {count}</div>181 }182183 render(184 <>185 <Counter />186 </>,187 )188

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 189why, one step back
189 expect(screen.getByText('count: 2')).toBeInTheDocument()

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

lines 190–191why, one step back
190})
191

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 192why, one step back
192it('can update the selector', () => {

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

lines 193–208why, one step back
193 type State = { one: string; two: string }
194 type Props = { selector: (state: State) => string }195 const useBoundStore = create<State>(() => ({196 one: 'one',197 two: 'two',198 }))199200 function Component({ selector }: Props) {201 return <div>{useBoundStore(selector)}</div>202 }203204 const { rerender } = render(205 <StrictMode>206 <Component selector={(s) => s.one} />207 </StrictMode>,208 )

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 209why, one step back
209 expect(screen.getByText('one')).toBeInTheDocument()

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

lines 210–215why, one step back
210
211 rerender(212 <StrictMode>213 <Component selector={(s) => s.two} />214 </StrictMode>,215 )

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 216why, one step back
216 expect(screen.getByText('two')).toBeInTheDocument()

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

lines 217–218why, one step back
217})
218

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 219why, one step back
219it('can update the equality checker', () => {

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

lines 220–247why, one step back
220 type State = { value: number }
221 type Props = { equalityFn: (a: State, b: State) => boolean }222 const useBoundStore = createWithEqualityFn<State>(223 () => ({ value: 0 }),224 Object.is,225 )226 const { setState } = useBoundStore227 const selector = (s: State) => s228229 let renderCount = 0230 function Component({ equalityFn }: Props) {231 const { value } = useBoundStore(selector, equalityFn)232 return (233 <div>234 renderCount: {++renderCount}, value: {value}235 </div>236 )237 }238239 // Set an equality checker that always returns false to always re-render.240 const { rerender } = render(241 <>242 <Component equalityFn={() => false} />243 </>,244 )245246 // This will cause a re-render due to the equality checker.247 act(() => setState({ value: 0 }))

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 248why, one step back
248 expect(screen.getByText('renderCount: 2, value: 0')).toBeInTheDocument()

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

lines 249–258why, one step back
249
250 // Set an equality checker that always returns true to never re-render.251 rerender(252 <>253 <Component equalityFn={() => true} />254 </>,255 )256257 // This will NOT cause a re-render due to the equality checker.258 act(() => setState({ value: 1 }))

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 259why, one step back
259 expect(screen.getByText('renderCount: 3, value: 0')).toBeInTheDocument()

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

lines 260–261why, one step back
260})
261

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 262why, one step back
262it('can call useBoundStore with progressively more arguments', () => {

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

lines 263–290why, one step back
263 type State = { value: number }
264 type Props = {265 selector?: (state: State) => number266 equalityFn?: (a: number, b: number) => boolean267 }268269 const useBoundStore = createWithEqualityFn<State>(270 () => ({ value: 0 }),271 Object.is,272 )273 const { setState } = useBoundStore274275 let renderCount = 0276 function Component({ selector, equalityFn }: Props) {277 const value = useBoundStore(selector as any, equalityFn)278 return (279 <div>280 renderCount: {++renderCount}, value: {JSON.stringify(value)}281 </div>282 )283 }284285 // Render with no args.286 const { rerender } = render(287 <>288 <Component />289 </>,290 )

test(*): add 'expect' with 'toBeInTheDocument' (#3162)Wonsuk Choibefore adoption295bf97

line 291why, one step back
291 expect(

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 292why, one step back
292 screen.getByText('renderCount: 1, value: {"value":0}'),

test(*): add 'expect' with 'toBeInTheDocument' (#3162)Wonsuk Choibefore adoption295bf97

line 293why, one step back
293 ).toBeInTheDocument()

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

lines 294–300why, one step back
294
295 // Render with selector.296 rerender(297 <>298 <Component selector={(s) => s.value} />299 </>,300 )

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 301why, one step back
301 expect(screen.getByText('renderCount: 2, value: 0')).toBeInTheDocument()

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

lines 302–314why, one step back
302
303 // Render with selector and equality checker.304 rerender(305 <>306 <Component307 selector={(s) => s.value}308 equalityFn={(oldV, newV) => oldV > newV}309 />310 </>,311 )312313 // Should not cause a re-render because new value is less than previous.314 act(() => setState({ value: -1 }))

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 315why, one step back
315 expect(screen.getByText('renderCount: 3, value: 0')).toBeInTheDocument()

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

lines 316–317why, one step back
316
317 act(() => setState({ value: 1 }))

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 318why, one step back
318 expect(screen.getByText('renderCount: 4, value: 1')).toBeInTheDocument()

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

lines 319–320why, one step back
319})
320

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 321why, one step back
321it('can throw an error in selector', () => {

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

lines 322–343why, one step back
322 console.error = vi.fn()
323 type State = { value: string | number }324325 const initialState: State = { value: 'foo' }326 const useBoundStore = create<State>(() => initialState)327 const { setState } = useBoundStore328 const selector = (s: State) =>329 // @ts-expect-error This function is supposed to throw an error330 s.value.toUpperCase()331332 class ErrorBoundary extends ClassComponent<333 { children?: ReactNode | undefined },334 { hasError: boolean }335 > {336 constructor(props: { children?: ReactNode | undefined }) {337 super(props)338 this.state = { hasError: false }339 }340 static getDerivedStateFromError() {341 return { hasError: true }342 }343 render() {

chore(eslint): enable 'testing-library', 'jest-dom', 'vitest' plugins and fix violations (#3321)Wonsuk Choibefore adoption234a948

line 344why, one step back
344 // eslint-disable-next-line testing-library/no-node-access

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

lines 345–361why, one step back
345 return this.state.hasError ? <div>errored</div> : this.props.children
346 }347 }348349 function Component() {350 useBoundStore(selector)351 return <div>no error</div>352 }353354 render(355 <StrictMode>356 <ErrorBoundary>357 <Component />358 </ErrorBoundary>359 </StrictMode>,360 )361

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 362why, one step back
362 expect(screen.getByText('no error')).toBeInTheDocument()

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

lines 363–366why, one step back
363
364 act(() => {365 setState({ value: 123 })366 })

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 367why, one step back
367 expect(screen.getByText('errored')).toBeInTheDocument()

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

lines 368–369why, one step back
368})
369

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 370why, one step back
370it('can throw an error in equality checker', () => {

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

lines 371–393why, one step back
371 console.error = vi.fn()
372 type State = { value: string | number }373374 const initialState: State = { value: 'foo' }375 const useBoundStore = createWithEqualityFn(() => initialState, Object.is)376 const { setState } = useBoundStore377 const selector = (s: State) => s378 const equalityFn = (a: State, b: State) =>379 // @ts-expect-error This function is supposed to throw an error380 a.value.trim() === b.value.trim()381382 class ErrorBoundary extends ClassComponent<383 { children?: ReactNode | undefined },384 { hasError: boolean }385 > {386 constructor(props: { children?: ReactNode | undefined }) {387 super(props)388 this.state = { hasError: false }389 }390 static getDerivedStateFromError() {391 return { hasError: true }392 }393 render() {

chore(eslint): enable 'testing-library', 'jest-dom', 'vitest' plugins and fix violations (#3321)Wonsuk Choibefore adoption234a948

line 394why, one step back
394 // eslint-disable-next-line testing-library/no-node-access

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

lines 395–411why, one step back
395 return this.state.hasError ? <div>errored</div> : this.props.children
396 }397 }398399 function Component() {400 useBoundStore(selector, equalityFn)401 return <div>no error</div>402 }403404 render(405 <StrictMode>406 <ErrorBoundary>407 <Component />408 </ErrorBoundary>409 </StrictMode>,410 )411

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 412why, one step back
412 expect(screen.getByText('no error')).toBeInTheDocument()

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

lines 413–416why, one step back
413
414 act(() => {415 setState({ value: 123 })416 })

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 417why, one step back
417 expect(screen.getByText('errored')).toBeInTheDocument()

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

lines 418–481why, one step back
418})
419420it('can get the store', () => {421 type State = {422 value: number423 getState1: () => State424 getState2: () => State425 }426 const { getState } = create<State>((_, get) => ({427 value: 1,428 getState1: () => get(),429 getState2: (): State => getState(),430 }))431432 expect(getState().getState1().value).toBe(1)433 expect(getState().getState2().value).toBe(1)434})435436it('can set the store', () => {437 type State = {438 value: number439 setState1: StoreApi<State>['setState']440 setState2: StoreApi<State>['setState']441 }442443 const { setState, getState } = create<State>((set) => ({444 value: 1,445 setState1: (v) => set(v),446 setState2: (v) => setState(v),447 }))448449 getState().setState1({ value: 2 })450 expect(getState().value).toBe(2)451 getState().setState2({ value: 3 })452 expect(getState().value).toBe(3)453 getState().setState1((s) => ({ value: ++s.value }))454 expect(getState().value).toBe(4)455 getState().setState2((s) => ({ value: ++s.value }))456 expect(getState().value).toBe(5)457})458459it('both NaN should not update', () => {460 const { setState, subscribe } = create<number>(() => NaN)461462 const fn = vi.fn()463 subscribe(fn)464465 setState(NaN)466467 expect(fn).not.toBeCalled()468})469470it('can set the store without merging', () => {471 const { setState, getState } = create<{ a: number } | { b: number }>(472 (_set) => ({473 a: 1,474 }),475 )476477 // Should override the state instead of merging.478 setState({ b: 2 }, true)479 expect(getState()).toEqual({ b: 2 })480})481

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 482why, one step back
482it('only calls selectors when necessary with static selector', () => {

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

lines 483–506why, one step back
483 type State = { a: number; b: number }
484 const useBoundStore = createWithEqualityFn<State>(() => ({ a: 0, b: 0 }))485 const { setState } = useBoundStore486 let staticSelectorCallCount = 0487488 function staticSelector(s: State) {489 staticSelectorCallCount++490 return s.a491 }492493 function Component() {494 useBoundStore(staticSelector)495 return (496 <>497 <div>static: {staticSelectorCallCount}</div>498 </>499 )500 }501502 const { rerender } = render(503 <>504 <Component />505 </>,506 )

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 507why, one step back
507 expect(screen.getByText('static: 1')).toBeInTheDocument()

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

lines 508–513why, one step back
508
509 rerender(510 <>511 <Component />512 </>,513 )

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 514why, one step back
514 expect(screen.getByText('static: 1')).toBeInTheDocument()

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

lines 515–516why, one step back
515
516 act(() => setState({ a: 1, b: 1 }))

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 517why, one step back
517 expect(screen.getByText('static: 2')).toBeInTheDocument()

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

lines 518–519why, one step back
518})
519

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 520why, one step back
520it('only calls selectors when necessary (traditional)', () => {

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

lines 521–547why, one step back
521 type State = { a: number; b: number }
522 const useBoundStore = createWithEqualityFn<State>(() => ({ a: 0, b: 0 }))523 const { setState } = useBoundStore524 let inlineSelectorCallCount = 0525 let staticSelectorCallCount = 0526527 function staticSelector(s: State) {528 staticSelectorCallCount++529 return s.a530 }531532 function Component() {533 useBoundStore((s) => (inlineSelectorCallCount++, s.b))534 useBoundStore(staticSelector)535 return (536 <>537 <div>inline: {inlineSelectorCallCount}</div>538 <div>static: {staticSelectorCallCount}</div>539 </>540 )541 }542543 const { rerender } = render(544 <>545 <Component />546 </>,547 )

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

lines 548–549why, one step back
548 expect(screen.getByText('inline: 1')).toBeInTheDocument()
549 expect(screen.getByText('static: 1')).toBeInTheDocument()

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

lines 550–555why, one step back
550
551 rerender(552 <>553 <Component />554 </>,555 )

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

lines 556–557why, one step back
556 expect(screen.getByText('inline: 2')).toBeInTheDocument()
557 expect(screen.getByText('static: 1')).toBeInTheDocument()

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

lines 558–559why, one step back
558
559 act(() => setState({ a: 1, b: 1 }))

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

lines 560–561why, one step back
560 expect(screen.getByText('inline: 4')).toBeInTheDocument()
561 expect(screen.getByText('static: 2')).toBeInTheDocument()

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

lines 562–563why, one step back
562})
563

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 564why, one step back
564it('ensures parent components subscribe before children', () => {

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

line 565why, one step back
565 type State = {

chore(eslint): enable 'testing-library', 'jest-dom', 'vitest' plugins and fix violations (#3321)Wonsuk Choibefore adoption234a948

line 566why, one step back
566 childItems: { [key: string]: { text: string } }

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

lines 567–569why, one step back
567 }
568 type Props = { id: string }569 const useBoundStore = create<State>(() => ({

chore(eslint): enable 'testing-library', 'jest-dom', 'vitest' plugins and fix violations (#3321)Wonsuk Choibefore adoption234a948

line 570why, one step back
570 childItems: {

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

lines 571–578why, one step back
571 '1': { text: 'child 1' },
572 '2': { text: 'child 2' },573 },574 }))575 const api = useBoundStore576577 function changeState() {578 api.setState({

chore(eslint): enable 'testing-library', 'jest-dom', 'vitest' plugins and fix violations (#3321)Wonsuk Choibefore adoption234a948

line 579why, one step back
579 childItems: {

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

lines 580–585why, one step back
580 '3': { text: 'child 3' },
581 },582 })583 }584585 function Child({ id }: Props) {

chore(eslint): enable 'testing-library', 'jest-dom', 'vitest' plugins and fix violations (#3321)Wonsuk Choibefore adoption234a948

line 586why, one step back
586 const text = useBoundStore((s) => s.childItems[id]?.text)

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

lines 587–590why, one step back
587 return <div>{text}</div>
588 }589590 function Parent() {

chore(eslint): enable 'testing-library', 'jest-dom', 'vitest' plugins and fix violations (#3321)Wonsuk Choibefore adoption234a948

line 591why, one step back
591 const childStates = useBoundStore((s) => s.childItems)

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

lines 592–609why, one step back
592 return (
593 <>594 <button onClick={changeState}>change state</button>595 {Object.keys(childStates).map((id) => (596 <Child id={id} key={id} />597 ))}598 </>599 )600 }601602 render(603 <StrictMode>604 <Parent />605 </StrictMode>,606 )607608 fireEvent.click(screen.getByText('change state'))609

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 610why, one step back
610 expect(screen.getByText('child 3')).toBeInTheDocument()

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

lines 611–613why, one step back
611})
612613// https://github.com/pmndrs/zustand/issues/84

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 614why, one step back
614it('ensures the correct subscriber is removed on unmount', () => {

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

lines 615–650why, one step back
615 const useBoundStore = create(() => ({ count: 0 }))
616 const api = useBoundStore617618 function increment() {619 api.setState(({ count }) => ({ count: count + 1 }))620 }621622 function Count() {623 const c = useBoundStore((s) => s.count)624 return <div>count: {c}</div>625 }626627 function CountWithInitialIncrement() {628 useLayoutEffect(increment, [])629 return <Count />630 }631632 function Component() {633 const [Counter, setCounter] = useState(() => CountWithInitialIncrement)634 useLayoutEffect(() => {635 setCounter(() => Count)636 }, [])637 return (638 <>639 <Counter />640 <Count />641 </>642 )643 }644645 render(646 <>647 <Component />648 </>,649 )650

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 651why, one step back
651 expect(screen.getAllByText('count: 1').length).toBe(2)

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

lines 652–654why, one step back
652
653 act(increment)654

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 655why, one step back
655 expect(screen.getAllByText('count: 2').length).toBe(2)

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

lines 656–658why, one step back
656})
657658// https://github.com/pmndrs/zustand/issues/86

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 659why, one step back
659it('ensures a subscriber is not mistakenly overwritten', () => {

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

lines 660–698why, one step back
660 const useBoundStore = create(() => ({ count: 0 }))
661 const { setState } = useBoundStore662663 function Count1() {664 const c = useBoundStore((s) => s.count)665 return <div>count1: {c}</div>666 }667668 function Count2() {669 const c = useBoundStore((s) => s.count)670 return <div>count2: {c}</div>671 }672673 // Add 1st subscriber.674 const { rerender } = render(675 <StrictMode>676 <Count1 />677 </StrictMode>,678 )679680 // Replace 1st subscriber with another.681 rerender(682 <StrictMode>683 <Count2 />684 </StrictMode>,685 )686687 // Add 2 additional subscribers.688 rerender(689 <StrictMode>690 <Count2 />691 <Count1 />692 <Count1 />693 </StrictMode>,694 )695696 // Call all subscribers697 act(() => setState({ count: 1 }))698

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

lines 699–700why, one step back
699 expect(screen.getAllByText('count1: 1').length).toBe(2)
700 expect(screen.getAllByText('count2: 1').length).toBe(1)

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

lines 701–702why, one step back
701})
702

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 703why, one step back
703it('works with non-object state', () => {

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

lines 704–722why, one step back
704 const useCount = create(() => 1)
705 const inc = () => useCount.setState((c) => c + 1)706707 const Counter = () => {708 const count = useCount()709 return (710 <>711 <div>count: {count}</div>712 <button onClick={inc}>button</button>713 </>714 )715 }716717 render(718 <StrictMode>719 <Counter />720 </StrictMode>,721 )722

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 723why, one step back
723 expect(screen.getByText('count: 1')).toBeInTheDocument()

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

lines 724–725why, one step back
724
725 fireEvent.click(screen.getByText('button'))

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 726why, one step back
726 expect(screen.getByText('count: 2')).toBeInTheDocument()

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

lines 727–728why, one step back
727})
728

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 729why, one step back
729it('works with "undefined" state', () => {

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

lines 730–742why, one step back
730 const useUndefined = create(() => undefined)
731732 const Component = () => {733 const str = useUndefined((v) => v || 'undefined')734 return <div>str: {str}</div>735 }736737 render(738 <StrictMode>739 <Component />740 </StrictMode>,741 )742

test: migrate 'findByText' and 'waitFor' to fake timers with 'getByText' (#3324)Wonsuk Choibefore adoption376af78

line 743why, one step back
743 expect(screen.getByText('str: undefined')).toBeInTheDocument()

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

line 744why, one step back
744})