Skip to content

Commit

Permalink
fix(zoom): Round scale before comparing against max and min values (#…
Browse files Browse the repository at this point in the history
…1290)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
jstoffan and mergify[bot] authored Nov 6, 2020
1 parent 17eac90 commit 0dc7e65
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/lib/viewers/controls/zoom/ZoomControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function ZoomControls({
onZoomOut,
scale = 1,
}: Props): JSX.Element {
const currentZoom = Math.round(scale * 100);
const currentScale = Math.round((scale + Number.EPSILON) * 100) / 100;
const maxScaleValue = isFinite(maxScale) ? Math.min(maxScale, MAX_SCALE) : MAX_SCALE;
const minScaleValue = isFinite(minScale) ? Math.max(minScale, MIN_SCALE) : MIN_SCALE;

Expand All @@ -31,7 +31,7 @@ export default function ZoomControls({
<button
className="bp-ZoomControls-button"
data-testid="bp-ZoomControls-out"
disabled={scale <= minScaleValue}
disabled={currentScale <= minScaleValue}
onClick={onZoomOut}
title={__('zoom_out')}
type="button"
Expand All @@ -42,11 +42,11 @@ export default function ZoomControls({
className="bp-ZoomControls-current"
data-testid="current-zoom"
title={__('zoom_current_scale')}
>{`${currentZoom}%`}</div>
>{`${Math.round(currentScale * 100)}%`}</div>
<button
className="bp-ZoomControls-button"
data-testid="bp-ZoomControls-in"
disabled={scale >= maxScaleValue}
disabled={currentScale >= maxScaleValue}
onClick={onZoomIn}
title={__('zoom_in')}
type="button"
Expand Down
27 changes: 14 additions & 13 deletions src/lib/viewers/controls/zoom/__tests__/ZoomControls-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,27 @@ describe('ZoomControls', () => {

describe('render', () => {
test.each`
minScale | scale | disabled
${null} | ${1} | ${false}
${0.5} | ${1} | ${false}
${0.5} | ${0.5} | ${true}
${-50} | ${0.1} | ${true}
${-50} | ${0.2} | ${false}
minScale | scale | disabled
${null} | ${1} | ${false}
${0.5} | ${1} | ${false}
${0.5} | ${0.5005} | ${true}
${0.5} | ${0.5} | ${true}
${-50} | ${0.1} | ${true}
${-50} | ${0.2} | ${false}
`('should set disabled for zoom out to $disabled for $scale / $minScale', ({ disabled, minScale, scale }) => {
const wrapper = getWrapper({ minScale, scale });

expect(getZoomOut(wrapper).prop('disabled')).toBe(disabled);
});

test.each`
maxScale | scale | disabled
${null} | ${1} | ${false}
${10} | ${1} | ${false}
${50} | ${10} | ${false}
${50} | ${50} | ${true}
${500} | ${100} | ${true}
${500} | ${99} | ${false}
maxScale | scale | disabled
${null} | ${1} | ${false}
${10} | ${1} | ${false}
${50} | ${49.9999} | ${true}
${50} | ${50} | ${true}
${500} | ${100} | ${true}
${500} | ${99} | ${false}
`('should set disabled for zoom in to $disabled for $scale / $maxScale', ({ disabled, maxScale, scale }) => {
const wrapper = getWrapper({ maxScale, scale });

Expand Down

0 comments on commit 0dc7e65

Please sign in to comment.