forked from bitovi/react-to-web-component
-
Notifications
You must be signed in to change notification settings - Fork 1
/
react-to-can-webcomponent-test.js
375 lines (305 loc) · 11 KB
/
react-to-can-webcomponent-test.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import QUnit from "steal-qunit";
import React from 'react';
import ReactDOM from "react-dom/client";
import PropTypes from 'prop-types';
import PreactCompat from "preact/compat";
import stache from "can-stache";
import stacheBindings from "can-stache-bindings";
import ObservableObject from "can-observable-object";
import ObservableArray from "can-observable-array";
import reactToWebComponent from "./react-to-can-webcomponent";
stache.addBindings(stacheBindings);
QUnit.module("react-to-can-webcomponent");
function wait10ms() {
return new Promise(function(resolve) { setTimeout(resolve, 100); });
}
QUnit.test("basics with react", function(assert) {
var mountCount = 0;
var unmountCount = 0;
class Welcome extends React.Component {
componentDidMount() {
mountCount += 1;
}
componentWillUnmount() {
unmountCount += 1;
}
render() {
return <h1>Hello, {
this.props.name
}</h1>;
}
}
class MyWelcome extends reactToWebComponent(Welcome, React, ReactDOM) {}
customElements.define("my-welcome", MyWelcome);
var fixture = document.getElementById("qunit-fixture");
var myWelcome = new MyWelcome();
fixture.appendChild(myWelcome);
return wait10ms().then(function() {
assert.equal(myWelcome.nodeName, "MY-WELCOME", "able to read nodeName");
assert.equal(myWelcome.childNodes.length, 1, "able to render something")
assert.equal(myWelcome.childNodes[0].innerHTML, "Hello, ", "renders the right thing");
myWelcome.name = "Justin";
return wait10ms();
}).then(function() {
assert.equal(myWelcome.childNodes[0].innerHTML, "Hello, Justin", "can update");
// assert.equal(mountCount, 1, "component has only been mounted once");
assert.equal(unmountCount, 0, "component has not been unmounted");
});
});
QUnit.test("works with attributes set with propTypes", function(assert) {
class Greeting extends React.Component {
render() {
return <h1 >Hello, {
this.props.name
}</h1>;
}
}
Greeting.propTypes = {
name: PropTypes.string.isRequired
};
var MyGreeting = reactToWebComponent(Greeting, React, ReactDOM)
customElements.define("my-greeting", MyGreeting);
var fixture = document.getElementById("qunit-fixture");
var myGreeting = new MyGreeting();
var oldError = console.error;
console.error = function(message) {
var message = arguments[0];
if (arguments.length > 1 && ~message.indexOf("%s")) {
message = arguments[2];
}
assert.ok(message.includes("required"), "got a warning with required");
oldError = console.error;
}
fixture.appendChild(myGreeting);
fixture.innerHTML = "<my-greeting name='Christopher'></my-greeting>";
return wait10ms().then(function() {
assert.equal(fixture.firstElementChild.innerHTML, "<h1>Hello, Christopher</h1>");
});
});
QUnit.test("basics with preact", function(assert){
class Welcome extends React.Component {
render() {
return PreactCompat.createElement("h1",null,[
"Hello, ",
this.props.name
]);
}
}
class MyWelcome extends reactToWebComponent(Welcome, PreactCompat, PreactCompat) {}
customElements.define("preact-welcome", MyWelcome);
var fixture = document.getElementById("qunit-fixture");
var myWelcome = new MyWelcome();
fixture.appendChild(myWelcome);
return wait10ms().then(function() {
assert.equal(myWelcome.nodeName, "PREACT-WELCOME", "able to read nodeName");
assert.equal(myWelcome.childNodes.length, 1, "able to render something")
assert.equal(myWelcome.childNodes[0].innerHTML, "Hello, ", "renders the right thing");
myWelcome.name = "Justin";
return wait10ms();
}).then(function() {
assert.equal(myWelcome.childNodes[0].innerHTML, "Hello, Justin", "can update");
});
});
QUnit.test("works within can-stache and can-stache-bindings (propTypes are writable)", function(assert){
class Welcome extends React.Component {
render() {
return <h1>Hello, {
this.props.user.name
}</h1>;
}
}
Welcome.propTypes = {
user: PropTypes.object
};
class MyWelcome extends reactToWebComponent(Welcome, React, ReactDOM) {}
customElements.define("can-welcome", MyWelcome);
var view = stache("<can-welcome user:from='this.person'/>");
var frag = view({
person: {name: "Bohdi"}
});
var fixture = document.getElementById("qunit-fixture");
var myWelcome = frag.firstElementChild;
fixture.appendChild(frag);
return wait10ms().then(function() {
assert.equal(myWelcome.nodeName, "CAN-WELCOME", "able to read nodeName");
assert.equal(myWelcome.childNodes.length, 1, "able to render something")
assert.equal(myWelcome.childNodes[0].innerHTML, "Hello, Bohdi", "can update");
});
});
QUnit.test("works with nested properties of observable objects and arrays", function(assert) {
class Welcome extends React.Component {
render() {
return <React.Fragment>
<h1>
Hello, { this.props.name.full }
</h1>
<h1>
I see you like { this.props.hobbies.join(" and ")}
</h1>
</React.Fragment>;
}
}
class MyWelcome extends reactToWebComponent(Welcome, React, ReactDOM) {}
customElements.define("nested-props-welcome", MyWelcome);
var fixture = document.getElementById("qunit-fixture");
var myWelcome = new MyWelcome();
myWelcome.name = new (class extends ObservableObject {
get full() {
return this.first + ' ' + this.last;
}
})({
first: "Justin",
last: "Meyer",
});
myWelcome.hobbies = new ObservableArray([
"basketball",
"javascript"
]);
fixture.appendChild(myWelcome);
return wait10ms().then(function() {
assert.deepEqual(myWelcome.name.get(), { first: "Justin", last: "Meyer" });
assert.equal(myWelcome.nodeName, "NESTED-PROPS-WELCOME", "able to read nodeName");
assert.equal(myWelcome.childNodes.length, 2, "able to render something")
assert.equal(myWelcome.childNodes[0].innerHTML, "Hello, Justin Meyer", "renders the right thing");
assert.equal(myWelcome.childNodes[1].innerHTML, "I see you like basketball and javascript", "renders the right array");
myWelcome.name.first = "Ramiya";
// Note: myWelcome.hobbies[0] = "school" will not update the view as binding of list-likes
// listens for changes on the length.
myWelcome.hobbies.splice(1, 1, "school");
return wait10ms();
}).then(function() {
assert.equal(myWelcome.childNodes[0].innerHTML, "Hello, Ramiya Meyer", "can update object properties");
assert.equal(myWelcome.childNodes[1].innerHTML, "I see you like basketball and school", "can update array elements");
});
});
QUnit.test("subproperties update with can-stache and can-stache-bindings", function(assert){
class Welcome extends React.Component {
render() {
return <h1>Hello, {
this.props.user.name
}</h1>;
}
}
Welcome.propTypes = {
user: PropTypes.object
};
class MyWelcome extends reactToWebComponent(Welcome, React, ReactDOM) {}
customElements.define("can-welcome-ii", MyWelcome);
var view = stache("<can-welcome-ii user:from='this.person'/>");
var person = new ObservableObject({name: "Bohdi"});
var frag = view({
person
});
var fixture = document.getElementById("qunit-fixture");
var myWelcome = frag.firstElementChild;
fixture.appendChild(frag);
return wait10ms().then(function() {
assert.equal(myWelcome.nodeName, "CAN-WELCOME-II", "able to read nodeName");
assert.equal(myWelcome.childNodes.length, 1, "able to render something")
assert.equal(myWelcome.childNodes[0].innerHTML, "Hello, Bohdi", "can update");
person.name = "Cherif";
return wait10ms();
}).then(function() {
assert.equal(myWelcome.childNodes[0].innerHTML, "Hello, Cherif", "can update");
});
});
QUnit.test("sibling subcomponents only update for their own changes", function(assert){
class Welcome extends React.Component {
render() {
return <h1>Hello, {
this.props.name.first
}</h1>;
}
}
class Farewell extends React.Component {
render() {
return <h1>Goodbye, Mr. {
this.props.name.last
}</h1>;
}
}
class HelloGoodbye extends React.Component {
render() {
return <section>
<Welcome name={this.props.name} />
<Farewell name={this.props.name} />
</section>
}
}
class MyHelloGoodbye extends reactToWebComponent(HelloGoodbye, React, ReactDOM) {}
customElements.define("can-hello-goodbye", MyHelloGoodbye);
var myHelloGoodbye = new MyHelloGoodbye();
myHelloGoodbye.name = new ObservableObject({
first: "Justin",
last: "Meyer",
});
var fixture = document.getElementById("qunit-fixture");
fixture.appendChild(myHelloGoodbye);
return wait10ms().then(function() {
assert.equal(myHelloGoodbye.nodeName, "CAN-HELLO-GOODBYE", "able to read nodeName");
assert.equal(myHelloGoodbye.childNodes.length, 1, "able to render something");
assert.equal(myHelloGoodbye.firstElementChild.firstElementChild.innerHTML, "Hello, Justin", "can update");
assert.equal(myHelloGoodbye.firstElementChild.lastElementChild.innerHTML, "Goodbye, Mr. Meyer", "can update");
myHelloGoodbye.childNodes[0].firstElementChild.innerHTML = "Hello, Brad";
myHelloGoodbye.name.last = "Momberger";
return wait10ms();
}).then(function() {
assert.equal(myHelloGoodbye.firstElementChild.firstElementChild.innerHTML, "Hello, Brad", "doesn't rerender for no reason");
assert.equal(myHelloGoodbye.firstElementChild.lastElementChild.innerHTML, "Goodbye, Mr. Momberger", "rerenders on change");
});
});
QUnit.test("sibling wrapped components only update with their own changes", function(assert){
class Welcome extends React.Component {
render() {
return <h1>Hello, {
this.props.name.first
}</h1>;
}
}
Welcome.propTypes = {
name: PropTypes.object
};
class Farewell extends React.Component {
render() {
return <h1>Goodbye, Mr. {
this.props.name.last
}</h1>;
}
}
Farewell.propTypes = {
name: PropTypes.object
};
class MyWelcome extends reactToWebComponent(Welcome, React, ReactDOM) {}
customElements.define("can-welcome-iii", MyWelcome);
class MyFarewell extends reactToWebComponent(Farewell, React, ReactDOM) {}
customElements.define("can-farewell", MyFarewell);
var tmpl = stache(`
<can-welcome-iii name:from="this" />
<can-farewell name:from="this" />
`)
var vm = new ObservableObject({
first: "Justin",
last: "Meyer",
});
var frag = tmpl(vm);
var fixture = document.getElementById("qunit-fixture");
fixture.appendChild(frag);
var myWelcome;
var myFarewell;
return wait10ms().then(function() {
myWelcome = fixture.firstElementChild;
myFarewell = fixture.lastElementChild;
assert.equal(myWelcome.nodeName, "CAN-WELCOME-III", "able to read nodeName");
assert.equal(myWelcome.childNodes.length, 1, "able to render something");
assert.equal(myFarewell.nodeName, "CAN-FAREWELL", "able to read nodeName");
assert.equal(myFarewell.childNodes.length, 1, "able to render something");
assert.equal(myWelcome.firstElementChild.innerHTML, "Hello, Justin", "can update");
assert.equal(myFarewell.firstElementChild.innerHTML, "Goodbye, Mr. Meyer", "can update");
myWelcome.firstElementChild.innerHTML = "Hello, Brad";
vm.last = "Momberger";
return wait10ms();
}).then(function() {
assert.equal(myWelcome.firstElementChild.innerHTML, "Hello, Brad", "doesn't rerender for no reason");
assert.equal(myFarewell.firstElementChild.innerHTML, "Goodbye, Mr. Momberger", "rerenders on change");
});
});