diff --git a/src/lib/viewers/image/ImageViewer.js b/src/lib/viewers/image/ImageViewer.js index 6c3fb262a..14a9762e6 100644 --- a/src/lib/viewers/image/ImageViewer.js +++ b/src/lib/viewers/image/ImageViewer.js @@ -222,18 +222,16 @@ class ImageViewer extends ImageBaseViewer { */ handleZoomEvent({ newScale, type }) { const [width, height] = newScale; - // This method is fired on first render - we don't want toggleAnnotationMode to be called on the first render - const isFirstRender = type === undefined; + const isUserInitiated = type !== undefined; + if (!isUserInitiated) { + return; + } const viewport = this.getViewportDimensions(); // We only set AnnotationMode to be NONE if the image overflows the viewport and the state is not explicit region creation const currentState = this.annotationControlsFSM.getState(); - if ( - !isFirstRender && - currentState === AnnotationState.REGION_TEMP && - (width > viewport.width || height > viewport.height) - ) { + if (currentState === AnnotationState.REGION_TEMP && (width > viewport.width || height > viewport.height)) { this.annotator.toggleAnnotationMode(AnnotationMode.NONE); this.processAnnotationModeChange(this.annotationControlsFSM.transition(AnnotationInput.CANCEL)); } diff --git a/src/lib/viewers/image/__tests__/ImageViewer-test.js b/src/lib/viewers/image/__tests__/ImageViewer-test.js index 17c2be9e0..c80f6da08 100644 --- a/src/lib/viewers/image/__tests__/ImageViewer-test.js +++ b/src/lib/viewers/image/__tests__/ImageViewer-test.js @@ -627,20 +627,27 @@ describe('lib/viewers/image/ImageViewer', () => { jest.spyOn(image, 'processAnnotationModeChange'); }); + test('should not call getViewportDimensions if type is undefined', () => { + const width = 100; + const height = 100; + image.getViewportDimensions = jest.fn(); + + image.handleZoomEvent({ newScale: [width, height], type: undefined }); + + expect(image.getViewportDimensions).not.toHaveBeenCalled(); + }); + test.each` - type | currentState | height | width | should - ${undefined} | ${AnnotationState.REGION} | ${60} | ${60} | ${'image does not overflow the viewport'} - ${undefined} | ${AnnotationState.REGION} | ${110} | ${110} | ${'image does overflow the viewport'} - ${undefined} | ${AnnotationState.REGION_TEMP} | ${60} | ${60} | ${'image does not overflow the viewport'} - ${'in'} | ${AnnotationState.REGION} | ${60} | ${60} | ${'image does not overflow the viewport'} - ${'in'} | ${AnnotationState.REGION} | ${110} | ${110} | ${'image does overflow the viewport'} - ${'in'} | ${AnnotationState.REGION_TEMP} | ${60} | ${60} | ${'image does not overflow the viewport'} + currentState | height | width | should + ${AnnotationState.REGION} | ${110} | ${110} | ${'image does overflow the viewport'} + ${AnnotationState.REGION} | ${60} | ${60} | ${'image does not overflow the viewport'} + ${AnnotationState.REGION_TEMP} | ${60} | ${60} | ${'image does not overflow the viewport'} `( - 'should not call processAnnotationModeChange if $should and currentState is $currentState and type is $type', - ({ currentState, height, type, width }) => { + 'should not call processAnnotationModeChange if $should and currentState is $currentState', + ({ currentState, height, width }) => { image.annotationControlsFSM = new AnnotationControlsFSM(currentState); - image.handleZoomEvent({ newScale: [width, height], type }); + image.handleZoomEvent({ newScale: [width, height], type: 'in' }); expect(image.processAnnotationModeChange).not.toHaveBeenCalled(); }, @@ -649,13 +656,12 @@ describe('lib/viewers/image/ImageViewer', () => { test('should call processAnnotationModeChange and toggleAnnotationMode if image does overflow the viewport and currentState is REGION_TEMP', () => { const width = 110; const height = 110; - const type = 'in'; image.annotator = { toggleAnnotationMode: jest.fn(), }; image.annotationControlsFSM = new AnnotationControlsFSM(AnnotationState.REGION_TEMP); - image.handleZoomEvent({ newScale: [width, height], type }); + image.handleZoomEvent({ newScale: [width, height], type: 'in' }); expect(image.processAnnotationModeChange).toHaveBeenCalled(); expect(image.annotator.toggleAnnotationMode).toHaveBeenCalled();