Skip to content

Commit

Permalink
Add additional test case to cover unmount behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Pope committed Apr 23, 2024
1 parent 8476bbf commit 2ab758f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/react-dom/src/__tests__/refs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,4 +770,37 @@ describe('refs return clean up function', () => {
// Original setup hit one more time
expect(setup).toHaveBeenCalledTimes(2);
});

it('calls cleanup function on unmount', async () => {
const container = document.createElement('div');
const cleanUp = jest.fn();
const setup = jest.fn();
const nullHandler = jest.fn();

function _onRefChangeWithCleanup(_ref) {
if (_ref) {
setup(_ref.id);
} else {
nullHandler();
}
return cleanUp;
}

const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<div id="test-div" ref={_onRefChangeWithCleanup} />);
});

expect(setup).toHaveBeenCalledTimes(1);
expect(cleanUp).toHaveBeenCalledTimes(0);
expect(nullHandler).toHaveBeenCalledTimes(0);

root.unmount();

expect(setup).toHaveBeenCalledTimes(1);
// Now cleanup has been called
expect(cleanUp).toHaveBeenCalledTimes(1);
// Ref callback never called with null when cleanup is returned
expect(nullHandler).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 2ab758f

Please sign in to comment.