Skip to content

Commit

Permalink
feat(discoverability): use guard logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenCodes committed Oct 8, 2020
1 parent 62fc65a commit 5af3073
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
12 changes: 5 additions & 7 deletions src/lib/viewers/image/ImageViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
30 changes: 18 additions & 12 deletions src/lib/viewers/image/__tests__/ImageViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
Expand All @@ -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();
Expand Down

0 comments on commit 5af3073

Please sign in to comment.