Skip to content

Commit 7f38e78

Browse files
React 17 renderHook (#2161)
* adds renderHook override to testing-lib * update renderHook imports in basic packages * update usage in toast * export `act` from @lg-tl * update usage in `hooks` * Updates return value of `useToast.updateToast` * remove `act` warnings * update types in RTLOverrides * Creates `waitForState ` * updates hooks & toast tests * adds changesets * updates dependencies * updates externalDependencies * update testing-lib build script * adds comment for @ts-expect-error * moves globToRegex * adds docs to Exists * Update config.ts * Update hooks.spec.tsx * Update hooks.spec.tsx
1 parent c080ec0 commit 7f38e78

File tree

24 files changed

+254
-97
lines changed

24 files changed

+254
-97
lines changed

.changeset/bright-walls-glow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@lg-tools/validate': patch
3+
---
4+
5+
- Adds `'@leafygreen-ui/testing-lib'` to list of external dependencies.
6+
- Updates handling of external dependencies with glob patterns

.changeset/fast-bananas-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@leafygreen-ui/toast': patch
3+
---
4+
5+
Updates `updateToast` function to consistently return the new toast object

.changeset/healthy-needles-learn.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@leafygreen-ui/leafygreen-provider': patch
3+
'@leafygreen-ui/hooks': patch
4+
'@leafygreen-ui/toast': patch
5+
'@leafygreen-ui/a11y': patch
6+
---
7+
8+
Updates test to import `renderHook` from `@leafygreen-ui/testing-lib`

.changeset/khaki-lies-carry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@leafygreen-ui/testing-lib': minor
3+
---
4+
5+
Exports `waitForState`, a wrapper around `act` that returns the result of the state update callback

.changeset/strange-scissors-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@leafygreen-ui/testing-lib': minor
3+
---
4+
5+
Exports overrides for `renderHook` and `act` that will work in both a React 17 and React 18 test environment

packages/a11y/src/A11y.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
22
import { render } from '@testing-library/react';
3-
import { renderHook } from '@testing-library/react-hooks';
43
import { axe } from 'jest-axe';
54

5+
import { renderHook } from '@leafygreen-ui/testing-lib';
6+
67
import { AriaLabelProps, AriaLabelPropsWithLabel } from './AriaLabelProps';
78
import {
89
prefersReducedMotion,

packages/hooks/src/hooks.spec.tsx

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { act, renderHook } from '@testing-library/react-hooks';
1+
import { waitFor } from '@testing-library/react';
2+
3+
import { act, renderHook } from '@leafygreen-ui/testing-lib';
24

35
import {
46
useEventListener,
@@ -94,7 +96,7 @@ describe('packages/hooks', () => {
9496
describe.skip('useMutationObserver', () => {}); //eslint-disable-line jest/no-disabled-tests
9597

9698
test('useViewportSize responds to updates in window size', async () => {
97-
const { result, waitForNextUpdate } = renderHook(() => useViewportSize());
99+
const { result, rerender } = renderHook(() => useViewportSize());
98100

99101
const mutableWindow: { -readonly [K in keyof Window]: Window[K] } = window;
100102
const initialHeight = 360;
@@ -104,10 +106,11 @@ describe('packages/hooks', () => {
104106
mutableWindow.innerWidth = initialWidth;
105107

106108
window.dispatchEvent(new Event('resize'));
107-
await act(waitForNextUpdate);
108-
109-
expect(result?.current?.height).toBe(initialHeight);
110-
expect(result?.current?.width).toBe(initialWidth);
109+
rerender();
110+
await waitFor(() => {
111+
expect(result?.current?.height).toBe(initialHeight);
112+
expect(result?.current?.width).toBe(initialWidth);
113+
});
111114

112115
const updateHeight = 768;
113116
const updateWidth = 1024;
@@ -116,10 +119,10 @@ describe('packages/hooks', () => {
116119
mutableWindow.innerWidth = updateWidth;
117120

118121
window.dispatchEvent(new Event('resize'));
119-
await act(waitForNextUpdate);
120-
121-
expect(result?.current?.height).toBe(updateHeight);
122-
expect(result?.current?.width).toBe(updateWidth);
122+
await waitFor(() => {
123+
expect(result?.current?.height).toBe(updateHeight);
124+
expect(result?.current?.width).toBe(updateWidth);
125+
});
123126
});
124127

125128
describe('usePoller', () => {
@@ -249,26 +252,29 @@ describe('packages/hooks', () => {
249252
expect(pollHandler).toHaveBeenCalledTimes(0);
250253
});
251254

252-
test('when document is not visible', () => {
255+
test('when document is not visible', async () => {
253256
const pollHandler = jest.fn();
254257

255-
renderHook(() => usePoller(pollHandler));
258+
const { rerender } = renderHook(() => usePoller(pollHandler));
256259

257260
expect(pollHandler).toHaveBeenCalledTimes(1);
258261

259262
mutableDocument.visibilityState = 'hidden';
260263
act(() => {
261264
document.dispatchEvent(new Event('visibilitychange'));
262265
});
263-
264266
jest.advanceTimersByTime(30e3);
265267

266268
expect(pollHandler).toHaveBeenCalledTimes(1);
267269

268270
mutableDocument.visibilityState = 'visible';
271+
269272
act(() => {
270273
document.dispatchEvent(new Event('visibilitychange'));
271274
});
275+
jest.advanceTimersByTime(30e3);
276+
277+
rerender(pollHandler);
272278

273279
// immediate triggers the pollHandler
274280
expect(pollHandler).toHaveBeenCalledTimes(2);
@@ -308,11 +314,11 @@ describe('packages/hooks', () => {
308314
rerender(2020);
309315
expect(result.current).toEqual(42);
310316

311-
rerender();
317+
rerender(123);
312318
expect(result.current).toEqual(2020);
313319

314320
rerender();
315-
expect(result.current).toEqual(2020);
321+
expect(result.current).toEqual(123);
316322
});
317323
});
318324

packages/hooks/src/useControlledValue/useControlledValue.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ChangeEvent } from 'react';
22
import { act } from 'react-test-renderer';
3-
import { renderHook } from '@testing-library/react-hooks';
3+
4+
import { renderHook } from '@leafygreen-ui/testing-lib';
45

56
import { useControlledValue } from './useControlledValue';
67

packages/hooks/src/useDynamicRefs/useDynamicRefs.spec.tsx

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { renderHook } from '@testing-library/react-hooks';
2-
1+
// import { renderHook } from '@testing-library/react-hooks';
32
import { consoleOnce } from '@leafygreen-ui/lib';
3+
import { renderHook } from '@leafygreen-ui/testing-lib';
44

55
import { DynamicRefGetter, useDynamicRefs } from '.';
66

@@ -11,11 +11,13 @@ describe('packages/hooks/useDynamicRefs', () => {
1111
});
1212

1313
test('returns identical getter when rerendered ', () => {
14+
const props = { prefix: 'A' };
1415
const { result, rerender } = renderHook(v => useDynamicRefs(v), {
15-
initialProps: { prefix: 'A' },
16+
initialProps: props,
1617
});
17-
rerender();
18-
expect(result.all[0]).toBe(result.all[1]);
18+
const initialValue = result.current;
19+
rerender(props);
20+
expect(result.current).toStrictEqual(initialValue);
1921
});
2022

2123
test('returns unique getters when called with the same prefix', () => {
@@ -33,11 +35,14 @@ describe('packages/hooks/useDynamicRefs', () => {
3335

3436
test('returns unique getters when re-rendered with a different prefix', () => {
3537
// This is an edge-case, but this is the behavior we want if it happens
38+
const props = { prefix: 'A' };
3639
const { result, rerender } = renderHook(v => useDynamicRefs(v), {
37-
initialProps: { prefix: 'A' },
40+
initialProps: props,
3841
});
39-
rerender({ prefix: 'B' });
40-
expect(result.all[0]).not.toBe(result.all[1]);
42+
const initialValue = result.current;
43+
const newProps = { prefix: 'B' };
44+
rerender(newProps);
45+
expect(result.current).not.toBe(initialValue);
4146
});
4247

4348
describe('ref getter function', () => {
@@ -66,18 +71,20 @@ describe('packages/hooks/useDynamicRefs', () => {
6671
});
6772

6873
test('returns identical refs when called with the same key', () => {
69-
const { result } = renderHook(() => useDynamicRefs({ prefix: 'A' }));
74+
const props = { prefix: 'A' };
75+
const { result } = renderHook(() => useDynamicRefs(props));
7076
const ref1 = result.current('key');
7177
const ref2 = result.current('key');
7278
expect(ref1).toBe(ref2);
7379
});
7480

7581
test('returns identical refs when rerendered', () => {
82+
const props = { prefix: 'A' };
7683
const { result, rerender } = renderHook(v => useDynamicRefs(v), {
77-
initialProps: { prefix: 'A' },
84+
initialProps: props,
7885
});
7986
const ref1 = result.current('key');
80-
rerender();
87+
rerender(props);
8188
const ref2 = result.current('key');
8289
expect(ref1).toBe(ref2);
8390
});

packages/leafygreen-provider/src/PopoverContext/PopoverContext.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { PropsWithChildren } from 'react';
22
import { act, fireEvent, render, waitFor } from '@testing-library/react';
3-
import { renderHook } from '@testing-library/react-hooks';
3+
4+
import { renderHook } from '@leafygreen-ui/testing-lib';
45

56
import { PopoverProvider, type PopoverState, usePopoverContext } from '.';
67

0 commit comments

Comments
 (0)