This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
react-wc-tests.js
284 lines (241 loc) · 8.48 KB
/
react-wc-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
273
274
275
276
277
278
279
280
281
282
283
284
import camelCase from 'camel-case';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils from 'react-addons-test-utils';
import reactify from '../src/index';
import skate from 'skatejs';
import helperElement from 'skatejs/test/lib/element';
function afterMutations (fn) {
setTimeout(fn, 100);
}
describe('react-integration;', function () {
let fixture;
beforeEach(function () {
fixture = document.createElement('div');
document.body.appendChild(fixture);
});
afterEach(function () {
ReactDOM.unmountComponentAtNode(fixture);
fixture.innerHTML = '';
document.body.removeChild(fixture);
});
function getWebComponent (tagName) {
return document.querySelector(tagName);
}
function wrapInReact (webComponentConstructor) {
return reactify(webComponentConstructor, {
React: React,
ReactDOM: ReactDOM
});
}
function renderApp(App) {
App.displayName = 'App';
return ReactDOM.render(<App />, fixture);
}
describe('(React > React wrapped terminal web component);', function () {
describe('that has an attribute;', function () {
function makeReactComponent (tagName) {
const webComponentConstructor = skate(tagName, {
render: function (elem) {
elem.innerHTML = '<em><input />' + elem.text + '</em>';
},
properties: {
text: {
attribute: true,
set: function (elem) {
skate.render(elem);
}
}
}
});
return wrapInReact(webComponentConstructor);
}
const tagName = helperElement('x-terminal-text').safe;
const ReactComponent = makeReactComponent(tagName);
it('the web component renders correctly', function () {
renderApp(
React.createClass({
render: function () {
return <ReactComponent/>;
}
})
);
expect(getWebComponent(tagName).firstChild.tagName).to.equal('EM');
});
it('the react component renders props through the attribute', function () {
const webComponentText = 'terminal component';
renderApp(
React.createClass({
render: function () {
return <ReactComponent text={webComponentText}/>;
}
})
);
expect(getWebComponent(tagName).firstChild.textContent).to.equal(webComponentText);
});
it('changing the state changes the web component attribute', function () {
var renderedApp = renderApp(
React.createClass({
getInitialState: function () {
return {
text: 'initial text'
};
},
render: function () {
return <ReactComponent text={this.state.text}/>;
}
})
);
expect(getWebComponent(tagName).firstChild.textContent).to.equal('initial text');
renderedApp.setState({
text: 'updated text'
});
expect(getWebComponent(tagName).firstChild.textContent).to.equal('updated text');
});
it('the react component\'s display is the camel cased version of the tag name', function () {
expect(camelCase(tagName)).to.equal(ReactComponent.displayName);
});
});
describe('that has a property but not an attribute;', function () {
function makeReactComponent (tagName) {
const webComponentConstructor = skate(tagName, {
render: function (elem) {
elem.innerHTML = '<div></div>';
if (elem.hidden) {
elem.firstChild.setAttribute('aria-hidden', '');
} else {
elem.firstChild.removeAttribute('aria-hidden');
}
},
attached: function (elem) {
elem._hidden_on_attach = elem.hidden;
},
properties: {
hidden: {
set: function (elem) {
skate.render(elem);
}
}
}
});
return wrapInReact(webComponentConstructor);
}
const tagName = helperElement('x-terminal-text').safe;
const ReactComponent = makeReactComponent(tagName);
it('the web component renders props through the property', function () {
renderApp(
React.createClass({
render: function () {
return <ReactComponent hidden={true}/>;
}
})
);
expect(getWebComponent(tagName).firstChild.hasAttribute('aria-hidden')).to.equal(true);
});
it('the web component properties are available on attach', function (done) {
renderApp(
React.createClass({
render: function () {
return <ReactComponent hidden={true}/>;
}
})
);
afterMutations(function () {
expect(getWebComponent(tagName)._hidden_on_attach).to.equal(true);
done();
});
});
});
});
describe('(React > React wrapped container web component', function () {
function isReactRoot (elem) {
return elem.hasAttribute('data-reactid') && elem.getAttribute('data-reactid').split('.').length === 2;
}
function makeReactComponent (tagName) {
var webComponentConstructor = skate(tagName, {
render: function (elem) {
elem.innerHTML = '<div class="initial-content"></div>';
}
});
return wrapInReact(webComponentConstructor);
}
const tagName = helperElement('x-terminal-text').safe;
const ReactComponent = makeReactComponent(tagName);
it('> DOM element): content is rendered directly to the container', function () {
renderApp(
React.createClass({
render: function () {
return (
<ReactComponent>
<em id="user-inserted-contents">User inserted contents</em>
</ReactComponent>
);
}
})
);
const componentContents = document.getElementById('user-inserted-contents');
const reactRoot = componentContents.parentNode;
expect(ReactTestUtils.isDOMComponent(componentContents)).to.equal(true, 'component contents');
expect(isReactRoot(reactRoot)).to.equal(true, 'react root');
expect(reactRoot.parentNode.tagName.indexOf('-')).to.not.equal(-1, 'web component');
});
it('> DOM element): properties are passed to the wrapped React component', function () {
var renderedApp = renderApp(
React.createClass({
render: function () {
return (
<ReactComponent>
<em id="user-inserted-contents">{this.props.text}</em>
</ReactComponent>
);
}
})
);
renderedApp.setProps({text: 'initial text'});
expect(document.getElementById('user-inserted-contents').textContent).to.equal('initial text');
renderedApp.setProps({text: 'updated text'});
expect(document.getElementById('user-inserted-contents').textContent).to.equal('updated text');
});
it('> React component): properties flow down into the child React component', function () {
const ContainedReactComponent = React.createClass({
render: function () {
return <em id="user-inserted-contents">{this.props.text}</em>;
}
});
var renderedApp = renderApp(
React.createClass({
render: function () {
return (
<ReactComponent>
<ContainedReactComponent text={this.props.text}/>
</ReactComponent>
);
}
})
);
renderedApp.setProps({text: 'initial text'});
expect(document.getElementById('user-inserted-contents').textContent).to.equal('initial text');
renderedApp.setProps({text: 'updated text'});
expect(document.getElementById('user-inserted-contents').textContent).to.equal('updated text');
});
it('> DOM element): events triggered on the DOM element can be handled on click', function () {
var clicked = false;
function handleClick () {
clicked = true;
}
renderApp(
React.createClass({
render: function () {
return (
<ReactComponent>
<em onClick={handleClick} id="user-inserted-contents">Click me</em>
</ReactComponent>
);
}
})
);
ReactTestUtils.Simulate.click(document.getElementById('user-inserted-contents'));
expect(clicked).to.equal(true);
});
});
});