Skip to content

Commit

Permalink
[tests] assertLog before act in ReactErrorBoundaries (facebook#28761)
Browse files Browse the repository at this point in the history
Fixes tests blocking facebook#28737
  • Loading branch information
rickhanlonii authored and AndyPengc12 committed Apr 15, 2024
1 parent 84e9db3 commit cf6ee16
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ describe('ReactErrorBoundaries', () => {
});
}).rejects.toThrow('Hello');

Scheduler.unstable_clearLog();
container = document.createElement('div');
root = ReactDOMClient.createRoot(container);
await expect(async () => {
Expand All @@ -592,6 +593,7 @@ describe('ReactErrorBoundaries', () => {
});
}).rejects.toThrow('Hello');

Scheduler.unstable_clearLog();
container = document.createElement('div');
root = ReactDOMClient.createRoot(container);
await expect(async () => {
Expand All @@ -607,28 +609,32 @@ describe('ReactErrorBoundaries', () => {
await act(async () => {
root.render(<BrokenComponentWillUpdate />);
});
Scheduler.unstable_clearLog();
await expect(async () => {
await act(async () => {
root.render(<BrokenComponentWillUpdate />);
});
}).rejects.toThrow('Hello');

Scheduler.unstable_clearLog();
container = document.createElement('div');
root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(<BrokenComponentWillReceiveProps />);
});
Scheduler.unstable_clearLog();
await expect(async () => {
await act(async () => {
root.render(<BrokenComponentWillReceiveProps />);
});
}).rejects.toThrow('Hello');

Scheduler.unstable_clearLog();
container = document.createElement('div');
root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(<BrokenComponentDidUpdate />);
});
Scheduler.unstable_clearLog();
await expect(async () => {
await act(async () => {
root.render(<BrokenComponentDidUpdate />);
Expand All @@ -642,6 +648,7 @@ describe('ReactErrorBoundaries', () => {
await act(async () => {
root.render(<BrokenComponentWillUnmount />);
});
Scheduler.unstable_clearLog();
await expect(async () => {
await act(() => root.unmount());
}).rejects.toThrow('Hello');
Expand All @@ -663,6 +670,15 @@ describe('ReactErrorBoundaries', () => {
root2.render(<BrokenRender />);
});
}).rejects.toThrow('Hello');

assertLog([
'BrokenRender constructor',
'BrokenRender componentWillMount',
'BrokenRender render [!]',
'BrokenRender constructor',
'BrokenRender componentWillMount',
'BrokenRender render [!]',
]);
await act(async () => {
root3.render(
<ErrorBoundary>
Expand All @@ -674,12 +690,15 @@ describe('ReactErrorBoundaries', () => {
expect(container2.firstChild).toBe(null);
expect(container3.firstChild.textContent).toBe('Caught an error: Hello.');

Scheduler.unstable_clearLog();
await act(async () => {
root1.render(<span>After 1</span>);
});
Scheduler.unstable_clearLog();
await act(async () => {
root2.render(<span>After 2</span>);
});
Scheduler.unstable_clearLog();
await act(async () => {
root3.render(<ErrorBoundary forceRetry={true}>After 3</ErrorBoundary>);
});
Expand Down Expand Up @@ -1828,6 +1847,7 @@ describe('ReactErrorBoundaries', () => {
);
});

Scheduler.unstable_clearLog();
await act(async () => {
root.render(
<ErrorBoundary>
Expand Down Expand Up @@ -1879,6 +1899,7 @@ describe('ReactErrorBoundaries', () => {
);
});
expect(container.textContent).toBe('Caught an error: Hello.');
Scheduler.unstable_clearLog();

await act(async () => {
root.render(
Expand All @@ -1888,6 +1909,7 @@ describe('ReactErrorBoundaries', () => {
);
});
expect(container.textContent).toBe('Caught an error: Hello.');
Scheduler.unstable_clearLog();

await act(async () => {
root.render(<div>Other screen</div>);
Expand All @@ -1909,7 +1931,7 @@ describe('ReactErrorBoundaries', () => {
</ErrorBoundary>,
);
});

Scheduler.unstable_clearLog();
await act(async () => {
root.render(<ErrorBoundary />);
});
Expand All @@ -1926,6 +1948,12 @@ describe('ReactErrorBoundaries', () => {
await act(async () => {
root.render(<ErrorBoundary />);
});
assertLog([
'ErrorBoundary constructor',
'ErrorBoundary componentWillMount',
'ErrorBoundary render success',
'ErrorBoundary componentDidMount',
]);
await act(async () => {
root.render(
<ErrorBoundary>
Expand Down Expand Up @@ -1983,6 +2011,7 @@ describe('ReactErrorBoundaries', () => {
expect(container.textContent).not.toContain('Caught an error');

fail = true;
Scheduler.unstable_clearLog();
await act(async () => {
root.render(
<ErrorBoundary>
Expand Down

0 comments on commit cf6ee16

Please sign in to comment.