From 2a2ab66a4432d4c55aa26f284d6b0080df98fe75 Mon Sep 17 00:00:00 2001 From: Iku-turso Date: Mon, 24 Oct 2022 12:49:00 +0300 Subject: [PATCH] Consolidate tests to now point-free composite Co-authored-by: Janne Savolainen Signed-off-by: Iku-turso --- .../composite-has-descendant.test.ts | 21 +++++++++++++---- .../find-composite/find-composite.test.ts | 23 +++++++++++-------- .../get-composite-normalization.test.ts | 14 ++++++++--- .../get-composite-paths.test.ts | 18 +++++++++++---- 4 files changed, 55 insertions(+), 21 deletions(-) diff --git a/src/common/utils/composite/composite-has-descendant/composite-has-descendant.test.ts b/src/common/utils/composite/composite-has-descendant/composite-has-descendant.test.ts index 0c20840e18..2dae7326d1 100644 --- a/src/common/utils/composite/composite-has-descendant/composite-has-descendant.test.ts +++ b/src/common/utils/composite/composite-has-descendant/composite-has-descendant.test.ts @@ -4,11 +4,11 @@ */ import type { Composite } from "../get-composite/get-composite"; -import getComposite from "../get-composite/get-composite"; import { compositeHasDescendant } from "./composite-has-descendant"; +import getCompositeFor from "../get-composite/get-composite"; describe("composite-has-descendant, given composite with children and grand children", () => { - let composite: Composite<{ id: string; parentId: string | undefined }>; + let composite: Composite<{ id: string; parentId?: string }>; beforeEach(() => { const items = [ @@ -21,7 +21,16 @@ describe("composite-has-descendant, given composite with children and grand chil }, ]; - composite = getComposite({ source: items }); + const getComposite = getCompositeFor<{ + id: string; + parentId?: string; + }>({ + rootId: "some-root-id", + getId: (x) => x.id, + getParentId: (x) => x.parentId, + }); + + composite = getComposite(items); }); it("has a child as descendant", () => { @@ -34,7 +43,8 @@ describe("composite-has-descendant, given composite with children and grand chil it("has a grand child as descendant", () => { const actual = compositeHasDescendant( - (referenceComposite) => referenceComposite.value.id === "some-grand-child-item", + (referenceComposite) => + referenceComposite.value.id === "some-grand-child-item", )(composite); expect(actual).toBe(true); @@ -42,7 +52,8 @@ describe("composite-has-descendant, given composite with children and grand chil it("does not have an unrelated descendant", () => { const actual = compositeHasDescendant( - (referenceComposite) => referenceComposite.value.id === "some-unknown-item", + (referenceComposite) => + referenceComposite.value.id === "some-unknown-item", )(composite); expect(actual).toBe(false); diff --git a/src/common/utils/composite/find-composite/find-composite.test.ts b/src/common/utils/composite/find-composite/find-composite.test.ts index 10ba1c6111..a04d79325c 100644 --- a/src/common/utils/composite/find-composite/find-composite.test.ts +++ b/src/common/utils/composite/find-composite/find-composite.test.ts @@ -3,23 +3,28 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import type { Composite } from "../get-composite/get-composite"; -import getComposite from "../get-composite/get-composite"; import { findComposite } from "./find-composite"; +import getCompositeFor from "../get-composite/get-composite"; describe("find-composite", () => { let composite: Composite<{ id: string; parentId?: string }>; beforeEach(() => { - composite = getComposite({ - source: [ - { id: "some-root-id" }, - { id: "some-child-id", parentId: "some-root-id" }, - { id: "some-grandchild-id", parentId: "some-child-id" }, - { id: "some-other-grandchild-id", parentId: "some-child-id" }, - ], - + const getComposite = getCompositeFor<{ + id: string; + parentId?: string; + }>({ rootId: "some-root-id", + getId: (x) => x.id, + getParentId: (x) => x.parentId, }); + + composite = getComposite([ + { id: "some-root-id" }, + { id: "some-child-id", parentId: "some-root-id" }, + { id: "some-grandchild-id", parentId: "some-child-id" }, + { id: "some-other-grandchild-id", parentId: "some-child-id" }, + ]); }); it("when finding root using path, does so", () => { diff --git a/src/common/utils/composite/get-composite-normalization/get-composite-normalization.test.ts b/src/common/utils/composite/get-composite-normalization/get-composite-normalization.test.ts index b4dbe8032e..2a3074c4f1 100644 --- a/src/common/utils/composite/get-composite-normalization/get-composite-normalization.test.ts +++ b/src/common/utils/composite/get-composite-normalization/get-composite-normalization.test.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getCompositeNormalization } from "./get-composite-normalization"; -import getComposite from "../get-composite/get-composite"; +import getCompositeFor from "../get-composite/get-composite"; describe("get-composite-normalization", () => { it("given a composite, flattens it to paths and composites", () => { @@ -24,10 +24,18 @@ describe("get-composite-normalization", () => { const items = [someRootItem, someItem, someNestedItem]; - const composite = getComposite({ - source: items, + const getComposite = getCompositeFor<{ + id: string; + parentId?: string; + orderNumber?: number; + }>({ + rootId: "some-root-id", + getId: (x) => x.id, + getParentId: (x) => x.parentId, }); + const composite = getComposite(items); + const actual = getCompositeNormalization(composite); expect(actual).toEqual([ diff --git a/src/common/utils/composite/get-composite-paths/get-composite-paths.test.ts b/src/common/utils/composite/get-composite-paths/get-composite-paths.test.ts index 67870713f0..5d0c28e771 100644 --- a/src/common/utils/composite/get-composite-paths/get-composite-paths.test.ts +++ b/src/common/utils/composite/get-composite-paths/get-composite-paths.test.ts @@ -2,11 +2,12 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -import getComposite from "../get-composite/get-composite"; import { getCompositePaths } from "./get-composite-paths"; +import getCompositeFor from "../get-composite/get-composite"; +import { sortBy } from "lodash/fp"; describe("get-composite-paths", () => { - it("given composite with ordered children, returns ordered paths", () => { + it("given composite with transformed children, returns paths of transformed children in hierarchical order", () => { const someRootItem = { id: "some-root-id", }; @@ -44,10 +45,19 @@ describe("get-composite-paths", () => { someGrandchildItem1, ]; - const composite = getComposite({ - source: items, + const getComposite = getCompositeFor<{ + id: string; + parentId?: string; + orderNumber?: number; + }>({ + rootId: "some-root-id", + getId: (x) => x.id, + getParentId: (x) => x.parentId, + transformChildren: children => sortBy(child => child.orderNumber, children), }); + const composite = getComposite(items); + const actual = getCompositePaths(composite); expect(actual).toEqual([