-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathbasic-tests.js
204 lines (182 loc) · 7.49 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* @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 {createApp, nextTick} from 'vue';
import {
ComponentWithoutChildren,
ComponentWithChildren,
ComponentWithChildrenRerender,
ComponentWithDifferentViews,
ComponentWithProperties,
ComponentWithUnregistered,
ComponentWithImperativeEvent,
ComponentWithDeclarativeEvent,
ComponentWithMethods
} from "./components";
import { expect } from "chai";
// Setup the test harness. This will get cleaned out with every test.
const container = document.createElement("div");
document.body.appendChild(container);
let scratch; // This will hold the actual element under test.
const isCustomElement = (tagName) => {
return window.customElements.get(tagName) !== undefined;
}
beforeEach(function() {
scratch = document.createElement("div");
scratch.id = "scratch";
container.appendChild(scratch);
});
afterEach(function() {
container.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 app = createApp(ComponentWithoutChildren)
app.config.compilerOptions.isCustomElement = isCustomElement;
app.mount(scratch);
const wc = scratch.querySelector("#wc");
expect(wc).to.exist;
});
});
describe("with children", function() {
function expectHasChildren(wc) {
expect(wc).to.exist;
const shadowRoot = wc.shadowRoot;
const heading = shadowRoot.querySelector("h1");
expect(heading).to.exist;
expect(heading.textContent).to.eql("Test h1");
const 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 app = createApp(ComponentWithChildren)
app.config.compilerOptions.isCustomElement = isCustomElement;
app.mount(scratch);
const 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;
const app = createApp(ComponentWithChildrenRerender)
app.config.compilerOptions.isCustomElement = isCustomElement;
app.mount(scratch);
const wc = scratch.querySelector("#wc");
// Waits for the tick in ComponentWithChildrenRerender's mount function
await nextTick();
// Waits for the increment inside that tick to appear in the DOM.
await nextTick();
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", async function() {
this.weight = 3;
const app = createApp(ComponentWithDifferentViews)
app.config.compilerOptions.isCustomElement = isCustomElement;
app.mount(scratch);
const toggler = scratch.querySelector('#toggler');
let wc = scratch.querySelector("#wc");
expectHasChildren(wc);
toggler.click();
await nextTick();
const dummy = scratch.querySelector("#dummy");
expect(dummy).to.exist;
expect(dummy.textContent).to.eql("Dummy view");
toggler.click();
await nextTick();
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;
const app = createApp(ComponentWithProperties)
app.config.compilerOptions.isCustomElement = isCustomElement;
app.mount(scratch);
const wc = scratch.querySelector("#wc");
const 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;
const app = createApp(ComponentWithProperties)
app.config.compilerOptions.isCustomElement = isCustomElement;
app.mount(scratch);
const wc = scratch.querySelector("#wc");
const 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;
const app = createApp(ComponentWithProperties)
app.config.compilerOptions.isCustomElement = isCustomElement;
app.mount(scratch);
const wc = scratch.querySelector("#wc");
const data = wc.str || wc.getAttribute("str");
expect(data).to.eql("Vue");
});
it('will not overwrite methods', function () {
const app = createApp(ComponentWithMethods);
app.mount(scratch);
const wc = scratch.querySelector('#wc');
expect(wc.innerText).to.eql('Success');
})
// it('will set boolean attributes on a Custom Element that has not already been defined and upgraded', function() {
// let root = new ComponentWithUnregistered().$mount(scratch).$el;
// let wc = root.querySelector('#wc');
// expect(wc.hasAttribute('bool')).to.be.true;
// });
// it('will set numeric attributes on a Custom Element that has not already been defined and upgraded', function() {
// let root = new ComponentWithUnregistered().$mount(scratch).$el;
// let wc = root.querySelector('#wc');
// expect(wc.getAttribute('num')).to.eql('42');
// });
// it('will set string attributes on a Custom Element that has not already been defined and upgraded', function() {
// let root = new ComponentWithUnregistered().$mount(scratch).$el;
// let wc = root.querySelector('#wc');
// expect(wc.getAttribute('str')).to.eql('Vue');
// });
// it('will set array properties on a Custom Element that has not already been defined and upgraded', function() {
// let root = new ComponentWithUnregistered().$mount(scratch).$el;
// let wc = root.querySelector('#wc');
// expect(wc.arr).to.eql(['V', 'u', 'e']);
// });
// it('will set object properties on a Custom Element that has not already been defined and upgraded', function() {
// let root = new ComponentWithUnregistered().$mount(scratch).$el;
// let wc = root.querySelector('#wc');
// expect(wc.obj).to.eql({ org: 'vuejs', repo: 'vue' });
// });
});
describe("events", function() {
it("can imperatively listen to a DOM event dispatched by a Custom Element", async function() {
this.weight = 3;
const app = createApp(ComponentWithImperativeEvent)
app.config.compilerOptions.isCustomElement = isCustomElement;
app.mount(scratch);
const wc = scratch.querySelector("#wc");
const handled = scratch.querySelector("#handled");
expect(handled.textContent).to.eql("false");
wc.click();
await nextTick();
expect(handled.textContent).to.eql("true");
});
});
});