Skip to content

Commit

Permalink
working tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lunelson committed Dec 4, 2018
1 parent 4cc98b1 commit 4fbf520
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
71 changes: 71 additions & 0 deletions src/__tests__/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import SplitEase from '../split-ease.js';

test('throw range errors, if any ease time < 0', () => {
[
[-0.1],
[-0.1, {}],
[0.3, -0.1],
[0.3, -0.1, {}],
].forEach(args => {
expect(() => {
SplitEase.apply(null, args);
}).toThrowError(RangeError);
});
});

test('throw type errors, when arg types are wrong', () => {
[
[{}],
['pow'],
[{}],
[0.3, 'pow'],
[0.3, 0.3, 'pow'],
[0.3, 0.3, 0.3],
].forEach(args => {
expect(() => {
SplitEase.apply(null, args);
}).toThrowError(TypeError);
});
});

test('console.warn about scaling, when durations exceed 1', () => {

// mocking console:
// https://stackoverflow.com/a/41224462/1204994 (see comments)
const consoleWarn = jest
.spyOn(global.console, 'warn')
.mockImplementation(() => {});

[
// et1, et2
[1.1],
[1.1, {}],
[0.3, 1.1],
[0.3, 1.1, {}],
// eSum
[0.3, 0.8],
[0.3, 0.8, {}],
].forEach((args, i) => {

SplitEase.apply(null, args);
expect(consoleWarn).toHaveBeenCalledTimes(i+1);
});
consoleWarn.mockRestore();
});

// test('catch dummy errors', () => {

// function doRangeError() { throw new RangeError('out of range!') }
// expect(doRangeError).toThrowError(RangeError);
// expect(doRangeError).toThrowError('out of range!');

// function doTypeError() { throw new TypeError('wrong type!') }
// expect(doTypeError).toThrowError(TypeError);
// expect(doTypeError).toThrowError('wrong type!');

// function doConsoleWarn() { console.warn('you have been warned') }
// const consoleWarn = jest.spyOn(console, 'warn');
// doConsoleWarn();
// expect(consoleWarn).toHaveBeenCalled();
// consoleWarn.mockRestore();
// });
30 changes: 27 additions & 3 deletions src/split-ease.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,39 @@ function power(t, b, c, d, p) {
c / -2 * (Math.pow(2 - t, p) - 2) + b;
}

// NOTE: possibly no need to use Math.max when parsing args, it's done on the next line anyway
function isPojo(value) {
if (typeof value !== 'object') { return false; }
if (Object.prototype.toString.call(value) !== '[object Object]') { return false; }
switch (Object.getPrototypeOf(value)) {
case Object.prototype: { return true; }
case null: { return true; }
default: { return false; }
}
}

export default function SplitEase(et1 = 0.5, et2 = Math.max(1 - et1, 0), opts = {}) {

// NOTE: possibly no need to use Math.max when parsing args, it's done on the next line anyway
// adjust re positional args
if (typeof et2 == 'object') { opts = et2; et2 = Math.max(1 - et1, 0); }

et1 = Math.max(et1, 0);
// check lower ranges
if (et1 < 0) throw new RangeError(`SplitEase: ease-in ratio must be > 0; received ${et1}`);
if (et2 < 0) throw new RangeError(`SplitEase: ease-out ratio must be > 0; received ${et2}`);

// check types
if (typeof et1 != 'number') throw new TypeError(`SplitEase: ease-in ratio must be a number; received ${et1}`);
if (typeof et2 != 'number') throw new TypeError(`SplitEase: ease-out ratio must be a number; received ${et2}`);
if (!isPojo(opts)) throw new TypeError('SplitEase: options argument must be a plain object');

// et1 = Math.max(et1, 0);
et2 = Math.max(et2, 0);
const eSum = et1 + et2;

// check upper ranges
if (et1 > 1) console.warn(`SplitEase: ease-in ratio > 1 [${et1}]; ease-in/-out ratios will both be scaled down`);
else if (et2 > 1) console.warn(`SplitEase: ease-out ratio > 1 [${et2}]; ease-in/-out ratios will both be scaled down`);
else if (eSum > 1) console.warn(`SplitEase: total easing ratio > 1 [${eSum}]; ease-in/-out ratios will both be scaled down`);

const eScale = eSum > 1 ? 1 / eSum : 1;
et1 *= eScale;
et2 *= eScale;
Expand Down

0 comments on commit 4fbf520

Please sign in to comment.