|
| 1 | +/* eslint-disable no-unused-expressions */ |
| 2 | +import { victoryInterpolator } from "victory-core/lib/victory-animation/util"; |
| 3 | + |
| 4 | +describe("victoryInterpolator", () => { |
| 5 | + it("does not attempt to interpolate identical values", () => { |
| 6 | + // This case fails with the default interpolator, returning *almost* 3. |
| 7 | + expect(victoryInterpolator(3, 3)(0.25920000000000004)).toEqual(3); |
| 8 | + }); |
| 9 | + |
| 10 | + it("does not attempt to interpolate Boolean values", () => { |
| 11 | + // The default interpolator would return 0.5. |
| 12 | + expect(victoryInterpolator(false, true)(0.5)).toEqual(true); |
| 13 | + }); |
| 14 | + |
| 15 | + it("always returns the end value if starting from null", () => { |
| 16 | + const interpolator = victoryInterpolator(null, 5); |
| 17 | + expect(interpolator(0)).toEqual(5); |
| 18 | + expect(interpolator(0.49)).toEqual(5); |
| 19 | + expect(interpolator(0.5)).toEqual(5); |
| 20 | + expect(interpolator(1)).toEqual(5); |
| 21 | + }); |
| 22 | + |
| 23 | + it("always returns the end value if ending on null", () => { |
| 24 | + const interpolator = victoryInterpolator(5, null); |
| 25 | + expect(interpolator(0)).toBeNull(); |
| 26 | + expect(interpolator(0.49)).toBeNull(); |
| 27 | + expect(interpolator(0.5)).toBeNull(); |
| 28 | + expect(interpolator(1)).toBeNull(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("always returns the end value if starting from undefined", () => { |
| 32 | + const interpolator = victoryInterpolator(undefined, 5); |
| 33 | + expect(interpolator(0)).toEqual(5); |
| 34 | + expect(interpolator(0.49)).toEqual(5); |
| 35 | + expect(interpolator(0.5)).toEqual(5); |
| 36 | + expect(interpolator(1)).toEqual(5); |
| 37 | + }); |
| 38 | + |
| 39 | + it("always returns the end value if ending on undefined", () => { |
| 40 | + const interpolator = victoryInterpolator(5, undefined); |
| 41 | + expect(interpolator(0)).toBeUndefined(); |
| 42 | + expect(interpolator(0.49)).toBeUndefined(); |
| 43 | + expect(interpolator(0.5)).toBeUndefined(); |
| 44 | + expect(interpolator(1)).toBeUndefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("interpolates functions", () => { |
| 48 | + const fromFn = () => 5; |
| 49 | + const toFn = () => 10; |
| 50 | + const interpolator = victoryInterpolator(fromFn, toFn); |
| 51 | + const halfwayFn = interpolator(0.5); |
| 52 | + expect(halfwayFn).toBeInstanceOf(Function); |
| 53 | + expect(halfwayFn()).toEqual(7.5); |
| 54 | + }); |
| 55 | + |
| 56 | + it("interpolates string values", () => { |
| 57 | + // From https://github.com/d3/d3-interpolate/blob/main/test/value-test.js#L5-L7 |
| 58 | + const interpolator = victoryInterpolator("foo", "bar"); |
| 59 | + expect(interpolator(0.5)).toEqual("bar"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("interpolates color values", () => { |
| 63 | + // From https://github.com/d3/d3-interpolate/blob/main/test/value-test.js#L15 |
| 64 | + const interpolator = victoryInterpolator("red", "blue"); |
| 65 | + expect(interpolator(0.5)).toEqual("rgb(128, 0, 128)"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("interpolates object values", () => { |
| 69 | + // From https://github.com/d3/d3-interpolate/blob/main/test/value-test.js#L44 |
| 70 | + const interpolator = victoryInterpolator( |
| 71 | + { color: "red" }, |
| 72 | + { color: "blue" } |
| 73 | + ); |
| 74 | + expect(interpolator(0.5)).toEqual({ color: "rgb(128, 0, 128)" }); |
| 75 | + }); |
| 76 | +}); |
0 commit comments