UNPKG

@aricma/itemids

Version:

Be faster in creating and updating react state, with the ItemIds object.

80 lines (74 loc) 3.92 kB
"use strict"; var _staticMethods = require("./staticMethods"); var _itemIds = require("../itemIds"); var ERRORS = _interopRequireWildcard(require("../errors")); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } describe("Static Methods", () => { describe("isItemIds", () => { test("does return false per default", () => { expect((0, _staticMethods.isItemIds)()).toBeFalsy(); }); test("returns false if given object is No Array", () => { expect((0, _staticMethods.isItemIds)({ name: "ItemIds" })).toBeFalsy(); }); test("returns true if given object has the key \"name\" with value \"ItemIds\"", () => { expect((0, _staticMethods.isItemIds)((0, _itemIds.ItemIds)())).toBeTruthy(); }); }); describe("isItemId", () => { test("returns false per default", () => { expect((0, _staticMethods.isItemId)()).toBeFalsy(); }); test("is true if given were string or number", () => { expect((0, _staticMethods.isItemId)("id")).toBeTruthy(); expect((0, _staticMethods.isItemId)(1)).toBeTruthy(); }); test("is false if neither string nor number were given", () => { expect((0, _staticMethods.isItemId)(true)).toBeFalsy(); expect((0, _staticMethods.isItemId)([])).toBeFalsy(); expect((0, _staticMethods.isItemId)({})).toBeFalsy(); expect((0, _staticMethods.isItemId)(NaN)).toBeFalsy(); }); }); describe("isItemIdList", () => { test("returns false per default", () => { expect((0, _staticMethods.isItemIdList)()).toBeFalsy(); }); test("is true if given was an array of ids", () => { expect((0, _staticMethods.isItemIdList)([])).toBeTruthy(); expect((0, _staticMethods.isItemIdList)([1, 2, 3])).toBeTruthy(); expect((0, _staticMethods.isItemIdList)([1, "string", 3])).toBeTruthy(); }); test("is false if no array of ids was given", () => { expect((0, _staticMethods.isItemIdList)({})).toBeFalsy(); expect((0, _staticMethods.isItemIdList)([{}])).toBeFalsy(); expect((0, _staticMethods.isItemIdList)([1, true])).toBeFalsy(); }); }); describe("unify", () => { test("throws if no ItemIdList was given", () => { expect(() => (0, _staticMethods.unify)({})).toThrow(ERRORS.staticMethods.unify.gotNoItemIdList); }); test("returns an empty array per default", () => { expect((0, _staticMethods.unify)()).toEqual([]); }); test("is removing all duplicates", () => { expect((0, _staticMethods.unify)([1, 2, 3, 2])).toEqual([1, 2, 3]); expect((0, _staticMethods.unify)(["1", "2", "3", "2"])).toEqual(["1", "2", "3"]); }); test("can handle ItemIds objects", () => { const itemIds = (0, _itemIds.ItemIds)([1, 2, 3, 1]); const result = (0, _staticMethods.unify)(itemIds); expect([...result] /*get the values*/ ).toEqual([1, 2, 3]); }); test("is mutating", () => { const itemIds = [1, 2, 3, 1]; expect((0, _staticMethods.unify)(itemIds)).toBe(itemIds); }); }); });