{
/>
);
- ReactTestUtils.renderIntoDocument(
-
- {instance1}
- {instance1}
- {instance1}
-
,
- );
-
- expect(log).toHaveBeenCalledTimes(6);
- expect(log).toHaveBeenNthCalledWith(1, null);
- expect(log).toHaveBeenNthCalledWith(2, null);
- expect(log).toHaveBeenNthCalledWith(3, null);
- expect(log).toHaveBeenNthCalledWith(
- 4,
- expect.objectContaining({tagName: 'DIV'}),
- );
- expect(log).toHaveBeenNthCalledWith(
- 5,
- expect.objectContaining({tagName: 'DIV'}),
- );
- expect(log).toHaveBeenNthCalledWith(
- 6,
- expect.objectContaining({tagName: 'DIV'}),
- );
+ const root = ReactDOMClient.createRoot(container);
+ await act(() => {
+ root.render(
+
+ {instance1}
+ {instance1}
+ {instance1}
+
,
+ );
+ });
+
+ assertLog([
+ 'mount undefined',
+ 'mount undefined',
+ 'mount undefined',
+ 'update DIV',
+ 'update DIV',
+ 'update DIV',
+ ]);
});
it('should distinguish between a script placeholder and an actual script tag', () => {
@@ -158,40 +167,39 @@ describe('ReactEmptyComponent', () => {
/>
);
- expect(function () {
- ReactTestUtils.renderIntoDocument(instance1);
+ const root1 = ReactDOMClient.createRoot(container);
+ expect(() => {
+ ReactDOM.flushSync(() => {
+ root1.render(instance1);
+ });
}).not.toThrow();
- expect(function () {
- ReactTestUtils.renderIntoDocument(instance2);
+
+ const container2 = document.createElement('div');
+ const root2 = ReactDOMClient.createRoot(container2);
+ expect(() => {
+ ReactDOM.flushSync(() => {
+ root2.render(instance2);
+ });
}).not.toThrow();
- expect(log).toHaveBeenCalledTimes(4);
- expect(log).toHaveBeenNthCalledWith(1, null);
- expect(log).toHaveBeenNthCalledWith(
- 2,
- expect.objectContaining({tagName: 'SCRIPT'}),
- );
- expect(log).toHaveBeenNthCalledWith(
- 3,
- expect.objectContaining({tagName: 'SCRIPT'}),
- );
- expect(log).toHaveBeenNthCalledWith(4, null);
+ assertLog([
+ 'mount undefined',
+ 'update SCRIPT',
+ 'mount SCRIPT',
+ 'update undefined',
+ ]);
});
it(
'should have findDOMNode return null when multiple layers of composite ' +
'components render to the same nullish placeholder',
() => {
- class GrandChild extends React.Component {
- render() {
- return nullORUndefined;
- }
+ function GrandChild() {
+ return nullORUndefined;
}
- class Child extends React.Component {
- render() {
- return ;
- }
+ function Child() {
+ return ;
}
const instance1 = (
@@ -201,29 +209,32 @@ describe('ReactEmptyComponent', () => {
);
- expect(function () {
- ReactTestUtils.renderIntoDocument(instance1);
+ const root1 = ReactDOMClient.createRoot(container);
+ expect(() => {
+ ReactDOM.flushSync(() => {
+ root1.render(instance1);
+ });
}).not.toThrow();
- expect(function () {
- ReactTestUtils.renderIntoDocument(instance2);
+
+ const container2 = document.createElement('div');
+ const root2 = ReactDOMClient.createRoot(container2);
+ expect(() => {
+ ReactDOM.flushSync(() => {
+ root2.render(instance2);
+ });
}).not.toThrow();
- expect(log).toHaveBeenCalledTimes(4);
- expect(log).toHaveBeenNthCalledWith(
- 1,
- expect.objectContaining({tagName: 'DIV'}),
- );
- expect(log).toHaveBeenNthCalledWith(2, null);
- expect(log).toHaveBeenNthCalledWith(3, null);
- expect(log).toHaveBeenNthCalledWith(
- 4,
- expect.objectContaining({tagName: 'DIV'}),
- );
+ assertLog([
+ 'mount DIV',
+ 'update undefined',
+ 'mount undefined',
+ 'update DIV',
+ ]);
},
);
- it('works when switching components', () => {
- let assertions = 0;
+ it('works when switching components', async () => {
+ let innerRef;
class Inner extends React.Component {
render() {
@@ -234,44 +245,51 @@ describe('ReactEmptyComponent', () => {
// Make sure the DOM node resolves properly even if we're replacing a
// `null` component
expect(ReactDOM.findDOMNode(this)).not.toBe(null);
- assertions++;
}
componentWillUnmount() {
// Even though we're getting replaced by `null`, we haven't been
// replaced yet!
expect(ReactDOM.findDOMNode(this)).not.toBe(null);
- assertions++;
}
}
- class Wrapper extends React.Component {
- render() {
- return this.props.showInner ? : nullORUndefined;
- }
+ function Wrapper({showInner}) {
+ innerRef = React.createRef(null);
+ return showInner ? : nullORUndefined;
}
const el = document.createElement('div');
- let component;
// Render the component...
- component = ReactDOM.render(, el);
- expect(ReactDOM.findDOMNode(component)).not.toBe(null);
+ const root = ReactDOMClient.createRoot(el);
+ await act(() => {
+ root.render();
+ });
+ expect(innerRef.current).not.toBe(null);
// Switch to null...
- component = ReactDOM.render(, el);
- expect(ReactDOM.findDOMNode(component)).toBe(null);
+ await act(() => {
+ root.render();
+ });
+ expect(innerRef.current).toBe(null);
// ...then switch back.
- component = ReactDOM.render(, el);
- expect(ReactDOM.findDOMNode(component)).not.toBe(null);
+ await act(() => {
+ root.render();
+ });
+ expect(innerRef.current).not.toBe(null);
- expect(assertions).toBe(3);
+ expect.assertions(6);
});
- it('can render nullish at the top level', () => {
+ it('can render nullish at the top level', async () => {
const div = document.createElement('div');
- ReactDOM.render(nullORUndefined, div);
+ const root = ReactDOMClient.createRoot(div);
+
+ await act(() => {
+ root.render(nullORUndefined);
+ });
expect(div.innerHTML).toBe('');
});
@@ -308,26 +326,30 @@ describe('ReactEmptyComponent', () => {
}
}
- expect(function () {
- ReactTestUtils.renderIntoDocument();
+ const root = ReactDOMClient.createRoot(container);
+ expect(() => {
+ ReactDOM.flushSync(() => {
+ root.render();
+ });
}).not.toThrow();
});
- it('preserves the dom node during updates', () => {
- class Empty extends React.Component {
- render() {
- return nullORUndefined;
- }
+ it('preserves the dom node during updates', async () => {
+ function Empty() {
+ return nullORUndefined;
}
- const container = document.createElement('div');
-
- ReactDOM.render(, container);
+ const root = ReactDOMClient.createRoot(container);
+ await act(() => {
+ root.render();
+ });
const noscript1 = container.firstChild;
expect(noscript1).toBe(null);
// This update shouldn't create a DOM node
- ReactDOM.render(, container);
+ await act(() => {
+ root.render();
+ });
const noscript2 = container.firstChild;
expect(noscript2).toBe(null);
});
@@ -338,8 +360,11 @@ describe('ReactEmptyComponent', () => {
};
const EmptyForwardRef = React.forwardRef(Empty);
+ const root = ReactDOMClient.createRoot(container);
expect(() => {
- ReactTestUtils.renderIntoDocument();
+ ReactDOM.flushSync(() => {
+ root.render();
+ });
}).not.toThrowError();
});
@@ -349,8 +374,11 @@ describe('ReactEmptyComponent', () => {
};
const EmptyMemo = React.memo(Empty);
+ const root = ReactDOMClient.createRoot(container);
expect(() => {
- ReactTestUtils.renderIntoDocument();
+ ReactDOM.flushSync(() => {
+ root.render();
+ });
}).not.toThrowError();
});
});