Skip to content

Commit

Permalink
Add test for unmounting callback ref passed to useImperativeHandle (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins authored Apr 1, 2023
1 parent 4f2af1c commit e97da39
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion hooks/test/browser/useImperativeHandle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('useImperativeHandle', () => {
expect(() => render(<Comp />, scratch)).to.not.throw();
});

it('should reset ref to null when the component get unmounted', () => {
it('should reset ref object to null when the component get unmounted', () => {
let ref,
createHandleSpy = sinon.spy(() => ({ test: () => 'test' }));

Expand All @@ -198,4 +198,25 @@ describe('useImperativeHandle', () => {
expect(createHandleSpy).to.have.been.calledOnce;
expect(ref.current).to.equal(null);
});

it('should reset ref callback to null when the component get unmounted', () => {
const ref = sinon.spy();
const handle = { test: () => 'test' };
const createHandleSpy = sinon.spy(() => handle);

function Comp() {
useImperativeHandle(ref, createHandleSpy, [1]);
return <p>Test</p>;
}

render(<Comp />, scratch);
expect(createHandleSpy).to.have.been.calledOnce;
expect(ref).to.have.been.calledWith(handle);

ref.resetHistory();

render(<div />, scratch);
expect(createHandleSpy).to.have.been.calledOnce;
expect(ref).to.have.been.calledWith(null);
});
});

0 comments on commit e97da39

Please sign in to comment.