Skip to content

🎥 Make videos programmatically with React

add unit tests

Calvin Falterb6fb8080b75a
diff --git a/packages/core/src/test/bezier.test.ts b/packages/core/src/test/bezier.test.ts
new file mode 100644
index 0000000000..87c71cfbe5
--- /dev/null
+++ b/packages/core/src/test/bezier.test.ts
@@ -0,0 +1,58 @@
+import {bezier} from '../bezier';
+
+const identity = (x: any) => {
+ return x;
+}
+
+const assertClose = (a: number, b: number, precision = 3) => {
+ expect(a).toBeCloseTo(b, precision);
+}
+
+const allEquals = (be1: (x: number) => number, be2: (x: number) => number, samples: number) => {
+ for (let i = 0; i <= samples; ++i) {
+ const x = i / samples;
+ assertClose(be1(x), be2(x));
+ }
+}
+
+function repeat(n: number) {
+ return function(f: any) {
+ for (let i = 0; i < n; ++i) {
+ f();
+ }
+ };
+}
+
+test('bezier - should create an object', () => {
+ expect(typeof bezier(0, 0, 1, 1) === 'function').toBe(true);
+});
+
+test('bezier - fail with wrong params', () => {
+ const valuesToTest: [number, number, number, number][] = [
+ [0.5, 0.5, -5, 0.5],
+ [0.5, 0.5, 5, 0.5],
+ [-2, 0.5, 0.5, 0.5],
+ [2, 0.5, 0.5, 0.5],
+ ];
+
+ valuesToTest.forEach((entry) => {
+ expect(() => bezier(entry[0], entry[1], entry[2], entry[3])).toThrow();
+ });
+});
+
+test('bezier - linear curves', () => {
+ allEquals(bezier(0, 0, 1, 1), bezier(1, 1, 0, 0), 100);
+ allEquals(bezier(0, 0, 1, 1), identity, 100);
+})
+
+test('bezier - right value at extremes', () => {
+ repeat(10)(() => {
+ const a = Math.random(),
+ b = 2 * Math.random() - 0.5,
+ c = Math.random(),
+ d = 2 * Math.random() - 0.5;
+ const easing = bezier(a, b, c, d);
+ expect(easing(0)).toBe(0);
+ expect(easing(1)).toBe(1);
+ });
+})
\ No newline at end of file
diff --git a/packages/core/src/test/easing.test.ts b/packages/core/src/test/easing.test.ts
index af829e27e3..bd459d6715 100644
--- a/packages/core/src/test/easing.test.ts
+++ b/packages/core/src/test/easing.test.ts
@@ -68,3 +68,95 @@ describe('Easing Cubic', () => {
numbersToTest.forEach((n) => expect(easingInOut(n)).toBe(inOut(n)));
});
});
+
+describe('Easing Circle', () => {
+ const circle = (n: number) => 1 - Math.sqrt(1 - n * n);
+ const out = (n: number) => 1 - circle(1 - n);
+ const inOut = (n: number) => {
+ if (n >= 0.5) {
+ return 1 - circle((1 - n) * 2) / 2;
+ } else return circle(n * 2) / 2;
+ };
+
+ test('Easing In', () => {
+ const easingIn = Easing.in(Easing.circle);
+ numbersToTest.forEach((n) => expect(easingIn(n)).toBe(circle(n)));
+ });
+
+ test('Easing In Out', () => {
+ const easingOut = Easing.out(Easing.circle);
+ numbersToTest.forEach((n) => expect(easingOut(n)).toBe(out(n)));
+ });
+
+ test('Easing Out', () => {
+ const easingInOut = Easing.inOut(Easing.circle);
+ numbersToTest.forEach((n) => expect(easingInOut(n)).toBe(inOut(n)));
+ });
+});
+
+describe('Easing Exp', () => {
+ const exp = (n: number) => Math.pow(2, 10 * (n - 1));
+ const out = (n: number) => 1 - exp(1 - n);
+ const inOut = (n: number) => {
+ if (n >= 0.5) {
+ return 1 - exp((1 - n) * 2) / 2;
+ } else return exp(n * 2) / 2;
+ };
+
+ test('Easing In', () => {
+ const easingIn = Easing.in(Easing.exp);
+ numbersToTest.forEach((n) => expect(easingIn(n)).toBe(exp(n)));
+ });
+
+ test('Easing In Out', () => {
+ const easingOut = Easing.out(Easing.exp);
+ numbersToTest.forEach((n) => expect(easingOut(n)).toBe(out(n)));
+ });
+
+ test('Easing Out', () => {
+ const easingInOut = Easing.inOut(Easing.exp);
+ numbersToTest.forEach((n) => expect(easingInOut(n)).toBe(inOut(n)));
+ });
+});
+
+describe('Easing Bounce', () => {
+ const bounce = (n: number) => {
+ if (n < 1 / 2.75) {
+ return 7.5625 * n * n;
+ }
+
+ if (n < 2 / 2.75) {
+ const t2_ = n - 1.5 / 2.75;
+ return 7.5625 * t2_ * t2_ + 0.75;
+ }
+
+ if (n < 2.5 / 2.75) {
+ const t2_ = n - 2.25 / 2.75;
+ return 7.5625 * t2_ * t2_ + 0.9375;
+ }
+
+ const t2 = n - 2.625 / 2.75;
+ return 7.5625 * t2 * t2 + 0.984375;
+ };
+ const out = (n: number) => 1 - bounce(1 - n);
+ const inOut = (n: number) => {
+ if (n >= 0.5) {
+ return 1 - bounce((1 - n) * 2) / 2;
+ } else return bounce(n * 2) / 2;
+ };
+
+ test('Easing In', () => {
+ const easingIn = Easing.in(Easing.bounce);
+ numbersToTest.forEach((n) => expect(easingIn(n)).toBe(bounce(n)));
+ });
+
+ test('Easing In Out', () => {
+ const easingOut = Easing.out(Easing.bounce);
+ numbersToTest.forEach((n) => expect(easingOut(n)).toBe(out(n)));
+ });
+
+ test('Easing Out', () => {
+ const easingInOut = Easing.inOut(Easing.bounce);
+ numbersToTest.forEach((n) => expect(easingInOut(n)).toBe(inOut(n)));
+ });
+});
\ No newline at end of file
diff --git a/packages/core/src/test/webpack-caching.test.ts b/packages/core/src/test/webpack-caching.test.ts
new file mode 100644
index 0000000000..97ece85cb9
--- /dev/null
+++ b/packages/core/src/test/webpack-caching.test.ts
@@ -0,0 +1,28 @@
+import {
+ DEFAULT_WEBPACK_CACHE_ENABLED,
+ getWebpackCaching,
+ setWebpackCaching,
+} from '../config/webpack-caching';
+import {expectToThrow} from './expect-to-throw';
+
+test('getWebpackCaching - default value', () => {
+ expect(getWebpackCaching()).toEqual(DEFAULT_WEBPACK_CACHE_ENABLED);
+});
+
+test('webpack caching - setter - valid input', () => {
+ const valuesToTest = [true, false];
+ valuesToTest.forEach((entry) => {
+ setWebpackCaching(entry);
+ expect(getWebpackCaching()).toEqual(entry);
+ });
+});
+
+test('webpack caching - setter - invalid input', () => {
+ const valuesToTest = [undefined, 'true', 'false'];
+ valuesToTest.forEach((entry) => {
+ expectToThrow(() => {
+ // @ts-expect-error
+ setWebpackCaching(entry);
+ }, /Caching flag must be a boolean./);
+ });
+});