|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 | 17 |
|
18 |
| -import { h, render } from "dio.js"; |
19 |
| -import { expect } from "chai"; |
| 18 | +import {h, render} from "dio.js"; |
| 19 | +import {expect} from "chai"; |
20 | 20 | import {
|
21 | 21 | ComponentWithoutChildren,
|
22 | 22 | ComponentWithChildren,
|
23 | 23 | ComponentWithChildrenRerender,
|
24 | 24 | ComponentWithDifferentViews,
|
25 | 25 | ComponentWithProperties,
|
26 |
| - ComponentWithUnregistered, |
27 | 26 | ComponentWithImperativeEvent,
|
28 |
| - ComponentWithDeclarativeEvent |
29 | 27 | } from "./components";
|
30 | 28 |
|
| 29 | +import tests from 'basic-tests'; |
| 30 | + |
31 | 31 | // Setup the test harness. This will get cleaned out with every test.
|
32 | 32 | let app = document.createElement("div");
|
33 | 33 | app.id = "app";
|
34 | 34 | document.body.appendChild(app);
|
35 | 35 | let scratch; // This will hold the actual element under test.
|
36 | 36 |
|
37 |
| -beforeEach(function() { |
| 37 | +beforeEach(function () { |
38 | 38 | scratch = document.createElement("div");
|
39 | 39 | scratch.id = "scratch";
|
40 | 40 | app.appendChild(scratch);
|
41 | 41 | });
|
42 | 42 |
|
43 |
| -afterEach(function() { |
| 43 | +afterEach(function () { |
44 | 44 | app.innerHTML = "";
|
45 | 45 | scratch = null;
|
46 | 46 | });
|
47 | 47 |
|
48 |
| -describe("basic support", function() { |
49 |
| - |
50 |
| - describe("no children", function() { |
51 |
| - it("can display a Custom Element with no children", function() { |
52 |
| - this.weight = 3; |
53 |
| - render(<ComponentWithoutChildren />, scratch), scratch; |
54 |
| - let wc = scratch.querySelector("#wc"); |
55 |
| - expect(wc).to.exist; |
56 |
| - }); |
57 |
| - }); |
58 |
| - |
59 |
| - describe("with children", function() { |
60 |
| - function expectHasChildren(wc) { |
61 |
| - expect(wc).to.exist; |
62 |
| - let shadowRoot = wc.shadowRoot; |
63 |
| - let heading = shadowRoot.querySelector("h1"); |
64 |
| - expect(heading).to.exist; |
65 |
| - expect(heading.textContent).to.eql("Test h1"); |
66 |
| - let paragraph = shadowRoot.querySelector("p"); |
67 |
| - expect(paragraph).to.exist; |
68 |
| - expect(paragraph.textContent).to.eql("Test p"); |
69 |
| - } |
70 |
| - |
71 |
| - it("can display a Custom Element with children in a Shadow Root", function() { |
72 |
| - this.weight = 3; |
73 |
| - render(<ComponentWithChildren />, scratch) |
74 |
| - let wc = scratch.querySelector("#wc"); |
75 |
| - expectHasChildren(wc); |
76 |
| - }); |
77 |
| - |
78 |
| - it("can display a Custom Element with children in a Shadow Root and pass in Light DOM children", async function() { |
79 |
| - this.weight = 3; |
80 |
| - let component; |
81 |
| - render(<ComponentWithChildrenRerender ref={(instance) => {component = instance}} />, scratch) |
82 |
| - let wc = scratch.querySelector("#wc"); |
83 |
| - await Promise.resolve(); |
84 |
| - component.forceUpdate(); |
85 |
| - expectHasChildren(wc); |
86 |
| - expect(wc.textContent.includes("2")).to.be.true; |
87 |
| - }); |
| 48 | +function _render(Component) { |
| 49 | + let component |
| 50 | + render(<Component ref={(instance) => component = instance} />, scratch), scratch; |
| 51 | + const wc = scratch.querySelector("#wc"); |
| 52 | + return { wc, component } |
| 53 | +} |
88 | 54 |
|
89 |
| - it("can display a Custom Element with children in the Shadow DOM and handle hiding and showing the element", function() { |
90 |
| - this.weight = 3; |
91 |
| - let component; |
92 |
| - render(<ComponentWithDifferentViews ref={(instance) => {component = instance}} />, scratch); |
93 |
| - let wc = scratch.querySelector("#wc"); |
94 |
| - expectHasChildren(wc); |
95 |
| - component.toggle(); |
96 |
| - component.forceUpdate(); |
97 |
| - let dummy = scratch.querySelector("#dummy"); |
98 |
| - expect(dummy).to.exist; |
99 |
| - expect(dummy.textContent).to.eql("Dummy view"); |
| 55 | +tests({ |
| 56 | + renderComponentWithoutChildren() { |
| 57 | + return _render(ComponentWithChildren); |
| 58 | + }, |
| 59 | + renderComponentWithChildren() { |
| 60 | + return _render(ComponentWithChildren); |
| 61 | + }, |
| 62 | + async renderComponentWithChildrenRerender() { |
| 63 | + const { wc, component } = _render(ComponentWithChildrenRerender); |
| 64 | + await Promise.resolve(); |
| 65 | + component.forceUpdate(); |
| 66 | + return { wc } |
| 67 | + }, |
| 68 | + renderComponentWithDifferentViews() { |
| 69 | + const { wc, component } = _render(ComponentWithDifferentViews); |
| 70 | + function toggle() { |
100 | 71 | component.toggle();
|
101 | 72 | component.forceUpdate();
|
102 |
| - wc = scratch.querySelector("#wc"); |
103 |
| - expectHasChildren(wc); |
104 |
| - }); |
105 |
| - }); |
106 |
| - |
107 |
| - describe("attributes and properties", function() { |
108 |
| - it("will pass boolean data as either an attribute or a property", function() { |
109 |
| - this.weight = 3; |
110 |
| - render(<ComponentWithProperties />, scratch); |
111 |
| - let wc = scratch.querySelector("#wc"); |
112 |
| - let data = wc.bool || wc.hasAttribute("bool"); |
113 |
| - expect(data).to.be.true; |
114 |
| - }); |
115 |
| - |
116 |
| - it("will pass numeric data as either an attribute or a property", function() { |
117 |
| - this.weight = 3; |
118 |
| - render(<ComponentWithProperties />, scratch); |
119 |
| - let wc = scratch.querySelector("#wc"); |
120 |
| - let data = wc.num || wc.getAttribute("num"); |
121 |
| - expect(parseInt(data, 10)).to.eql(42); |
122 |
| - }); |
123 |
| - |
124 |
| - it("will pass string data as either an attribute or a property", function() { |
125 |
| - this.weight = 3; |
126 |
| - render(<ComponentWithProperties />, scratch); |
127 |
| - let wc = scratch.querySelector("#wc"); |
128 |
| - let data = wc.str || wc.getAttribute("str"); |
129 |
| - expect(data).to.eql("DIO"); |
130 |
| - }); |
131 |
| - }); |
132 |
| - |
133 |
| - describe("events", function() { |
134 |
| - it("can imperatively listen to a DOM event dispatched by a Custom Element", function() { |
135 |
| - this.weight = 3; |
136 |
| - let component |
137 |
| - render(<ComponentWithImperativeEvent ref={(instance) => {component = instance}} />, scratch); |
138 |
| - let wc = scratch.querySelector("#wc"); |
139 |
| - expect(wc).to.exist; |
140 |
| - let handled = scratch.querySelector("#handled"); |
141 |
| - expect(handled.textContent).to.eql("false"); |
| 73 | + } |
| 74 | + return { wc, toggle } |
| 75 | + }, |
| 76 | + renderComponentWithProperties() { |
| 77 | + return _render(ComponentWithProperties); |
| 78 | + }, |
| 79 | + renderComponentWithImperativeEvent() { |
| 80 | + const { wc, component } = _render(ComponentWithImperativeEvent); |
| 81 | + function click() { |
142 | 82 | wc.click();
|
143 | 83 | component.forceUpdate();
|
144 |
| - expect(handled.textContent).to.eql("true"); |
145 |
| - }); |
146 |
| - }); |
147 |
| - |
| 84 | + } |
| 85 | + return { wc, click }; |
| 86 | + } |
148 | 87 | });
|
0 commit comments