|
| 1 | +import {canvasPath, CanvasKeyframe} from "./animate"; |
| 2 | + |
| 3 | +const genKeyframe = (): CanvasKeyframe => ({ |
| 4 | + duration: 1000 * Math.random(), |
| 5 | + delay: 1000 * Math.random(), |
| 6 | + timingFunction: "linear", |
| 7 | + callback: () => {}, |
| 8 | + blobOptions: { |
| 9 | + extraPoints: Math.floor(10 * Math.random()), |
| 10 | + randomness: Math.floor(10 * Math.random()), |
| 11 | + seed: Math.random(), |
| 12 | + size: 100 + 200 * Math.random(), |
| 13 | + }, |
| 14 | + canvasOptions: { |
| 15 | + offsetX: 100 * Math.random(), |
| 16 | + offsetY: 100 * Math.random(), |
| 17 | + }, |
| 18 | +}); |
| 19 | + |
| 20 | +describe("animate", () => { |
| 21 | + describe("canvasPath", () => { |
| 22 | + describe("transition", () => { |
| 23 | + describe("keyframe", () => { |
| 24 | + // TODO test for errors in the nth keyframe. |
| 25 | + it("should accept minimal generated keyframe", () => { |
| 26 | + const animation = canvasPath(); |
| 27 | + const keyframe = genKeyframe(); |
| 28 | + |
| 29 | + expect(() => animation.transition(keyframe)).not.toThrow(); |
| 30 | + }); |
| 31 | + |
| 32 | + interface TestCase { |
| 33 | + test: string; |
| 34 | + edit: (keyframe: CanvasKeyframe) => void; |
| 35 | + error?: RegExp; |
| 36 | + } |
| 37 | + |
| 38 | + const testCases: Array<TestCase> = [ |
| 39 | + { |
| 40 | + test: "should accept valid duration", |
| 41 | + edit: (keyframe) => (keyframe.duration = 100), |
| 42 | + }, |
| 43 | + { |
| 44 | + test: "should accept zero duration", |
| 45 | + edit: (keyframe) => (keyframe.duration = 0), |
| 46 | + }, |
| 47 | + { |
| 48 | + test: "should reject undefined duration", |
| 49 | + edit: (keyframe) => delete keyframe.duration, |
| 50 | + error: /duration.*number.*undefined/g, |
| 51 | + }, |
| 52 | + { |
| 53 | + test: "should reject negative duration", |
| 54 | + edit: (keyframe) => (keyframe.duration = -10), |
| 55 | + error: /duration.*invalid/g, |
| 56 | + }, |
| 57 | + { |
| 58 | + test: "should reject broken duration", |
| 59 | + edit: (keyframe) => (keyframe.duration = NaN), |
| 60 | + error: /duration.*number.*NaN/g, |
| 61 | + }, |
| 62 | + { |
| 63 | + test: "should reject invalid duration", |
| 64 | + edit: (keyframe) => (keyframe.duration = "123" as any), |
| 65 | + error: /duration.*number.*string/g, |
| 66 | + }, |
| 67 | + { |
| 68 | + test: "should accept valid delay", |
| 69 | + edit: (keyframe) => (keyframe.delay = 200), |
| 70 | + }, |
| 71 | + { |
| 72 | + test: "should accept zero delay", |
| 73 | + edit: (keyframe) => (keyframe.delay = 0), |
| 74 | + }, |
| 75 | + { |
| 76 | + test: "should accept undefined delay", |
| 77 | + edit: (keyframe) => delete keyframe.delay, |
| 78 | + }, |
| 79 | + { |
| 80 | + test: "should reject negative delay", |
| 81 | + edit: (keyframe) => (keyframe.delay = -10), |
| 82 | + error: /delay.*invalid/g, |
| 83 | + }, |
| 84 | + { |
| 85 | + test: "should reject broken delay", |
| 86 | + edit: (keyframe) => (keyframe.delay = NaN), |
| 87 | + error: /delay.*number.*NaN/g, |
| 88 | + }, |
| 89 | + { |
| 90 | + test: "should reject invalid delay", |
| 91 | + edit: (keyframe) => (keyframe.delay = "123" as any), |
| 92 | + error: /delay.*number.*string/g, |
| 93 | + }, |
| 94 | + { |
| 95 | + test: "should accept known timingFunction", |
| 96 | + edit: (keyframe) => (keyframe.timingFunction = "ease"), |
| 97 | + }, |
| 98 | + { |
| 99 | + test: "should accept undefined timingFunction", |
| 100 | + edit: (keyframe) => delete keyframe.timingFunction, |
| 101 | + }, |
| 102 | + { |
| 103 | + test: "should reject invalid timingFunction", |
| 104 | + edit: (keyframe) => (keyframe.timingFunction = (() => 0) as any), |
| 105 | + error: /timingFunction.*string.*function/g, |
| 106 | + }, |
| 107 | + { |
| 108 | + test: "should reject unknown timingFunction", |
| 109 | + edit: (keyframe) => (keyframe.timingFunction = "unknown" as any), |
| 110 | + error: /timingFunction.*not recognized.*unknown/g, |
| 111 | + }, |
| 112 | + { |
| 113 | + test: "should accept valid callback", |
| 114 | + edit: (keyframe) => (keyframe.callback = () => console.log("test")), |
| 115 | + }, |
| 116 | + { |
| 117 | + test: "should accept undefined callback", |
| 118 | + edit: (keyframe) => delete keyframe.callback, |
| 119 | + }, |
| 120 | + { |
| 121 | + test: "should reject invalid callback", |
| 122 | + edit: (keyframe) => (keyframe.callback = {} as any), |
| 123 | + error: /callback.*function.*object/g, |
| 124 | + }, |
| 125 | + // TODO complete blobOptions type tests, should be the same as non-animated. |
| 126 | + { |
| 127 | + test: "should reject undefined blobOptions", |
| 128 | + edit: (keyframe) => delete keyframe.blobOptions, |
| 129 | + error: /blobOptions.*object.*undefined/g, |
| 130 | + }, |
| 131 | + { |
| 132 | + test: "should accept empty canvasOptions", |
| 133 | + edit: (keyframe) => (keyframe.canvasOptions = {}), |
| 134 | + }, |
| 135 | + { |
| 136 | + test: "should accept undefined canvasOptions", |
| 137 | + edit: (keyframe) => delete keyframe.canvasOptions, |
| 138 | + }, |
| 139 | + { |
| 140 | + test: "should accept undefined canvasOptions offsetX", |
| 141 | + edit: (keyframe) => delete keyframe.canvasOptions?.offsetX, |
| 142 | + }, |
| 143 | + { |
| 144 | + test: "should reject broken canvasOptions offsetX", |
| 145 | + edit: (keyframe) => (keyframe.canvasOptions = {offsetX: NaN}), |
| 146 | + error: /canvasOptions.*offsetX.*number.*NaN/g, |
| 147 | + }, |
| 148 | + { |
| 149 | + test: "should accept undefined canvasOptions offsetY", |
| 150 | + edit: (keyframe) => delete keyframe.canvasOptions?.offsetY, |
| 151 | + }, |
| 152 | + { |
| 153 | + test: "should reject broken canvasOptions offsetY", |
| 154 | + edit: (keyframe) => (keyframe.canvasOptions = {offsetY: NaN}), |
| 155 | + error: /canvasOptions.*offsetY.*number.*NaN/g, |
| 156 | + }, |
| 157 | + ]; |
| 158 | + |
| 159 | + for (const testCase of testCases) { |
| 160 | + it(testCase.test, () => { |
| 161 | + const animation = canvasPath(); |
| 162 | + const keyframe = genKeyframe(); |
| 163 | + testCase.edit(keyframe); |
| 164 | + if (testCase.error) { |
| 165 | + expect(() => animation.transition(keyframe)).toThrow(testCase.error); |
| 166 | + } else { |
| 167 | + expect(() => animation.transition(keyframe)).not.toThrow(); |
| 168 | + } |
| 169 | + }); |
| 170 | + } |
| 171 | + }); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments