UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

176 lines (132 loc) 4.57 kB
import { Touchable } from "@applicaster/zapp-react-native-ui-components/Components/Focusable/Touchable"; import { TreeNode } from "../TreeNode"; const component = { current: { props: { id: "testId", }, }, } as React.RefObject<Touchable>; describe("TreeNode", () => { it("should create a node", () => { const node = new TreeNode(component, "testId"); expect(node).toHaveProperty("component"); expect(node).toHaveProperty("children"); expect(node).toHaveProperty("id"); expect(node).toBeInstanceOf(TreeNode); }); describe("addChild", () => { const childComponent = { current: { props: { id: "childId", }, }, } as React.RefObject<Touchable>; it("should add a node to children array", () => { const node = new TreeNode(component, "testId"); const child = new TreeNode(childComponent, "childId"); node.addChild(child); expect(node.children).toContain(child); expect(node.children).toHaveLength(1); expect(node.children[0]).toBe(child); expect(node.children[0].id).toBe(childComponent.current.props.id); }); it("should create node if component passed", () => { const node = new TreeNode(component, "testId"); node.addChild(childComponent, "childId"); expect(node.children).toHaveLength(1); expect(node.children[0]).toBeInstanceOf(TreeNode); expect(node.children[0].id).toBe(childComponent.current.props.id); }); it("should not add a child with an existing id", () => { const node = new TreeNode(component, "testId"); const child1 = new TreeNode(component, "childId"); const child2 = new TreeNode(component, "childId"); node.addChild(child1); node.addChild(child2); expect(node.children).toHaveLength(1); }); }); describe("updateNode", () => { const childComponent = { current: { props: { id: "childId", }, }, } as React.RefObject<Touchable>; it("should update node on the root level", () => { const node = new TreeNode(component, "testId"); const updatedComponent = { current: { props: { id: "testId", }, }, } as React.RefObject<Touchable>; node.updateNode(updatedComponent); expect(node.component).toBe(updatedComponent); }); it("shouldn't overwrite children when updating", () => { const node = new TreeNode(component, "testId"); const child = new TreeNode(childComponent, "childId"); node.addChild(child); const updatedComponent = { current: { props: { id: "childId", }, }, } as React.RefObject<Touchable>; node.updateNode(updatedComponent); expect(node.children).toHaveLength(1); expect(node.children[0].id).toBe(childComponent.current.props.id); }); it("should not update node if id doesn't match", () => { const node = new TreeNode(component, "testId"); const updatedComponent = { current: { props: { id: "wrongId", }, }, } as React.RefObject<Touchable>; node.updateNode(updatedComponent); expect(node.component).toBe(component); }); it("should not update node if component is null", () => { const node = new TreeNode(component, "testId"); expect(() => node.updateNode(null)).toThrowError(); expect(node.component).toBe(component); }); }); describe("removeNode", () => { it("should remove node", () => { const node = new TreeNode(component, "testId"); const child = new TreeNode(component, "childId"); node.addChild(child); node.removeNode("childId"); expect(node.children).toHaveLength(0); }); it("should do nothing if node is not found", () => { const node = new TreeNode(component, "testId"); node.removeNode("nonexistentId"); expect(node.children).toHaveLength(0); }); }); describe("findNode", () => { it("should find node", () => { const node = new TreeNode(component, "testId"); const child = new TreeNode(component, "childId"); node.addChild(child); const found = node.findNode("childId"); expect(found).toBe(child); }); it("should return null if node is not found", () => { const node = new TreeNode(component, "testId"); const found = node.findNode("nonexistentId"); expect(found).toBeNull(); }); }); });