UNPKG

@rxx/core

Version:

React MVI micro framework.

180 lines (179 loc) 8.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("../utils"); var chai_1 = require("chai"); describe('utils', function () { describe('isDefined', function () { it('should return false if value is undefined or null', function () { chai_1.expect(utils_1.isDefined(null)).to.be.eq(false); chai_1.expect(utils_1.isDefined(undefined)).to.be.eq(false); chai_1.expect(utils_1.isDefined(NaN)).to.be.eq(true); chai_1.expect(utils_1.isDefined(Infinity)).to.be.eq(true); chai_1.expect(utils_1.isDefined(0)).to.be.eq(true); chai_1.expect(utils_1.isDefined([])).to.be.eq(true); chai_1.expect(utils_1.isDefined('')).to.be.eq(true); }); }); describe('isDefined', function () { it('should return true if value is array', function () { chai_1.expect(utils_1.isArray([])).to.be.eq(true); chai_1.expect(utils_1.isArray(new Array())).to.be.eq(true); chai_1.expect(utils_1.isArray({})).to.be.eq(false); chai_1.expect(utils_1.isArray(1)).to.be.eq(false); chai_1.expect(utils_1.isArray(function () { })).to.be.eq(false); }); }); describe('isObject', function () { it('should return true if value is object', function () { chai_1.expect(utils_1.isObject({})).to.be.eq(true); chai_1.expect(utils_1.isObject(new Object())).to.be.eq(true); chai_1.expect(utils_1.isObject([])).to.be.eq(false); chai_1.expect(utils_1.isObject(1)).to.be.eq(false); chai_1.expect(utils_1.isObject(function () { })).to.be.eq(false); }); }); describe('isRegExp', function () { it('should return true if value is RegExp', function () { chai_1.expect(utils_1.isRegExp(/a/)).to.be.eq(true); chai_1.expect(utils_1.isRegExp(new RegExp('a'))).to.be.eq(true); chai_1.expect(utils_1.isRegExp([])).to.be.eq(false); chai_1.expect(utils_1.isRegExp(1)).to.be.eq(false); chai_1.expect(utils_1.isRegExp(function () { })).to.be.eq(false); }); }); describe('assign', function () { it('should merge two object with shallow and return new object.', function () { var a = { a: 1 }; var b = { b: 1 }; var ab = utils_1.assign(a, b); chai_1.expect(ab).to.be.deep.eq({ a: 1, b: 1 }); chai_1.expect(ab).to.be.not.eq(a); chai_1.expect(ab).to.be.not.eq(b); }); it('should overwrite same key property', function () { var a = { a: 1, b: { c: 1 } }; var b = { a: 1, b: { d: 1 } }; var ab = utils_1.assign(a, b); chai_1.expect(ab).to.be.deep.eq({ a: 1, b: { d: 1 } }); chai_1.expect(ab).to.be.not.eq(a); chai_1.expect(ab).to.be.not.eq(b); }); }); describe('extend', function () { it('should extend first arguments by seconds arguments', function () { var a = { a: 1 }; var b = { b: 1 }; var ab = utils_1.extend(a, b); chai_1.expect(ab).to.be.deep.eq({ a: 1, b: 1 }); chai_1.expect(ab).to.be.eq(a); chai_1.expect(ab).to.be.not.eq(b); }); it('should overwrite same key property', function () { var a = { a: 1, b: { c: 1 } }; var b = { a: 1, b: { d: 1 } }; var ab = utils_1.extend(a, b); chai_1.expect(ab).to.be.deep.eq({ a: 1, b: { d: 1 } }); chai_1.expect(ab).to.be.eq(a); chai_1.expect(ab).to.be.not.eq(b); }); }); describe('omit', function () { it('should omit specified key', function () { var a = { a: 1, b: 2, c: 3 }; var ab = utils_1.omit(a, 'c'); chai_1.expect(ab).to.be.deep.eq({ a: 1, b: 2 }); chai_1.expect(ab).to.be.not.eq(a); }); it('should overwrite same key property', function () { var a = { a: 1, b: 2, c: 3 }; var ab = utils_1.omit(a, ['b', 'c']); chai_1.expect(ab).to.be.deep.eq({ a: 1 }); chai_1.expect(ab).to.be.not.eq(a); }); }); describe('forIn', function () { it('should iterator over object with key and value', function () { var a = { a: 1, b: 2, c: 3 }; var b = {}; utils_1.forIn(a, function (v, k) { return b[k] = v; }); chai_1.expect(b).to.be.deep.eq(a); }); }); describe('filter', function () { it('should filter array.', function () { var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = utils_1.filter(a, function (v) { return v % 2 === 0; }); chai_1.expect(b).to.be.deep.eq([2, 4, 6, 8, 10]); }); it('should filter object.', function () { var a = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }; var b = utils_1.filter(a, function (v) { return v % 2 === 0; }); chai_1.expect(b).to.be.deep.eq([2, 4, 6, 8, 10]); }); }); describe('map', function () { it('should map array.', function () { var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = utils_1.map(a, function (v) { return v * 2; }); chai_1.expect(b).to.be.deep.eq([2, 4, 6, 8, 10, 12, 14, 16, 18, 20]); }); it('should map object.', function () { var a = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }; var b = utils_1.map(a, function (v) { return v * 2; }); chai_1.expect(b).to.be.deep.eq([2, 4, 6, 8, 10, 12, 14, 16, 18, 20]); }); }); describe('some', function () { it('should iterator array and return true if one of callback return true.', function () { var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = utils_1.some(a, function (v) { return v === 5; }); var c = utils_1.some(a, function (v) { return v === 11; }); chai_1.expect(b).to.be.eq(true); chai_1.expect(c).to.be.eq(false); }); it('should iterator object and return true if one of callback return true.', function () { var a = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }; var b = utils_1.some(a, function (v) { return v === 5; }); var c = utils_1.some(a, function (v) { return v === 11; }); chai_1.expect(b).to.be.eq(true); chai_1.expect(c).to.be.eq(false); }); }); describe('every', function () { it('should iterator array and return true if all callback return true.', function () { var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = utils_1.every(a, function (v) { return true; }); var c = utils_1.every(a, function (v) { return v === 5; }); chai_1.expect(b).to.be.eq(true); chai_1.expect(c).to.be.eq(false); }); it('should iterator object and return true if all callback return true.', function () { var a = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }; var b = utils_1.every(a, function (v) { return true; }); var c = utils_1.every(a, function (v) { return v === 5; }); chai_1.expect(b).to.be.eq(true); chai_1.expect(c).to.be.eq(false); }); }); describe('mapValues', function () { it('should iterator object and return object that has mapped value', function () { var a = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }; var b = utils_1.mapValues(a, function (v) { return v * 2; }); chai_1.expect(b).to.be.deep.eq({ a: 2, b: 4, c: 6, d: 8, e: 10, f: 12, g: 14, h: 16, i: 18, j: 20 }); }); }); describe('clone', function () { it('should shallow clone array', function () { var a = [1, 2, 3, 4, 5]; var b = utils_1.clone(a); chai_1.expect(b).to.be.deep.eq(a); chai_1.expect(b).to.be.not.eq(a); }); it('should shallow clone object', function () { var a = { a: 1, b: 2, c: 3, d: 4, e: 5 }; var b = utils_1.clone(a); chai_1.expect(b).to.be.deep.eq(a); chai_1.expect(b).to.be.not.eq(a); }); }); });