diff --git a/compat/test/browser/suspense.test.js b/compat/test/browser/suspense.test.js index a7c18c03d28..af94589da41 100644 --- a/compat/test/browser/suspense.test.js +++ b/compat/test/browser/suspense.test.js @@ -224,7 +224,7 @@ describe('suspense', () => { }); }); - it('should not call lifecycle methods of an initially suspending component', () => { + it('should properly call lifecycle methods of an initially suspending component', () => { /** @type {() => Promise} */ let resolve; let resolved = false; @@ -243,15 +243,16 @@ describe('suspense', () => { } return
Lifecycle
; } - componentWillMount() {} componentDidMount() {} + componentDidUpdate() {} componentWillUnmount() {} } const lifecycles = LifecycleSuspender.prototype; sinon.spy(lifecycles, 'componentWillMount'); sinon.spy(lifecycles, 'componentDidMount'); + sinon.spy(lifecycles, 'componentDidUpdate'); sinon.spy(lifecycles, 'componentWillUnmount'); render( @@ -264,6 +265,7 @@ describe('suspense', () => { expect(scratch.innerHTML).to.eql(``); expect(lifecycles.componentWillMount).to.have.been.calledOnce; expect(lifecycles.componentDidMount).to.not.have.been.called; + expect(lifecycles.componentDidUpdate).to.not.have.been.called; expect(lifecycles.componentWillUnmount).to.not.have.been.called; rerender(); @@ -271,6 +273,7 @@ describe('suspense', () => { expect(scratch.innerHTML).to.eql(`
Suspended...
`); expect(lifecycles.componentWillMount).to.have.been.calledOnce; expect(lifecycles.componentDidMount).to.not.have.been.called; + expect(lifecycles.componentDidUpdate).to.not.have.been.called; expect(lifecycles.componentWillUnmount).to.not.have.been.called; return resolve().then(() => { @@ -279,6 +282,8 @@ describe('suspense', () => { expect(lifecycles.componentWillMount).to.have.been.calledOnce; expect(lifecycles.componentDidMount).to.have.been.calledOnce; + // TODO: This is unexpected. See TODO in next test regarding this and preactjs/preact#2098 + expect(lifecycles.componentDidUpdate).to.have.been.calledOnce; expect(lifecycles.componentWillUnmount).to.not.have.been.called; }); }); @@ -377,13 +382,15 @@ describe('suspense', () => { } componentWillMount() {} componentDidMount() {} + componentDidUpdate() {} componentWillUnmount() {} } - const lifecyles = LifecycleLogger.prototype; - sinon.spy(lifecyles, 'componentWillMount'); - sinon.spy(lifecyles, 'componentDidMount'); - sinon.spy(lifecyles, 'componentWillUnmount'); + const lifecycles = LifecycleLogger.prototype; + sinon.spy(lifecycles, 'componentWillMount'); + sinon.spy(lifecycles, 'componentDidMount'); + sinon.spy(lifecycles, 'componentDidUpdate'); + sinon.spy(lifecycles, 'componentWillUnmount'); const [Suspender, suspend] = createSuspender(() =>
Suspense
); @@ -396,18 +403,20 @@ describe('suspense', () => { ); expect(scratch.innerHTML).to.eql(`
Suspense
Lifecycle
`); - expect(lifecyles.componentWillMount).to.have.been.calledOnce; - expect(lifecyles.componentDidMount).to.have.been.calledOnce; - expect(lifecyles.componentWillUnmount).to.not.have.been.called; + expect(lifecycles.componentWillMount).to.have.been.calledOnce; + expect(lifecycles.componentDidMount).to.have.been.calledOnce; + expect(lifecycles.componentDidUpdate).to.not.have.been.called; + expect(lifecycles.componentWillUnmount).to.not.have.been.called; const [resolve] = suspend(); rerender(); expect(scratch.innerHTML).to.eql(`
Suspended...
`); - expect(lifecyles.componentWillMount).to.have.been.calledOnce; - expect(lifecyles.componentDidMount).to.have.been.calledOnce; - expect(lifecyles.componentWillUnmount).to.not.have.been.called; + expect(lifecycles.componentWillMount).to.have.been.calledOnce; + expect(lifecycles.componentDidMount).to.have.been.calledOnce; + expect(lifecycles.componentDidUpdate).to.not.have.been.called; + expect(lifecycles.componentWillUnmount).to.not.have.been.called; return resolve(() =>
Suspense 2
).then(() => { rerender(); @@ -415,9 +424,10 @@ describe('suspense', () => { `
Suspense 2
Lifecycle
` ); - expect(lifecyles.componentWillMount).to.have.been.calledOnce; - expect(lifecyles.componentDidMount).to.have.been.calledOnce; - expect(lifecyles.componentWillUnmount).to.not.have.been.called; + expect(lifecycles.componentWillMount).to.have.been.calledOnce; + expect(lifecycles.componentDidMount).to.have.been.calledOnce; + expect(lifecycles.componentDidUpdate).to.not.have.been.called; + expect(lifecycles.componentWillUnmount).to.not.have.been.called; }); }); @@ -431,18 +441,10 @@ describe('suspense', () => { componentWillUnmount() {} } - const componentWillMount = sinon.spy( - LifecycleLogger.prototype, - 'componentWillMount' - ); - const componentDidMount = sinon.spy( - LifecycleLogger.prototype, - 'componentDidMount' - ); - const componentWillUnmount = sinon.spy( - LifecycleLogger.prototype, - 'componentWillUnmount' - ); + const lifecycles = LifecycleLogger.prototype; + sinon.spy(lifecycles, 'componentWillMount'); + sinon.spy(lifecycles, 'componentDidMount'); + sinon.spy(lifecycles, 'componentWillUnmount'); const [Suspender, suspend] = createSuspender(() =>
Suspense
); @@ -454,26 +456,26 @@ describe('suspense', () => { ); expect(scratch.innerHTML).to.eql(`
Suspense
`); - expect(componentWillMount).to.not.have.been.called; - expect(componentDidMount).to.not.have.been.called; - expect(componentWillUnmount).to.not.have.been.called; + expect(lifecycles.componentWillMount).to.not.have.been.called; + expect(lifecycles.componentDidMount).to.not.have.been.called; + expect(lifecycles.componentWillUnmount).to.not.have.been.called; const [resolve] = suspend(); rerender(); expect(scratch.innerHTML).to.eql(`
Lifecycle
`); - expect(componentWillMount).to.have.been.calledOnce; - expect(componentDidMount).to.have.been.calledOnce; - expect(componentWillUnmount).to.not.have.been.called; + expect(lifecycles.componentWillMount).to.have.been.calledOnce; + expect(lifecycles.componentDidMount).to.have.been.calledOnce; + expect(lifecycles.componentWillUnmount).to.not.have.been.called; return resolve(() =>
Suspense 2
).then(() => { rerender(); expect(scratch.innerHTML).to.eql(`
Suspense 2
`); - expect(componentWillMount).to.have.been.calledOnce; - expect(componentDidMount).to.have.been.calledOnce; - expect(componentWillUnmount).to.have.been.calledOnce; + expect(lifecycles.componentWillMount).to.have.been.calledOnce; + expect(lifecycles.componentDidMount).to.have.been.calledOnce; + expect(lifecycles.componentWillUnmount).to.have.been.calledOnce; }); });