|
| 1 | +import { renderHook } from "@testing-library/react-hooks"; |
| 2 | +import * as useTween from "../src/useTween"; |
| 3 | +import useInterpolations from "../src/useInterpolations"; |
| 4 | + |
| 5 | +let spyUseTween; |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + spyUseTween = jest.spyOn(useTween, "default").mockReturnValue(0.5); |
| 9 | +}); |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + jest.restoreAllMocks(); |
| 13 | +}); |
| 14 | + |
| 15 | +it("should interpolate map values with default parameters", () => { |
| 16 | + const { result } = renderHook(() => |
| 17 | + useInterpolations({ |
| 18 | + left: [0, 100], |
| 19 | + top: [50, 150], |
| 20 | + opacity: [0, 1], |
| 21 | + }) |
| 22 | + ); |
| 23 | + |
| 24 | + expect(result.current.left).toBe(50); |
| 25 | + expect(result.current.top).toBe(100); |
| 26 | + expect(result.current.opacity).toBe(0.5); |
| 27 | + expect(spyUseTween).toHaveBeenCalledTimes(1); |
| 28 | + expect(spyUseTween).toHaveBeenCalledWith("inCirc", 200, 0); |
| 29 | +}); |
| 30 | + |
| 31 | +it("should interpolate map values with custom parameters", () => { |
| 32 | + const { result } = renderHook(() => |
| 33 | + useInterpolations( |
| 34 | + { |
| 35 | + x: [10, 20], |
| 36 | + y: [-5, 5], |
| 37 | + }, |
| 38 | + "outCirc", |
| 39 | + 500, |
| 40 | + 100 |
| 41 | + ) |
| 42 | + ); |
| 43 | + |
| 44 | + expect(result.current.x).toBe(15); |
| 45 | + expect(result.current.y).toBe(0); |
| 46 | + expect(spyUseTween).toHaveBeenCalledTimes(1); |
| 47 | + expect(spyUseTween).toHaveBeenCalledWith("outCirc", 500, 100); |
| 48 | +}); |
| 49 | + |
| 50 | +it("should interpolate at t=0", () => { |
| 51 | + spyUseTween.mockReturnValue(0); |
| 52 | + |
| 53 | + const { result } = renderHook(() => |
| 54 | + useInterpolations({ |
| 55 | + left: [10, 90], |
| 56 | + top: [20, 80], |
| 57 | + }) |
| 58 | + ); |
| 59 | + |
| 60 | + expect(result.current.left).toBe(10); |
| 61 | + expect(result.current.top).toBe(20); |
| 62 | +}); |
| 63 | + |
| 64 | +it("should interpolate at t=1", () => { |
| 65 | + spyUseTween.mockReturnValue(1); |
| 66 | + |
| 67 | + const { result } = renderHook(() => |
| 68 | + useInterpolations({ |
| 69 | + left: [10, 90], |
| 70 | + top: [20, 80], |
| 71 | + }) |
| 72 | + ); |
| 73 | + |
| 74 | + expect(result.current.left).toBe(90); |
| 75 | + expect(result.current.top).toBe(80); |
| 76 | +}); |
| 77 | + |
| 78 | +describe("when invalid map is provided", () => { |
| 79 | + beforeEach(() => { |
| 80 | + jest.spyOn(console, "error").mockImplementation(() => {}); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should log an error when map is not an object", () => { |
| 84 | + const { result } = renderHook(() => |
| 85 | + useInterpolations(null as unknown as Record<string, readonly [number, number]>) |
| 86 | + ); |
| 87 | + |
| 88 | + expect(result.current).toEqual({}); |
| 89 | + expect(console.error).toHaveBeenCalledTimes(1); |
| 90 | + expect(console.error).toHaveBeenCalledWith( |
| 91 | + 'useInterpolations() expected "map" to be an object.' |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should log an error when map value is not a tuple", () => { |
| 96 | + const { result } = renderHook(() => |
| 97 | + useInterpolations({ |
| 98 | + left: [10] as unknown as readonly [number, number], |
| 99 | + }) |
| 100 | + ); |
| 101 | + |
| 102 | + expect(result.current).toEqual({}); |
| 103 | + expect(console.error).toHaveBeenCalledTimes(1); |
| 104 | + expect(console.error).toHaveBeenCalledWith( |
| 105 | + expect.stringContaining('useInterpolations() expected map["left"] to be a [start, end] tuple') |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should log an error when map value contains non-numbers", () => { |
| 110 | + const { result } = renderHook(() => |
| 111 | + useInterpolations({ |
| 112 | + left: ["0", 100] as unknown as readonly [number, number], |
| 113 | + }) |
| 114 | + ); |
| 115 | + |
| 116 | + expect(result.current).toEqual({}); |
| 117 | + expect(console.error).toHaveBeenCalledTimes(1); |
| 118 | + expect(console.error).toHaveBeenCalledWith( |
| 119 | + expect.stringContaining('useInterpolations() expected map["left"] to contain numbers') |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + it("should log an error when map value contains non-finite numbers", () => { |
| 124 | + const { result } = renderHook(() => |
| 125 | + useInterpolations({ |
| 126 | + left: [0, Infinity], |
| 127 | + }) |
| 128 | + ); |
| 129 | + |
| 130 | + expect(result.current).toEqual({}); |
| 131 | + expect(console.error).toHaveBeenCalledTimes(1); |
| 132 | + expect(console.error).toHaveBeenCalledWith( |
| 133 | + expect.stringContaining('useInterpolations() expected map["left"] to contain finite numbers') |
| 134 | + ); |
| 135 | + }); |
| 136 | +}); |
0 commit comments