UNPKG

@darkobits/formation

Version:
452 lines (358 loc) 13.8 kB
'use strict'; var _ramda = require('ramda'); var _utils = require('./utils'); function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } describe('Utils', function () { describe('assertIsEntry', function () { it('should require its argument to be a [key, value] pair', function () { expect(function () { (0, _utils.assertIsEntry)(null); }).toThrow('Expected [key, value] entry, but got Null'); expect(function () { (0, _utils.assertIsEntry)([]); }).toThrow('Expected [key, value] entry, but got Array'); expect(function () { (0, _utils.assertIsEntry)(['foo', 'bar']); }).not.toThrow(); }); }); describe('isDefined', function () { it('should assert that its argument is not "undefined"', function () { expect((0, _utils.isDefined)('foo')).toBe(true); expect((0, _utils.isDefined)(null)).toBe(true); expect((0, _utils.isDefined)(NaN)).toBe(true); expect((0, _utils.isDefined)(false)).toBe(true); expect((0, _utils.isDefined)(undefined)).toBe(false); }); }); describe('isFunction', function () { it('should assert that its argument is a function', function () { expect((0, _utils.isFunction)(function () {})).toBe(true); expect((0, _utils.isFunction)(Array.isArray)).toBe(true); // Including jest.fn(), which does not pass using R.is. expect((0, _utils.isFunction)(jest.fn())).toBe(true); expect((0, _utils.isFunction)({})).toBe(false); expect((0, _utils.isFunction)(null)).toBe(false); }); }); describe('throwError', function () { it('should throw an error with the provided message', function () { var message = 'foo'; expect(function () { return (0, _utils.throwError)(message); }).toThrow(message); }); }); describe('assertType', function () { var testAssertType = (0, _utils.assertType)('Test', _ramda.__, 'value'); it('should check for type Function', function () { expect(testAssertType(Function, function () {})).toBe(true); expect(testAssertType([Function, String], function () {})).toBe(true); expect(function () { testAssertType(Function, null); }).toThrow('Test expected value to be of type Function, but got Null.'); expect(function () { testAssertType([Function, String], null); }).toThrow('Test expected value to be of type Function or String, but got Null.'); }); it('should check for type Array', function () { expect(testAssertType(Array, [])).toBe(true); expect(testAssertType([Array, String], [])).toBe(true); expect(function () { testAssertType(Array, null); }).toThrow('Test expected value to be of type Array, but got Null.'); expect(function () { testAssertType([Array, String], null); }).toThrow('Test expected value to be of type Array or String, but got Null.'); }); it('should check for type Undefined', function () { expect(testAssertType(undefined, undefined)).toBe(true); expect(testAssertType([undefined, String], undefined)).toBe(true); expect(function () { testAssertType(undefined, 'foo'); }).toThrow('Test expected value to be of type Undefined, but got String.'); expect(function () { testAssertType([undefined, Array], 'foo'); }).toThrow('Test expected value to be of type Undefined or Array, but got String.'); }); it('should check for type Null', function () { expect(testAssertType(null, null)).toBe(true); expect(testAssertType([null, String], null)).toBe(true); expect(function () { testAssertType(null, 'foo'); }).toThrow('Test expected value to be of type Null, but got String.'); expect(function () { testAssertType([null, Array], 'foo'); }).toThrow('Test expected value to be of type Null or Array, but got String.'); }); it('should check for the provided constructor', function () { function Foo() {} var myFoo = new Foo(); var Bar = function Bar() { _classCallCheck(this, Bar); }; var myBar = new Bar(); expect(testAssertType(Foo, myFoo)).toBe(true); expect(testAssertType([Foo, String], myFoo)).toBe(true); expect(testAssertType(Bar, myBar)).toBe(true); expect(testAssertType([Bar, String], myBar)).toBe(true); expect(function () { testAssertType(Foo, 'foo'); }).toThrow('Test expected value to be of type Foo, but got String.'); expect(function () { testAssertType([Foo, Array], 'foo'); }).toThrow('Test expected value to be of type Foo or Array, but got String.'); expect(function () { testAssertType(Bar, 'foo'); }).toThrow('Test expected value to be of type Bar, but got String.'); expect(function () { testAssertType([Bar, Array], 'foo'); }).toThrow('Test expected value to be of type Bar or Array, but got String.'); }); }); describe('capitalizeFirst', function () { it('should capitalize a string', function () { var input = 'foo'; expect((0, _utils.capitalizeFirst)(input)).toBe('Foo'); }); }); describe('lowercaseFirst', function () { it('should convert the first character of a string to lowercase', function () { var input = 'Foo'; expect((0, _utils.lowercaseFirst)(input)).toBe('foo'); }); }); describe('parseFlags', function () { it('should convert a comma-delimited string into an array', function () { var input = 'foo,bar,baz'; var expected = ['$foo', '$bar', '$baz']; expect((0, _utils.parseFlags)(input)).toEqual(expected); }); it('should convert a space-delimited string into an array', function () { var input = 'foo bar baz'; var expected = ['$foo', '$bar', '$baz']; expect((0, _utils.parseFlags)(input)).toEqual(expected); }); it('should convert a comma and space-delimited string into an array', function () { var input = 'foo, bar, baz'; var expected = ['$foo', '$bar', '$baz']; expect((0, _utils.parseFlags)(input)).toEqual(expected); }); }); describe('onReady', function () { // Note: Improve these tests when Jest's timers API sucks less. it('should resolve when the spied value becomes truthy', function (done) { expect.assertions(1); var obj = { foo: false }; (0, _utils.onReady)(obj, 'foo').then(function () { expect(true).toBe(true); done(); }); setTimeout(function () { obj.foo = true; }, 1); }); }); describe('assignToScope', function () { var $scope = {}; var expression = 'vm.foo'; var value = 'foo'; var assignSpy = jest.fn(); var $parse = jest.fn().mockImplementation(function () { return { assign: assignSpy }; }); it('should assign the provided value to the scope', function () { (0, _utils.assignToScope)($parse, {}, value, expression); expect(assignSpy.mock.calls[0]).toEqual([$scope, value]); expect($parse.mock.calls[0][0]).toEqual(expression); }); }); describe('toPairsWith', function () { describe('when passed 2 arguments', function () { it('should generate keys using the provided function', function () { var collection = [{ name: 'foo' }, { name: 'bar' }, { name: 'baz' }]; var keyFn = function keyFn(i) { return i.name; }; var expected = [[collection[0].name, collection[0]], [collection[1].name, collection[1]], [collection[2].name, collection[2]]]; expect((0, _utils.toPairsWith)(keyFn, collection)).toEqual(expected); }); }); describe('when passed 3 arguments', function () { it('should generate keys and values using the provided functions', function () { var collection = [{ name: 'foo', age: 1 }, { name: 'bar', age: 2 }, { name: 'baz', age: 3 }]; var keyFn = function keyFn(i) { return i.name; }; var valueFn = function valueFn(i) { return i.age; }; var expected = [[collection[0].name, collection[0].age], [collection[1].name, collection[1].age], [collection[2].name, collection[2].age]]; expect((0, _utils.toPairsWith)(keyFn, valueFn, collection)).toEqual(expected); }); }); describe('when passed a bad key generation function', function () { it('should throw an error', function () { expect(function () { return (0, _utils.toPairsWith)(false, []); }).toThrow('to be of type "Function"'); }); }); describe('when passed a bad value generation function', function () { it('should throw an error', function () { expect(function () { return (0, _utils.toPairsWith)(function (i) { return i; }, false, []); }).toThrow('to be of type "Function"'); }); }); describe('when passed a bad collection', function () { it('should throw an error', function () { expect(function () { return (0, _utils.toPairsWith)(function (i) { return i; }, function (i) { return i; }, 'foo'); }).toThrow('to be of type "Array"'); }); }); }); describe('mergeEntries', function () { it('should match source entries to destination entries and return triplets', function () { var dest = [[1, 'foo'], [2, 'bar'], [3, 'baz']]; var src = [[1, 'qux']]; var expected = [[1, 'foo', 'qux'], [2, 'bar', undefined], [3, 'baz', undefined]]; expect((0, _utils.mergeEntries)(dest, src)).toEqual(expected); }); it('should duplicate source entries to matching destination entries', function () { var dest = [[1, 'foo'], [1, 'bar'], [2, 'baz']]; var src = [[1, 'qux']]; var expected = [[1, 'foo', 'qux'], [1, 'bar', 'qux'], [2, 'baz', undefined]]; expect((0, _utils.mergeEntries)(dest, src)).toEqual(expected); }); describe('error-handling', function () { it('should throw an error when passed non-arrays', function () { expect(function () { return (0, _utils.mergeEntries)('foo', []); }).toThrow('first argument'); }); it('should throw an error when passed non-arrays', function () { expect(function () { return (0, _utils.mergeEntries)([], 'foo'); }).toThrow('second argument'); }); it('should throw an error when arrays contain non-entries', function () { expect(function () { return (0, _utils.mergeEntries)([1], [[1, 'foo']]); }).toThrow('Expected [key, value] entry'); expect(function () { return (0, _utils.mergeEntries)([[1, 'foo']], [1]); }).toThrow('Expected [key, value] entry'); }); }); }); describe('invoke', function () { it('should invoke the named method on the provided object with provided arguments', function () { // We can't use jest.fn() here, because they fail R.is(Function). var wasInvoked = false; var invokedWith = void 0; var obj = { getName: function getName() { wasInvoked = true; for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } invokedWith = args; } }; var args = ['foo']; _utils.invoke.apply(undefined, ['getName', obj].concat(_toConsumableArray(args))); expect(wasInvoked).toEqual(true); expect(invokedWith).toEqual(args); }); }); describe('greaterScopeId', function () { it('should return the object with the greater scope id', function () { var objA = { $getScope: function $getScope() { return 1; } }; var objB = { getScope: function getScope() { return 2; } }; expect((0, _utils.greaterScopeId)(objA, objB)).toEqual(objB); }); }); describe('mergeDeep', function () { it('should overwrite literals', function () { var objA = { foo: 'bar' }; var objB = { foo: 'baz' }; expect((0, _utils.mergeDeep)(objA, objB)).toEqual({ foo: 'baz' }); }); it('should append arrays', function () { var objA = { foo: [1, 2, 3] }; var objB = { foo: [4, 5, 6] }; expect((0, _utils.mergeDeep)(objA, objB)).toEqual({ foo: [1, 2, 3, 4, 5, 6] }); }); it('should merge objects', function () { var objA = { foo: { bar: { baz: 1 } } }; var objB = { foo: { bar: { bleep: 2 } } }; expect((0, _utils.mergeDeep)(objA, objB)).toEqual({ foo: { bar: { baz: 1, bleep: 2 } } }); }); }); describe('applyToCollection', function () { var item = function item(id) { return { id: id, setName: function setName(name) { this.name = name; } }; }; var collection = [item(1), item(2)]; var data = { 1: 'foo', 2: 'bar' }; (0, _utils.applyToCollection)(collection, (0, _ramda.prop)('id'), 'setName', data); expect(collection[0].name).toEqual('foo'); expect(collection[1].name).toEqual('bar'); }); });