From 5af44b7e9bf2adf7382933e2a1cdffafe644d188 Mon Sep 17 00:00:00 2001 From: ldd Date: Tue, 7 Apr 2020 20:44:44 -0400 Subject: [PATCH 1/3] test(Tree): simplify tests - refactor tests by using react-dom/test-utils's Simulate - use querySelector instead of querySelectorAll where possible --- src/components/Tree.test.js | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/src/components/Tree.test.js b/src/components/Tree.test.js index 7433844..6a5121b 100644 --- a/src/components/Tree.test.js +++ b/src/components/Tree.test.js @@ -4,7 +4,7 @@ import React from "react"; import renderer from "react-test-renderer"; import { Tree } from "./Tree"; import ReactDOM from "react-dom"; -import { act } from "react-dom/test-utils"; +import { Simulate } from "react-dom/test-utils"; // Enzyme.configure({ adapter: new Adapter() }); @@ -75,11 +75,8 @@ describe("React Component [Tree]", () => { }); it("can render and activate Nodes", () => { ReactDOM.render(, container); - const domNodes = container.querySelectorAll(".Node"); - const clickedNode = domNodes[0]; - act(() => { - clickedNode.dispatchEvent(new MouseEvent("click", { bubbles: true })); - }); + const clickedNode = container.querySelector(".Node"); + Simulate.click(clickedNode); const activeNode = container.querySelector(".Node.active"); expect(activeNode).toBeDefined(); expect(activeNode).toEqual(expect.objectContaining({ id: clickedNode.id })); @@ -91,14 +88,9 @@ describe("React Component [Tree]", () => { }); it("can render and deactivate Nodes", () => { ReactDOM.render(, container); - const domNodes = container.querySelectorAll(".Node"); - const clickedNode = domNodes[0]; - act(() => { - clickedNode.dispatchEvent(new MouseEvent("click", { bubbles: true })); - }); - act(() => { - clickedNode.dispatchEvent(new MouseEvent("click", { bubbles: true })); - }); + const clickedNode = container.querySelector(".Node"); + Simulate.click(clickedNode); + Simulate.click(clickedNode); const activeNode = container.querySelector(".Node.active"); expect(activeNode).toBeNull(); @@ -108,11 +100,8 @@ describe("React Component [Tree]", () => { it("can render and ignore invalid Links", () => { const invalidLinks = [{ from: "A" }]; ReactDOM.render(, container); - const domNodes = container.querySelectorAll(".Node"); - const clickedNode = domNodes[0]; - act(() => { - clickedNode.dispatchEvent(new MouseEvent("click", { bubbles: true })); - }); + const clickedNode = container.querySelector(".Node"); + Simulate.click(clickedNode); const activeNode = container.querySelector(".Node.active"); expect(activeNode).toBeDefined(); From 7840db81f8846e9dd5fa7e9352e2f7705936251e Mon Sep 17 00:00:00 2001 From: ldd Date: Tue, 7 Apr 2020 20:47:19 -0400 Subject: [PATCH 2/3] test(Node): add corner-case test --- src/components/Node.test.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/components/Node.test.js diff --git a/src/components/Node.test.js b/src/components/Node.test.js new file mode 100644 index 0000000..09c9735 --- /dev/null +++ b/src/components/Node.test.js @@ -0,0 +1,22 @@ +import React from "react"; +import { render } from "react-dom"; +import { Simulate } from "react-dom/test-utils"; +import { Node } from "./Node"; + +describe("React Component [Node]", () => { + it("can test Nodes near invalid Links", () => { + const container = document.createElement("div"); + document.body.appendChild(container); + const invalidLinkId = "AB"; + render( + <> +
+ + , + container + ); + const domNode = container.querySelector(".Node"); + Simulate.click(domNode); + document.body.removeChild(container); + }); +}); From ec0ff386c2ffb9d8003b9e095cd5a453b52bc8a3 Mon Sep 17 00:00:00 2001 From: ldd Date: Tue, 7 Apr 2020 20:48:25 -0400 Subject: [PATCH 3/3] fix(Node): remove default value forid in toggleChildren --- src/components/Node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Node.js b/src/components/Node.js index 2c0f387..6300cee 100644 --- a/src/components/Node.js +++ b/src/components/Node.js @@ -2,7 +2,7 @@ import React from "react"; import { Sprite } from "./Sprite"; import "./Node.css"; -const toggleChildren = (id = "", isActive) => { +const toggleChildren = (id, isActive) => { // given a link A-N, we are interested in A's child, N const childNode = document.getElementById(id.split("-")[1]); if (!childNode) return;