13
let React;
14
let ReactDOM;
15
let ReactDOMClient;
16
-let act;
16
17
describe('ReactES6Class', () => {
18
let container;
30
React = require('react');
31
ReactDOM = require('react-dom');
32
ReactDOMClient = require('react-dom/client');
34
- act = require('jest-react').act;
33
container = document.createElement('div');
34
root = ReactDOMClient.createRoot(container);
35
attachedListener = null;
47
});
48
49
function test(element, expectedTag, expectedClassName) {
52
- act(() => root.render(element));
50
+ ReactDOM.flushSync(() => root.render(element));
51
expect(container.firstChild).not.toBeNull();
52
expect(container.firstChild.tagName).toBe(expectedTag);
53
expect(container.firstChild.className).toBe(expectedClassName);
61
it('throws if no render function is defined', () => {
62
class Foo extends React.Component {}
63
expect(() => {
66
- expect(() => act(() => root.render(<Foo />))).toThrow();
64
+ expect(() => ReactDOM.flushSync(() => root.render(<Foo />))).toThrow();
65
}).toErrorDev([
66
// A failed component renders four times in DEV in concurrent mode
67
'Warning: Foo(...): No `render` method found on the returned component ' +
116
}
117
const ref = React.createRef();
118
test(<Foo initialValue="foo" ref={ref} />, 'DIV', 'foo');
121
- act(() => ref.current.changeState());
119
+ ReactDOM.flushSync(() => ref.current.changeState());
120
test(<Foo />, 'SPAN', 'bar');
121
});
122
146
}
147
}
148
expect(() => {
151
- act(() => root.render(<Foo foo="foo" />));
149
+ ReactDOM.flushSync(() => root.render(<Foo foo="foo" />));
150
}).toErrorDev(
151
'Foo: getDerivedStateFromProps() is defined as an instance method ' +
152
'and will be ignored. Instead, declare it as a static method.',
163
}
164
}
165
expect(() => {
168
- act(() => root.render(<Foo foo="foo" />));
166
+ ReactDOM.flushSync(() => root.render(<Foo foo="foo" />));
167
}).toErrorDev(
168
'Foo: getDerivedStateFromError() is defined as an instance method ' +
169
'and will be ignored. Instead, declare it as a static method.',
178
}
179
}
180
expect(() => {
183
- act(() => root.render(<Foo foo="foo" />));
181
+ ReactDOM.flushSync(() => root.render(<Foo foo="foo" />));
182
}).toErrorDev(
183
'Foo: getSnapshotBeforeUpdate() is defined as a static method ' +
184
'and will be ignored. Instead, declare it as an instance method.',
198
}
199
}
200
expect(() => {
203
- act(() => root.render(<Foo foo="foo" />));
201
+ ReactDOM.flushSync(() => root.render(<Foo foo="foo" />));
202
}).toErrorDev(
203
'`Foo` uses `getDerivedStateFromProps` but its initial state is ' +
204
'undefined. This is not recommended. Instead, define the initial state by ' +
345
}
346
test(<Foo initialValue="foo" />, 'DIV', 'foo');
347
350
- act(() => attachedListener());
348
+ ReactDOM.flushSync(() => attachedListener());
349
expect(renderedName).toBe('bar');
350
});
351
386
}
387
}
388
test(<Foo initialValue="foo" />, 'DIV', 'foo');
391
- act(() => attachedListener());
389
+ ReactDOM.flushSync(() => attachedListener());
390
expect(renderedName).toBe('bar');
391
});
392
435
'did-update', freeze({value: 'foo'}), {},
436
]);
437
lifeCycles = []; // reset
440
- act(() => root.unmount());
438
+ ReactDOM.flushSync(() => root.unmount());
439
expect(lifeCycles).toEqual(['will-unmount']);
440
});
441