1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Introduce way to find out if composite has a descendant

This will serve eg. hiding of empty preference tab groups.

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-turso 2022-10-17 10:55:01 +03:00 committed by Janne Savolainen
parent dd0e2dc394
commit f48dcba59d
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
2 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,50 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { Composite } from "../get-composite";
import getComposite from "../get-composite";
import { compositeHasDescendant } from "./composite-has-descendant";
describe("composite-has-descendant, given composite with children and grand children", () => {
let composite: Composite<{ id: string; parentId: string | undefined }>;
beforeEach(() => {
const items = [
{ id: "some-root-id", parentId: undefined },
{ id: "some-child-item", parentId: "some-root-id" },
{
id: "some-grand-child-item",
parentId: "some-child-item",
},
];
composite = getComposite({ source: items });
});
it("has a child as descendant", () => {
const actual = compositeHasDescendant<typeof composite["value"]>(
(referenceComposite) => referenceComposite.value.id === "some-child-item",
)(composite);
expect(actual).toBe(true);
});
it("has a grand child as descendant", () => {
const actual = compositeHasDescendant<typeof composite["value"]>(
(referenceComposite) => referenceComposite.value.id === "some-grand-child-item",
)(composite);
expect(actual).toBe(true);
});
it("does not have an unrelated descendant", () => {
const actual = compositeHasDescendant<typeof composite["value"]>(
(referenceComposite) => referenceComposite.value.id === "some-unknown-item",
)(composite);
expect(actual).toBe(false);
});
});

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { Composite } from "../get-composite";
const compositeHasDescendant = <T>(
predicate: (referenceComposite: Composite<T>) => boolean,
) => {
const _compositeHasDescendant = (composite: Composite<T>): boolean =>
predicate(composite) ||
!!composite.children.find((childComposite) =>
_compositeHasDescendant(childComposite),
);
return _compositeHasDescendant;
};
export { compositeHasDescendant };