-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathbasic-tests.ts
157 lines (140 loc) · 5.69 KB
/
basic-tests.ts
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
/**
* @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 { expect } from "chai";
import {
ComponentWithoutChildren,
ComponentWithChildren,
ComponentWithChildrenRerender,
ComponentWithDifferentViews,
ComponentWithProperties,
ComponentWithImperativeEvent,
ComponentWithoutProperties
} from "./components";
import renderer, { w } from "@dojo/framework/core/vdom";
// 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: any; // 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;
const r = renderer(() => w(ComponentWithoutChildren, {}));
r.mount({ domNode: scratch, sync: true });
const wc = document.querySelector("ce-without-children");
expect(wc).to.exist;
});
});
describe("with children", function() {
function expectHasChildren(wc: any) {
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;
const r = renderer(() => w(ComponentWithChildren, {}));
r.mount({ domNode: scratch, sync: true });
const wc = document.querySelector("ce-with-children");
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;
const r = renderer(() => w(ComponentWithChildrenRerender, {}));
r.mount({ domNode: scratch, sync: true });
const wc = document.querySelector("ce-with-children") as HTMLElement;
expect(wc.innerHTML).to.eq('1');
expectHasChildren(wc);
await Promise.resolve();
expect(wc.innerHTML).to.eq('2');
expectHasChildren(wc);
});
it("can display a Custom Element with children in the Shadow DOM and handle hiding and showing the element", function() {
this.weight = 3;
const r = renderer(() => w(ComponentWithDifferentViews, {}));
r.mount({ domNode: scratch, sync: true });
let wc = document.querySelector("ce-with-children");
expectHasChildren(wc);
const toggle = document.querySelector("#toggle") as HTMLButtonElement;
toggle.click();
const dummy = document.querySelector("#dummy") as HTMLDivElement;
expect(dummy.innerHTML).to.eq('Dummy view');
toggle.click();
wc = document.querySelector("ce-with-children");
expectHasChildren(wc);
});
});
describe("attributes and properties", function() {
it("will pass boolean data as either an attribute or a property", function() {
this.weight = 3;
const r = renderer(() => w(ComponentWithProperties, {}));
r.mount({ domNode: scratch, sync: true });
const wc = document.querySelector("ce-with-properties");
expect((wc as any).bool).to.be.true;
});
it("will pass numeric data as either an attribute or a property", function() {
this.weight = 3;
const r = renderer(() => w(ComponentWithProperties, {}));
r.mount({ domNode: scratch, sync: true });
const wc = document.querySelector("ce-with-properties");
expect((wc as any).num).to.eql(42);
});
it("will pass string data as either an attribute or a property", function() {
this.weight = 3;
const r = renderer(() => w(ComponentWithProperties, {}));
r.mount({ domNode: scratch, sync: true });
const wc: any = document.querySelector("ce-with-properties");
const data = wc.getAttribute("str");
expect(data).to.eql("Dojo");
});
it('will not overwrite unwriteable properties', function () {
const r = renderer(() => w(ComponentWithoutProperties, {}));
r.mount({ domNode: scratch, sync: true });
const wc: any = document.querySelector("ce-without-properties");
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;
const r = renderer(() => w(ComponentWithImperativeEvent, {}));
r.mount({ domNode: scratch, sync: true });
const wc: any = document.querySelector("ce-with-event");
let handledResult: any = document.querySelector("#eventHandled");
expect(handledResult.handled).to.be.false;
wc.click();
expect(handledResult.handled).to.be.true;
});
});
});