ts-prime
Version:
A utility library for JavaScript and Typescript.
72 lines (71 loc) • 3.1 kB
JavaScript
import { times } from './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, noop); }).toThrow();
expect(function () { return times(-1000, noop); }).toThrow();
expect(function () { return times(Number.MIN_SAFE_INTEGER, noop); }).toThrow();
});
it('returns empty array', function () {
var noop = function () { return undefined; };
var res = times(0, noop);
expect(res).toEqual([]);
});
it('returns arr with fn result', function () {
var one = function () { return 1; };
expect(times(1, one)).toEqual([1]);
expect(times(5, one)).toEqual([1, 1, 1, 1, 1]);
});
it('passes idx to fn', function () {
var fn = jest.fn();
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(5, idx)).toEqual([0, 1, 2, 3, 4]);
var mul2 = function (idx) { return idx * 2; };
expect(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(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(noop)(0);
expect(res).toEqual([]);
});
it('returns arr with fn result', function () {
var one = function () { return 1; };
var oneTime = 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(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(idx)(5)).toEqual([0, 1, 2, 3, 4]);
var mul2 = function (idx) { return idx * 2; };
expect(times(mul2)(5)).toEqual([0, 2, 4, 6, 8]);
});
});
});