Skip to content

Commit 88cda26

Browse files
authored
Add test ensuring effects are cleaned up (#4792)
1 parent ee5d7e8 commit 88cda26

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

hooks/test/browser/useState.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,44 @@ describe('useState', () => {
355355
expect(renderSpy).to.be.calledThrice;
356356
});
357357

358+
it('Cancels effect invocations correctly when bailing', () => {
359+
const renderSpy = sinon.spy();
360+
const cleanupSpy = sinon.spy();
361+
const spy = sinon.spy();
362+
let set;
363+
364+
function App() {
365+
const [state, setState] = useState('initial');
366+
set = setState;
367+
368+
renderSpy();
369+
useEffect(() => {
370+
spy();
371+
return () => {
372+
cleanupSpy();
373+
};
374+
});
375+
return <p>{state}</p>;
376+
}
377+
378+
act(() => {
379+
render(<App />, scratch);
380+
});
381+
382+
expect(renderSpy).to.be.calledOnce;
383+
expect(spy).to.be.calledOnce;
384+
expect(cleanupSpy).to.not.be.called;
385+
386+
act(() => {
387+
set('updated');
388+
set('initial');
389+
});
390+
391+
expect(renderSpy).to.be.calledTwice;
392+
expect(spy).to.be.calledOnce;
393+
expect(cleanupSpy).to.not.be.called;
394+
});
395+
358396
// see preactjs/preact#3731
359397
it('respects updates initiated from the parent', () => {
360398
let setChild, setParent;

0 commit comments

Comments
 (0)