import * as _ from "lodash"; import { omitFunctions, isEqualWithoutFunctions, wasShuffled, filterNil, iterateMiddleOut, } from "../utilities/collections"; const obj = { id: 1, uuid: "abc", identify: () => { return "abc"; }, }; const objWithoutFunctions = { id: 1, uuid: "abc", }; const objWithDifferentFunction = { id: 1, uuid: "abc", identify: () => { return "---"; }, }; const objB = { id: 2, uuid: "def", identify: () => { return "def"; }, }; const objC = { id: 1, uuid: "abc", identify: () => { return "abc"; }, list: [ { sublist: ["a"], } ], }; const objCDeeplyDifferent = { id: 1, uuid: "abc", identify: () => { return "abc"; }, list: [ { sublist: ["b"], } ], }; const arr = [ { id: 1, }, { id: 2, }, ]; const arrWithNils = [ undefined, { id: 1, }, null, { id: 2, }, ]; const arrUpdated = [ { id: 1, }, { id: 2, newField: "foo", }, ]; const arrShuffled = [ { id: 2, }, { id: 1, }, ]; const arrB = [ { id: 3, }, ]; describe("omitFunction", () => { test("does return only non-functions", () => { expect(omitFunctions(obj)).toEqual(_.omit(obj, 'identify')); }); }); describe("isEqualWithoutFunctions", () => { test("does find objects equal with lacking function", () => { expect(isEqualWithoutFunctions(obj, objWithoutFunctions)).toEqual(true); }); test("does find objects equal with different function", () => { expect(isEqualWithoutFunctions(obj, objWithDifferentFunction)).toEqual(true); }); test("does find objects non-equal", () => { expect(isEqualWithoutFunctions(obj, objB)).toEqual(false); }); test("does find objects deeply non-equal", () => { expect(isEqualWithoutFunctions(objC, objCDeeplyDifferent)).toEqual(false); }); }); describe("wasShuffled", () => { test("does detect that array was shuffled", () => { expect(wasShuffled(arr, arrShuffled, "id")).toEqual(true); }); test("does detect that different array was not shuffled", () => { expect(wasShuffled(arr, arrB, "id")).toEqual(false); }); test("does detect that updated array was not shuffled", () => { expect(wasShuffled(arr, arrUpdated, "id")).toEqual(false); }); test("does detect that unshuffled array was not shuffled", () => { expect(wasShuffled(arr, arr, "id")).toEqual(false); }); }); describe("filterNil", () => { test("does remove nil elements", () => { expect(filterNil(arrWithNils)).toEqual([{ id: 1 }, { id: 2 }]); }); }); describe("iterateMiddleOut", () => { test("does iterate middle out with odd number of elements", () => { const oddArr = [1,2,3,4,5]; let results = []; iterateMiddleOut(oddArr, (item) => { results.push(item); }) expect(results).toEqual([3,4,2,5,1]); }) test("does iterate middle out with even number of elements", () => { const evenArr = [1,2,3,4]; let results = []; iterateMiddleOut(evenArr, (item) => { results.push(item); }) expect(results).toEqual([2,3,1,4]); }) test("does iterate middle out with provided middleIndex at the beginning", () => { const oddArr = [1,2,3,4,5]; let results = []; iterateMiddleOut(oddArr, (item) => { results.push(item); }, 0) expect(results).toEqual([1,2,3,4,5]); }) test("does iterate middle out with provided middleIndex at the end", () => { const oddArr = [1,2,3,4,5]; let results = []; iterateMiddleOut(oddArr, (item) => { results.push(item); }, oddArr.length - 1) expect(results).toEqual([5,4,3,2,1]); }) });