Skip to content

Fix act() tests by properly unmounting the container #14974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let React;
let ReactDOM;
let ReactTestUtils;
let act;
let container;

jest.useRealTimers();

Expand All @@ -29,6 +30,12 @@ describe('ReactTestUtils.act()', () => {
ReactDOM = require('react-dom');
ReactTestUtils = require('react-dom/test-utils');
act = ReactTestUtils.act;
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
ReactDOM.unmountComponentAtNode(container);
document.body.removeChild(container);
});

describe('sync', () => {
Expand Down Expand Up @@ -66,9 +73,6 @@ describe('ReactTestUtils.act()', () => {
);
}

const container = document.createElement('div');
// attach to body so events works
document.body.appendChild(container);
let calledCounter = 0;
act(() => {
ReactDOM.render(
Expand Down Expand Up @@ -96,8 +100,6 @@ describe('ReactTestUtils.act()', () => {
act(click);
expect(calledCounter).toBe(5);
expect(button.innerHTML).toBe('5');

document.body.removeChild(container);
});

it('should flush effects recursively', () => {
Expand All @@ -111,7 +113,6 @@ describe('ReactTestUtils.act()', () => {
return ctr;
}

const container = document.createElement('div');
act(() => {
ReactDOM.render(<App />, container);
});
Expand All @@ -130,8 +131,6 @@ describe('ReactTestUtils.act()', () => {
</button>
);
}
const container = document.createElement('div');
document.body.appendChild(container);
let button;
act(() => {
ReactDOM.render(<App />, container);
Expand All @@ -142,7 +141,6 @@ describe('ReactTestUtils.act()', () => {
expect(() => setValue(1)).toWarnDev([
'An update to App inside a test was not wrapped in act(...).',
]);
document.body.removeChild(container);
});
describe('fake timers', () => {
beforeEach(() => {
Expand All @@ -162,7 +160,6 @@ describe('ReactTestUtils.act()', () => {
}, []);
return toggle;
}
const container = document.createElement('div');

act(() => {
ReactDOM.render(<App />, container);
Expand All @@ -184,7 +181,6 @@ describe('ReactTestUtils.act()', () => {
}, []);
return toggle;
}
const container = document.createElement('div');

act(() => {
ReactDOM.render(<App />, container);
Expand Down Expand Up @@ -220,6 +216,33 @@ describe('ReactTestUtils.act()', () => {
// all 5 ticks present and accounted for
expect(el.innerHTML).toBe('5');
});
it('flushes immediate re-renders with act', () => {
function App() {
let [ctr, setCtr] = React.useState(0);
React.useEffect(() => {
if (ctr === 0) {
setCtr(1);
}
const timeout = setTimeout(() => setCtr(2), 1000);
return () => clearTimeout(timeout);
});
return ctr;
}

act(() => {
ReactDOM.render(<App />, container);
// Since the effects won't be flushed yet, this does not advance the timer
jest.runAllTimers();
});

expect(container.innerHTML).toBe('1');

act(() => {
jest.runAllTimers();
});

expect(container.innerHTML).toBe('2');
});
});

it('warns if you return a value inside act', () => {
Expand Down