-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix zoom out of chart with cursor outside chart container (#2893)
- Loading branch information
Showing
3 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"victory-zoom-container": patch | ||
--- | ||
|
||
Fix: #2761 zoom out zooming in if cursor outside chart container |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { RawZoomHelpers } from "./zoom-helpers"; | ||
|
||
describe("RawZoomHelpers.getMinimumDomain", () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
it("should be calculating the minimum domain", () => { | ||
jest.spyOn(RawZoomHelpers, "getDomain").mockImplementation(() => ({ | ||
x: [0, 100], | ||
})); | ||
expect( | ||
RawZoomHelpers.getMinimumDomain(30, { minimumZoom: true }, "x"), | ||
).toStrictEqual([29.95, 30.05]); | ||
}); | ||
}); | ||
|
||
describe("RawZoomHelpers.getScaledDomain", () => { | ||
it("should scale the domain correctly with a zoom in factor", () => { | ||
expectToBeCloseToArray( | ||
RawZoomHelpers.getScaledDomain([0, 100], 0.9, 0.5), | ||
[5, 95], | ||
); | ||
}); | ||
it("should scale the domain correctly with a zoom out factor", () => { | ||
expectToBeCloseToArray( | ||
RawZoomHelpers.getScaledDomain([0, 100], 1.1, 0.5), | ||
[-5, 105], | ||
); | ||
}); | ||
}); | ||
|
||
describe("RawZoomHelpers.scale", () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("should get the correct domain", () => { | ||
jest.spyOn(RawZoomHelpers, "getDomain").mockImplementation(() => ({ | ||
x: [0, 100], | ||
})); | ||
jest.spyOn(RawZoomHelpers, "getScalePercent").mockImplementation(() => 0.5); | ||
jest | ||
.spyOn(RawZoomHelpers, "getMinimumDomain") | ||
.mockImplementation(() => [29.955, 30.045]); | ||
|
||
expectToBeCloseToArray( | ||
RawZoomHelpers.scale([0, 100], { deltaY: -1 }, {}, "x"), | ||
[0.166, 99.833], | ||
); | ||
}); | ||
|
||
it("should't change the domain when zooming out with max zoom out", () => { | ||
jest.spyOn(RawZoomHelpers, "getDomain").mockImplementation(() => ({ | ||
x: [0, 100], | ||
})); | ||
jest.spyOn(RawZoomHelpers, "getScalePercent").mockImplementation(() => 0.1); | ||
jest | ||
.spyOn(RawZoomHelpers, "getMinimumDomain") | ||
.mockImplementation(() => [29.955, 30.045]); | ||
|
||
expectToBeCloseToArray( | ||
RawZoomHelpers.scale([0, 100], { deltaY: 1 }, {}, "x"), | ||
[0, 100], | ||
); | ||
}); | ||
|
||
it("should't change the domain when zooming out with max zoom out with the cursor outside the container boundary", () => { | ||
jest.spyOn(RawZoomHelpers, "getDomain").mockImplementation(() => ({ | ||
x: [0, 100], | ||
})); | ||
jest | ||
.spyOn(RawZoomHelpers, "getScalePercent") | ||
.mockImplementation(() => -0.1); | ||
jest | ||
.spyOn(RawZoomHelpers, "getMinimumDomain") | ||
.mockImplementation(() => [29.955, 30.045]); | ||
|
||
expectToBeCloseToArray( | ||
RawZoomHelpers.scale([0, 100], { deltaY: 1 }, {}, "x"), | ||
[0, 100], | ||
); | ||
}); | ||
}); | ||
|
||
function expectToBeCloseToArray(actual, expected) { | ||
expect(actual.length).toBe(expected.length); | ||
actual.forEach((x, i) => expect(x).toBeCloseTo(expected[i])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters