-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathbasic-tests.js
272 lines (248 loc) · 7.94 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* @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 React from "react";
import { createRoot } from "react-dom/client";
import { act } from "react-dom/test-utils";
import * as ReactDOM from "react-dom";
import { expect } from "chai";
import {
ComponentWithoutChildren,
ComponentWithChildren,
ComponentWithChildrenRerender,
ComponentWithDifferentViews,
ComponentWithProperties,
ComponentWithUnregistered,
ComponentWithImperativeEvent,
ComponentWithDeclarativeEvent,
ComponentWithMethods,
} 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.
let reactRoot = null;
function render(element) {
act(() => {
reactRoot.render(element);
});
}
before(() => {
window.IS_REACT_ACT_ENVIRONMENT = true;
});
beforeEach(function () {
scratch = document.createElement("div");
scratch.id = "scratch";
app.appendChild(scratch);
reactRoot = createRoot(scratch);
});
afterEach(function () {
app.innerHTML = "";
scratch = null;
act(() => {
reactRoot.unmount();
});
});
describe("basic support", function () {
describe("no children", function () {
it("can display a Custom Element with no children", function () {
this.weight = 3;
let root;
render(
<ComponentWithoutChildren
ref={(current) => {
root = current;
}}
/>
);
let wc = root.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;
let root;
render(
<ComponentWithChildren
ref={(current) => {
root = current;
}}
/>
);
let wc = root.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 root;
render(
<ComponentWithChildrenRerender
ref={(current) => {
root = current;
}}
/>
);
let wc = root.wc;
await act(async () => {
await Promise.resolve();
});
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 root;
render(
<ComponentWithDifferentViews
ref={(current) => {
root = current;
}}
/>
);
let wc = root.wc;
expectHasChildren(wc);
act(() => {
root.toggle();
});
let dummy = root.dummy.current;
expect(dummy).to.exist;
expect(dummy.textContent).to.eql("Dummy view");
act(() => {
root.toggle();
});
wc = root.wc;
expectHasChildren(wc);
});
});
describe("attributes and properties", function () {
it("will pass boolean data as either an attribute or a property", function () {
this.weight = 3;
let root;
render(
<ComponentWithProperties
ref={(current) => {
root = current;
}}
/>
);
let wc = root.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;
let root;
render(
<ComponentWithProperties
ref={(current) => {
root = current;
}}
/>
);
let wc = root.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;
let root;
render(
<ComponentWithProperties
ref={(current) => {
root = current;
}}
/>
);
let wc = root.wc;
let data = wc.str || wc.getAttribute("str");
expect(data).to.eql("React");
});
it('will not overwrite methods', function () {
let root;
render(
<ComponentWithMethods
ref={(current) => {
root = current;
}}
/>
)
let wc = root.wc;
expect(wc.innerText).to.eql('Success');
})
// TODO: Is it the framework's responsibility to check if the underlying
// property is defined? Or should it just always assume it is and do its
// usual default behavior? Preact will actually check if it's defined and
// use an attribute if it is not, otherwise it prefers properties for
// everything. Is there a "right" answer in this situation?
// it('will set boolean attributes on a Custom Element that has not already been defined and upgraded', function() {
// let root = ReactDOM.render(<ComponentWithUnregistered />, scratch);
// let wc = ReactDOM.findDOMNode(root.refs.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 = ReactDOM.render(<ComponentWithUnregistered />, scratch);
// let wc = ReactDOM.findDOMNode(root.refs.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 = ReactDOM.render(<ComponentWithUnregistered />, scratch);
// let wc = ReactDOM.findDOMNode(root.refs.wc);
// expect(wc.getAttribute('str')).to.eql('React');
// });
// it('will set array attributes on a Custom Element that has not already been defined and upgraded', function() {
// let root = ReactDOM.render(<ComponentWithUnregistered />, scratch);
// let wc = ReactDOM.findDOMNode(root.refs.wc);
// expect(wc.getAttribute('arr')).to.eql(JSON.stringify(['R', 'e', 'a', 'c', 't']));
// });
// it('will set object attributes on a Custom Element that has not already been defined and upgraded', function() {
// let root = ReactDOM.render(<ComponentWithUnregistered />, scratch);
// let wc = ReactDOM.findDOMNode(root.refs.wc);
// expect(wc.getAttribute('obj')).to.eql(JSON.stringify({ org: 'facebook', repo: 'react' }));
// });
});
describe("events", function () {
it("can imperatively listen to a DOM event dispatched by a Custom Element", function () {
this.weight = 3;
let root;
render(
<ComponentWithImperativeEvent
ref={(current) => {
root = current;
}}
/>
);
let wc = root.wc;
let handled = root.handled;
expect(handled.textContent).to.eql("false");
act(() => {
wc.click();
});
expect(handled.textContent).to.eql("true");
});
});
});