Skip to content

Commit 1424fc8

Browse files
eps1lonkoba04
andauthored
test: Add React 18 to test matrix (#771)
* Adjust tests for React 18 * Wrap all updates in act() * Run with next in CI * Branch tests for React < 18 and 18 * Don't block releases when React 18 fails (yet) * double onEnter is due to StrictEffects * Apply suggestions from code review Co-authored-by: Toru Kobayashi <[email protected]> Co-authored-by: Toru Kobayashi <[email protected]>
1 parent f5bfc10 commit 1424fc8

8 files changed

+220
-118
lines changed

.github/workflows/ci.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ jobs:
1414
# Otherwise how would we know if a specific React version caused the failure?
1515
fail-fast: false
1616
matrix:
17-
REACT_DIST: [16, 17]
18-
17+
REACT_DIST: [16, 17, next]
18+
# Unstable release channel so let's not block a potential release of `react-transition-group`
19+
continue-on-error: ${{ matrix.REACT_DIST == 'next' }}
1920
steps:
2021
- uses: actions/checkout@v2
2122
- name: Use Node.js 14

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"@semantic-release/npm": "^7.0.5",
8181
"@storybook/addon-actions": "^6.3.4",
8282
"@storybook/react": "^6.3.4",
83-
"@testing-library/react": "^12.1.2",
83+
"@testing-library/react": "alpha",
8484
"@typescript-eslint/eslint-plugin": "^4.26.1",
8585
"astroturf": "^0.10.4",
8686
"babel-eslint": "^10.1.0",

test/CSSTransition-test.js

+88-60
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { render } from './utils';
2+
import { render, waitFor } from './utils';
33

44
import CSSTransition from '../src/CSSTransition';
55
import TransitionGroup from '../src/TransitionGroup';
@@ -36,8 +36,9 @@ describe('CSSTransition', () => {
3636
});
3737

3838
describe('entering', () => {
39-
it('should apply classes at each transition state', (done) => {
39+
it('should apply classes at each transition state', async () => {
4040
let count = 0;
41+
let done = false;
4142
const nodeRef = React.createRef();
4243
const { setProps } = render(
4344
<CSSTransition nodeRef={nodeRef} timeout={10} classNames="test">
@@ -63,12 +64,16 @@ describe('CSSTransition', () => {
6364
onEntered() {
6465
expect(nodeRef.current.className).toEqual('test-enter-done');
6566
expect(count).toEqual(2);
66-
done();
67+
done = true;
6768
},
6869
});
70+
71+
await waitFor(() => {
72+
expect(done).toBe(true);
73+
});
6974
});
7075

71-
it('should apply custom classNames names', (done) => {
76+
it('should apply custom classNames names', async () => {
7277
let count = 0;
7378
const nodeRef = React.createRef();
7479
const { setProps } = render(
@@ -102,15 +107,17 @@ describe('CSSTransition', () => {
102107

103108
onEntered() {
104109
expect(nodeRef.current.className).toEqual('custom-super-done');
105-
expect(count).toEqual(2);
106-
done();
107110
},
108111
});
112+
113+
await waitFor(() => {
114+
expect(count).toEqual(2);
115+
});
109116
});
110117
});
111118

112119
describe('appearing', () => {
113-
it('should apply appear classes at each transition state', (done) => {
120+
it('should apply appear classes at each transition state', async () => {
114121
let count = 0;
115122
const nodeRef = React.createRef();
116123
render(
@@ -137,17 +144,21 @@ describe('CSSTransition', () => {
137144
expect(nodeRef.current.className).toEqual(
138145
'appear-test-appear-done appear-test-enter-done'
139146
);
140-
expect(count).toEqual(2);
141-
done();
142147
}}
143148
>
144149
<div ref={nodeRef} />
145150
</CSSTransition>
146151
);
152+
153+
await waitFor(() => {
154+
expect(count).toEqual(2);
155+
});
147156
});
148157

149-
it('should lose the "*-appear-done" class after leaving and entering again', (done) => {
158+
it('should lose the "*-appear-done" class after leaving and entering again', async () => {
150159
const nodeRef = React.createRef();
160+
let entered = false;
161+
let exited = false;
151162
const { setProps } = render(
152163
<CSSTransition
153164
timeout={10}
@@ -156,31 +167,45 @@ describe('CSSTransition', () => {
156167
in={true}
157168
appear={true}
158169
onEntered={() => {
159-
setProps({
160-
in: false,
161-
onEntered: () => {},
162-
onExited: () => {
163-
expect(nodeRef.current.className).toBe('appear-test-exit-done');
164-
setProps({
165-
in: true,
166-
onEntered: () => {
167-
expect(nodeRef.current.className).toBe(
168-
'appear-test-enter-done'
169-
);
170-
done();
171-
},
172-
});
173-
},
174-
});
170+
entered = true;
175171
}}
176172
>
177173
<div ref={nodeRef} />
178174
</CSSTransition>
179175
);
176+
177+
await waitFor(() => {
178+
expect(entered).toEqual(true);
179+
});
180+
setProps({
181+
in: false,
182+
onEntered: () => {},
183+
onExited: () => {
184+
exited = true;
185+
},
186+
});
187+
188+
await waitFor(() => {
189+
expect(exited).toEqual(true);
190+
});
191+
expect(nodeRef.current.className).toBe('appear-test-exit-done');
192+
entered = false;
193+
setProps({
194+
in: true,
195+
onEntered: () => {
196+
entered = true;
197+
},
198+
});
199+
200+
await waitFor(() => {
201+
expect(entered).toEqual(true);
202+
});
203+
expect(nodeRef.current.className).toBe('appear-test-enter-done');
180204
});
181205

182-
it('should not add undefined when appearDone is not defined', (done) => {
206+
it('should not add undefined when appearDone is not defined', async () => {
183207
const nodeRef = React.createRef();
208+
let done = false;
184209
render(
185210
<CSSTransition
186211
timeout={10}
@@ -195,15 +220,19 @@ describe('CSSTransition', () => {
195220
onEntered={(isAppearing) => {
196221
expect(isAppearing).toEqual(true);
197222
expect(nodeRef.current.className).toEqual('');
198-
done();
223+
done = true;
199224
}}
200225
>
201226
<div ref={nodeRef} />
202227
</CSSTransition>
203228
);
229+
230+
await waitFor(() => {
231+
expect(done).toEqual(true);
232+
});
204233
});
205234

206-
it('should not be appearing in normal enter mode', (done) => {
235+
it('should not be appearing in normal enter mode', async () => {
207236
let count = 0;
208237
const nodeRef = React.createRef();
209238
render(
@@ -237,10 +266,12 @@ describe('CSSTransition', () => {
237266
expect(nodeRef.current.className).toEqual(
238267
'not-appear-test-enter-done'
239268
);
240-
expect(count).toEqual(2);
241-
done();
242269
},
243270
});
271+
272+
await waitFor(() => {
273+
expect(count).toEqual(2);
274+
});
244275
});
245276

246277
it('should not enter the transition states when appear=false', () => {
@@ -269,7 +300,7 @@ describe('CSSTransition', () => {
269300
});
270301

271302
describe('exiting', () => {
272-
it('should apply classes at each transition state', (done) => {
303+
it('should apply classes at each transition state', async () => {
273304
let count = 0;
274305
const nodeRef = React.createRef();
275306
const { setProps } = render(
@@ -295,13 +326,15 @@ describe('CSSTransition', () => {
295326

296327
onExited() {
297328
expect(nodeRef.current.className).toEqual('test-exit-done');
298-
expect(count).toEqual(2);
299-
done();
300329
},
301330
});
331+
332+
await waitFor(() => {
333+
expect(count).toEqual(2);
334+
});
302335
});
303336

304-
it('should apply custom classNames names', (done) => {
337+
it('should apply custom classNames names', async () => {
305338
let count = 0;
306339
const nodeRef = React.createRef();
307340
const { setProps } = render(
@@ -336,13 +369,15 @@ describe('CSSTransition', () => {
336369

337370
onExited() {
338371
expect(nodeRef.current.className).toEqual('custom-super-done');
339-
expect(count).toEqual(2);
340-
done();
341372
},
342373
});
374+
375+
await waitFor(() => {
376+
expect(count).toEqual(2);
377+
});
343378
});
344379

345-
it('should support empty prefix', (done) => {
380+
it('should support empty prefix', async () => {
346381
let count = 0;
347382

348383
const nodeRef = React.createRef();
@@ -367,10 +402,12 @@ describe('CSSTransition', () => {
367402

368403
onExited() {
369404
expect(nodeRef.current.className).toEqual('exit-done');
370-
expect(count).toEqual(2);
371-
done();
372405
},
373406
});
407+
408+
await waitFor(() => {
409+
expect(count).toEqual(2);
410+
});
374411
});
375412
});
376413

@@ -412,20 +449,7 @@ describe('CSSTransition', () => {
412449
<Test direction="down" text="foo" nodeRef={nodeRef.foo} />
413450
);
414451

415-
const rerender = (getProps) =>
416-
new Promise((resolve) =>
417-
setProps({
418-
onEnter: undefined,
419-
onEntering: undefined,
420-
onEntered: undefined,
421-
onExit: undefined,
422-
onExiting: undefined,
423-
onExited: undefined,
424-
...getProps(resolve),
425-
})
426-
);
427-
428-
await rerender((resolve) => ({
452+
setProps({
429453
direction: 'up',
430454
text: 'bar',
431455
nodeRef: nodeRef.bar,
@@ -439,11 +463,14 @@ describe('CSSTransition', () => {
439463
expect(nodeRef.bar.current.className).toEqual(
440464
'up-enter up-enter-active'
441465
);
442-
resolve();
443466
},
444-
}));
467+
});
468+
469+
await waitFor(() => {
470+
expect(count).toEqual(2);
471+
});
445472

446-
await rerender((resolve) => ({
473+
setProps({
447474
direction: 'down',
448475
text: 'foo',
449476
nodeRef: nodeRef.foo,
@@ -457,11 +484,12 @@ describe('CSSTransition', () => {
457484
onEntered() {
458485
count++;
459486
expect(nodeRef.foo.current.className).toEqual('down-enter-done');
460-
resolve();
461487
},
462-
}));
488+
});
463489

464-
expect(count).toEqual(4);
490+
await waitFor(() => {
491+
expect(count).toEqual(4);
492+
});
465493
});
466494
});
467495
});

test/CSSTransitionGroup-test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ describe('CSSTransitionGroup', () => {
218218
ReactDOM.unmountComponentAtNode(container);
219219

220220
// Testing that no exception is thrown here, as the timeout has been cleared.
221-
jest.runAllTimers();
221+
act(() => {
222+
jest.runAllTimers();
223+
});
222224
});
223225

224226
it('should handle unmounted elements properly', () => {

test/SwitchTransition-test.js

+6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ describe('SwitchTransition', () => {
106106
act(() => {
107107
jest.runAllTimers();
108108
});
109+
act(() => {
110+
jest.runAllTimers();
111+
});
109112
expect(log).toEqual([
110113
'exit',
111114
'exiting',
@@ -137,6 +140,9 @@ describe('SwitchTransition', () => {
137140
act(() => {
138141
jest.runAllTimers();
139142
});
143+
act(() => {
144+
jest.runAllTimers();
145+
});
140146
expect(log).toEqual([
141147
'enter',
142148
'entering',

0 commit comments

Comments
 (0)