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:
parent
dd0e2dc394
commit
f48dcba59d
@ -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);
|
||||
});
|
||||
});
|
||||
@ -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 };
|
||||
Loading…
Reference in New Issue
Block a user