-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathbasic-tests.js
159 lines (143 loc) · 5.37 KB
/
basic-tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* @license
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { h, render } from "dio.js";
import { expect } from "chai";
import {
ComponentWithoutChildren,
ComponentWithChildren,
ComponentWithChildrenRerender,
ComponentWithDifferentViews,
ComponentWithProperties,
ComponentWithUnregistered,
ComponentWithImperativeEvent,
ComponentWithDeclarativeEvent,
ComponentWithoutProperties
} from "./components";
// Setup the test harness. This will get cleaned out with every test.
let app = document.createElement("div");
app.id = "app";
document.body.appendChild(app);
let scratch; // This will hold the actual element under test.
beforeEach(function() {
scratch = document.createElement("div");
scratch.id = "scratch";
app.appendChild(scratch);
});
afterEach(function() {
app.innerHTML = "";
scratch = null;
});
describe("basic support", function() {
describe("no children", function() {
it("can display a Custom Element with no children", function() {
this.weight = 3;
render(<ComponentWithoutChildren />, scratch), scratch;
let wc = scratch.querySelector("#wc");
expect(wc).to.exist;
});
});
describe("with children", function() {
function expectHasChildren(wc) {
expect(wc).to.exist;
let shadowRoot = wc.shadowRoot;
let heading = shadowRoot.querySelector("h1");
expect(heading).to.exist;
expect(heading.textContent).to.eql("Test h1");
let paragraph = shadowRoot.querySelector("p");
expect(paragraph).to.exist;
expect(paragraph.textContent).to.eql("Test p");
}
it("can display a Custom Element with children in a Shadow Root", function() {
this.weight = 3;
render(<ComponentWithChildren />, scratch)
let wc = scratch.querySelector("#wc");
expectHasChildren(wc);
});
it("can display a Custom Element with children in a Shadow Root and pass in Light DOM children", async function() {
this.weight = 3;
let component;
render(<ComponentWithChildrenRerender ref={(instance) => {component = instance}} />, scratch)
let wc = scratch.querySelector("#wc");
await Promise.resolve();
component.forceUpdate();
expectHasChildren(wc);
expect(wc.textContent.includes("2")).to.be.true;
});
it("can display a Custom Element with children in the Shadow DOM and handle hiding and showing the element", function() {
this.weight = 3;
let component;
render(<ComponentWithDifferentViews ref={(instance) => {component = instance}} />, scratch);
let wc = scratch.querySelector("#wc");
expectHasChildren(wc);
component.toggle();
component.forceUpdate();
let dummy = scratch.querySelector("#dummy");
expect(dummy).to.exist;
expect(dummy.textContent).to.eql("Dummy view");
component.toggle();
component.forceUpdate();
wc = scratch.querySelector("#wc");
expectHasChildren(wc);
});
});
describe("attributes and properties", function() {
it("will pass boolean data as either an attribute or a property", function() {
this.weight = 3;
render(<ComponentWithProperties />, scratch);
let wc = scratch.querySelector("#wc");
let data = wc.bool || wc.hasAttribute("bool");
expect(data).to.be.true;
});
it("will pass numeric data as either an attribute or a property", function() {
this.weight = 3;
render(<ComponentWithProperties />, scratch);
let wc = scratch.querySelector("#wc");
let data = wc.num || wc.getAttribute("num");
expect(parseInt(data, 10)).to.eql(42);
});
it("will pass string data as either an attribute or a property", function() {
this.weight = 3;
render(<ComponentWithProperties />, scratch);
let wc = scratch.querySelector("#wc");
let data = wc.str || wc.getAttribute("str");
expect(data).to.eql("DIO");
});
it('will not overwrite unwriteable properties', function () {
render(<ComponentWithoutProperties />, scratch);
console.log(scratch);
let wc = scratch.querySelector("#wc");
expect(wc.getAttribute('amethod')).to.eql('method');
expect(wc.getAttribute('agetter')).to.eql('getter');
expect(wc.getAttribute('areadonly')).to.eql('readonly');
expect(wc.innerHTML).to.eql('Success');
})
});
describe("events", function() {
it("can imperatively listen to a DOM event dispatched by a Custom Element", function() {
this.weight = 3;
let component
render(<ComponentWithImperativeEvent ref={(instance) => {component = instance}} />, scratch);
let wc = scratch.querySelector("#wc");
expect(wc).to.exist;
let handled = scratch.querySelector("#handled");
expect(handled.textContent).to.eql("false");
wc.click();
component.forceUpdate();
expect(handled.textContent).to.eql("true");
});
});
});