diff --git a/packages/editor/src/components/autosave-monitor/index.js b/packages/editor/src/components/autosave-monitor/index.js index 2780c40041de63..33637310ab9483 100644 --- a/packages/editor/src/components/autosave-monitor/index.js +++ b/packages/editor/src/components/autosave-monitor/index.js @@ -42,6 +42,11 @@ export class AutosaveMonitor extends Component { return; } + if ( this.props.interval !== prevProps.interval ) { + clearTimeout( this.timerId ); + this.setAutosaveTimer(); + } + if ( ! this.props.isDirty ) { this.needsAutosave = false; return; diff --git a/packages/editor/src/components/autosave-monitor/test/index.js b/packages/editor/src/components/autosave-monitor/test/index.js index 0c8693fa34f580..6fd95c2c2c67c9 100644 --- a/packages/editor/src/components/autosave-monitor/test/index.js +++ b/packages/editor/src/components/autosave-monitor/test/index.js @@ -36,6 +36,12 @@ describe( 'AutosaveMonitor', () => { } ); describe( '#componentDidUpdate()', () => { + it( 'should clear and restart autosave timer when the interval changes', () => { + wrapper.setProps( { interval: 999 } ); + expect( clearTimeout ).toHaveBeenCalled(); + expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 2 ); + } ); + it( 'should set needsAutosave=true when editReference changes', () => { expect( wrapper.instance().needsAutosave ).toBe( false ); wrapper.setProps( { @@ -95,9 +101,9 @@ describe( 'AutosaveMonitor', () => { isAutosaveable: true, interval: 5, } ); - expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 1 ); - wrapper.instance().autosaveTimerHandler(); expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 2 ); + wrapper.instance().autosaveTimerHandler(); + expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 3 ); expect( setTimeout ).lastCalledWith( expect.any( Function ), 5000 ); } ); @@ -106,9 +112,9 @@ describe( 'AutosaveMonitor', () => { isAutosaveable: false, interval: 5, } ); - expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 1 ); - wrapper.instance().autosaveTimerHandler(); expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 2 ); + wrapper.instance().autosaveTimerHandler(); + expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 3 ); expect( setTimeout ).lastCalledWith( expect.any( Function ), 1000 ); } );