mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 00:33:44 +00:00
Assign ownership to many libs files (#6928)
Assign ownership to many of the remaining libs/common files. Criteria for ownership: * Files used by a single team, is now owned by that team. * Files related to a domain owned by a team is now owned by that team. * Where ownership is unclear the "lowest level" service takes ownership.
This commit is contained in:
71
libs/common/src/vault/service-utils.spec.ts
Normal file
71
libs/common/src/vault/service-utils.spec.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { ITreeNodeObject, TreeNode } from "./models/domain/tree-node";
|
||||
import { ServiceUtils } from "./service-utils";
|
||||
|
||||
type FakeObject = { id: string; name: string };
|
||||
|
||||
describe("serviceUtils", () => {
|
||||
let nodeTree: TreeNode<FakeObject>[];
|
||||
beforeEach(() => {
|
||||
nodeTree = [
|
||||
createTreeNode({ id: "1", name: "1" }, [
|
||||
createTreeNode({ id: "1.1", name: "1.1" }, [
|
||||
createTreeNode({ id: "1.1.1", name: "1.1.1" }),
|
||||
]),
|
||||
createTreeNode({ id: "1.2", name: "1.2" }),
|
||||
])(null),
|
||||
createTreeNode({ id: "2", name: "2" }, [createTreeNode({ id: "2.1", name: "2.1" })])(null),
|
||||
createTreeNode({ id: "3", name: "3" }, [])(null),
|
||||
];
|
||||
});
|
||||
|
||||
describe("nestedTraverse", () => {
|
||||
it("should traverse a tree and add a node at the correct position given a valid path", () => {
|
||||
const nodeToBeAdded: FakeObject = { id: "1.2.1", name: "1.2.1" };
|
||||
const path = ["1", "1.2", "1.2.1"];
|
||||
|
||||
ServiceUtils.nestedTraverse(nodeTree, 0, path, nodeToBeAdded, null, "/");
|
||||
expect(nodeTree[0].children[1].children[0].node).toEqual(nodeToBeAdded);
|
||||
});
|
||||
|
||||
it("should combine the path for missing nodes and use as the added node name given an invalid path", () => {
|
||||
const nodeToBeAdded: FakeObject = { id: "blank", name: "blank" };
|
||||
const path = ["3", "3.1", "3.1.1"];
|
||||
|
||||
ServiceUtils.nestedTraverse(nodeTree, 0, path, nodeToBeAdded, null, "/");
|
||||
expect(nodeTree[2].children[0].node.name).toEqual("3.1/3.1.1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getTreeNodeObject", () => {
|
||||
it("should return a matching node given a single tree branch and a valid id", () => {
|
||||
const id = "1.1.1";
|
||||
const given = ServiceUtils.getTreeNodeObject(nodeTree[0], id);
|
||||
expect(given.node.id).toEqual(id);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getTreeNodeObjectFromList", () => {
|
||||
it("should return a matching node given a list of branches and a valid id", () => {
|
||||
const id = "1.1.1";
|
||||
const given = ServiceUtils.getTreeNodeObjectFromList(nodeTree, id);
|
||||
expect(given.node.id).toEqual(id);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
type TreeNodeFactory<T extends ITreeNodeObject> = (
|
||||
obj: T,
|
||||
children?: TreeNodeFactoryWithoutParent<T>[]
|
||||
) => TreeNodeFactoryWithoutParent<T>;
|
||||
|
||||
type TreeNodeFactoryWithoutParent<T extends ITreeNodeObject> = (
|
||||
parent?: TreeNode<T>
|
||||
) => TreeNode<T>;
|
||||
|
||||
const createTreeNode: TreeNodeFactory<FakeObject> =
|
||||
(obj, children = []) =>
|
||||
(parent) => {
|
||||
const node = new TreeNode<FakeObject>(obj, parent, obj.name, obj.id);
|
||||
node.children = children.map((childFunc) => childFunc(node));
|
||||
return node;
|
||||
};
|
||||
Reference in New Issue
Block a user