ts-prime
Version:
A utility library for JavaScript and Typescript.
74 lines (73 loc) • 3.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var times_1 = require("./times");
describe('times', function () {
describe('data_first', function () {
it('throws error on invalid idx', function () {
var noop = function () { return undefined; };
expect(function () { return times_1.times(-1, noop); }).toThrow();
expect(function () { return times_1.times(-1000, noop); }).toThrow();
expect(function () { return times_1.times(Number.MIN_SAFE_INTEGER, noop); }).toThrow();
});
it('returns empty array', function () {
var noop = function () { return undefined; };
var res = times_1.times(0, noop);
expect(res).toEqual([]);
});
it('returns arr with fn result', function () {
var one = function () { return 1; };
expect(times_1.times(1, one)).toEqual([1]);
expect(times_1.times(5, one)).toEqual([1, 1, 1, 1, 1]);
});
it('passes idx to fn', function () {
var fn = jest.fn();
times_1.times(5, fn);
expect(fn).toHaveBeenCalledWith(0);
expect(fn).toHaveBeenCalledWith(1);
expect(fn).toHaveBeenCalledWith(2);
expect(fn).toHaveBeenCalledWith(3);
expect(fn).toHaveBeenCalledWith(4);
});
it('returns fn results as arr', function () {
var idx = function (idx) { return idx; };
expect(times_1.times(5, idx)).toEqual([0, 1, 2, 3, 4]);
var mul2 = function (idx) { return idx * 2; };
expect(times_1.times(5, mul2)).toEqual([0, 2, 4, 6, 8]);
});
});
describe('data_last', function () {
it('throws error on invalid idx', function () {
var noop = function () { return undefined; };
var noopTimes = times_1.times(noop);
expect(function () { return noopTimes(-1); }).toThrow();
expect(function () { return noopTimes(-1000); }).toThrow();
expect(function () { return noopTimes(Number.MIN_SAFE_INTEGER); }).toThrow();
});
it('returns empty array', function () {
var noop = function () { return undefined; };
var res = times_1.times(noop)(0);
expect(res).toEqual([]);
});
it('returns arr with fn result', function () {
var one = function () { return 1; };
var oneTime = times_1.times(one);
expect(oneTime(1)).toEqual([1]);
expect(oneTime(5)).toEqual([1, 1, 1, 1, 1]);
});
it('passes idx to fn', function () {
var fn = jest.fn();
times_1.times(fn)(5);
expect(fn).toHaveBeenCalledWith(0);
expect(fn).toHaveBeenCalledWith(1);
expect(fn).toHaveBeenCalledWith(2);
expect(fn).toHaveBeenCalledWith(3);
expect(fn).toHaveBeenCalledWith(4);
});
it('returns fn results as arr', function () {
var idx = function (idx) { return idx; };
expect(times_1.times(idx)(5)).toEqual([0, 1, 2, 3, 4]);
var mul2 = function (idx) { return idx * 2; };
expect(times_1.times(mul2)(5)).toEqual([0, 2, 4, 6, 8]);
});
});
});