-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathunit-testing.jsx
More file actions
412 lines (353 loc) · 13 KB
/
unit-testing.jsx
File metadata and controls
412 lines (353 loc) · 13 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// "use strict"; will automatically be added by both the jsxLoader or Babel
const isStrictMode = (function() { return !this; })();
// Make sure the compiler allows [//] comment characters with-in the 3 types of JavaScript Strings.
const links = [
'https://www.dataformsjs.com/en/',
"https://www.dataformsjs.com/pt-BR/",
`https://www.dataformsjs.com/zh-CN/`,
];
// Comments with data that would cause parsing errors are included to make sure they
// are removed and that the overall rendering is not affected.
// Test Comment: <Data>{test}
/*
Test <div>{test}
*/
{/*
Test <Test>{test}
*/}
// This should not cause a parsing error or unexpected data.
const testHtmlString = `${`'<div>test</div>'`}`
// Build data and make sure `< ` is handled in `for` loops:
const columns = [];
const records = [];
for (let n = 0; n < 5; n++) {
columns.push('Column ' + n);
}
for (let n = 0; n < 5; n++) {
const record = [];
for (let m = 0; m < 5; m++) {
record.push('R-' + n + ' C-' + m);
}
records.push({ values: record });
}
class HelloMessage extends React.Component {
render() {
return (
<div id={this.props.id}>
Hello {this.props.name}
</div>
);
}
}
function ShowTrue() {
return <span>true</span>
}
function ShowFalse() {
return <span>false</span>
}
const testNamespace = {
Hello: function() {
return <div className='test-namespace-1'>Hello from [testNamespace.Hello]</div>
}
};
const TestNamespace = {
Hello: function() {
return <div className="test-namespace-2">Hello from [TestNamespace.Hello]</div>
}
};
function Node(props) {
if (props.child) {
return <div id={props.id}><span>{String.fromCharCode(160).repeat(props.indent * 4)}{props.text}</span>{props.child}</div>;
}
return <div id={props.id}>
<span>{String.fromCharCode(160).repeat(props.indent * 4)}{props.text}</span> {props.children}
</div>;
}
function BlogPage(props) {
return <section className="blog">{props.children}</section>;
}
function VideoPage(props) {
return <section className="video">{props.children}</section>;
}
const pageTypes = {
blog: BlogPage,
video: VideoPage,
};
function Page(props) {
const PageComponent = pageTypes[props.pageType];
return <PageComponent>{props.children}</PageComponent>;
}
// Modified from: https://reactjs.org/docs/jsx-in-depth.html
function NumberDescriber(props) {
let description;
if (props.number % 2 == 0) {
description = <strong>even</strong>;
} else {
description = <i>odd</i>;
}
return <div id={props.id}>{props.number} is an {description} number</div>;
}
function DisplayUsers({users}) {
return (
<ul className="users">
{users.map(({id, name}) => {
return <li id={'user-' + id}>{name}</li>
})}
</ul>
);
}
function DisplayUsers2(props) {
return (
<ul className="users-2">
{[
<li id={'user2-' + props.users[0].id}>{props.users[0].name + ' Test'}</li>,
<li id={'user2-' + props.users[1].id}>{props.users[1].name + ' Test'}</li>
]}
</ul>
);
}
// Make sure <React.Fragment> shorthand <> is supported.
// For Older Browsers that use Babel this requires Babel 7 instead of Babel 6.
function ShorthandFragment() {
return (
<>
<div className="shorthand-fragment">Shorthand Fragment Check</div>
</>
);
}
// Included a 2nd time for a handled edge case error discovered when updating tests.
// Specifically a `state.elementCount > 0` check in `removeComments()`
function ShorthandFragment2() {
return (
<>
<div className="shorthand-fragment2">Shorthand Fragment Check 2</div>
</>
);
}
function Greeting(props) {
return <div className="greeting">{props.firstName} {props.lastName}</div>
}
function DisplayProps(props) {
const { id, ...other } = props;
other.message += ' test';
return <React.Fragment>
<div {...other} id="display-prop-test">{id + ' test'}</div>
<div {...props} >{id}</div>
</React.Fragment>;
}
function ShowLinks() {
return <ul className="links2">
<li>Test</li>
{links.map(link => {
return <li key={link}>{link}</li>
})}
</ul>;
}
// https://github.com/dataformsjs/dataformsjs/issues/19
const Issue19_Ob = (props) => {
return <div>{props.r()}</div>
}
const Issue19_App = () => {
return <div id="issue-19">
hello
<Issue19_Ob r={() => {
return <h1>zzzz</h1>
}}/>
</div>
}
function Issue20() {
const n = 10;
const m = 1<n;
const text = (1<n ? 'true': 'false');
const text2 = (1<n || 1 ? 'true2': 'false2');
const text3 = (1<n && 1 ? 'true3': 'false3');
const text4 = ((1<n) ? 'true4': 'false4');
return (
<div id="issue-20">{m ? 'yes': 'no'} {text} {text2} {text3} {text4}</div>
);
}
function Issue21() {
const items = ['Item1', 'Item2', 'Item3']
return (
<div id="issue-21">
<select>
<option value=""></option>
{items.map(item => <option value={item}>{item}</option>)}
</select>
</div>
);
}
class UnitTestPage extends React.Component {
render() {
const users = [
{ id:1, name:"User1" },
{ id:2, name:"User2" },
];
const user = {firstName: 'First', lastName: 'Last'};
const div10 = (123 <= 456 ? 'true' : 'false');
return (
<React.Fragment>
<h1>Unit Testing Content</h1>
<div className="is-strict-mode">use strict: {(isStrictMode ? 'true' : 'false')}</div>
<HelloMessage name="World" id="hello-1" />
<HelloMessage name="Test" id="hello-2"></HelloMessage>
<div id="div-1">{' '}<span>div-1</span></div>
<div id="div-2">{' '} <span>div-2</span></div>
<div id="div-3">{( <span>div-3</span>) }</div>
<div id="div-4"><span>Hello</span>{' '}<span>World</span></div>
<div id="div-5">{123} & {456} '</div>
<div id="div-6"> Hello World </div>
<div id="div-7"> Hello World </div>
<div id="div-8"> Hello World </div>
<div id="div-9" data-selector="ul.link > li">https://www.dataformsjs.com/</div>
<div id="div-10">{div10}</div>
<div id="div-11">{(123 <= 456 ? 'true' : 'false')}</div>
<ShowProps id="div-12"></ShowProps>
<ShowProps id="div-13" test></ShowProps>
<hr />
<hr></hr>
<hr/>
<ul className="links">
{links.map(link => {
// This specific comment is related to a fix for release 4.0.1 where
// support for JavaScript comments nested with-in elements was removed.
// This specific line would cause an error release 4.0.0. <li>
return (
<li key={link}>
<a href={link} target="_blank">{link}</a>
</li>
)
})}
</ul>
<ShowLinks />
{/*
Including [accept="image/*"] verifies that the [/*] inside of the string in [accept]
does not cause an issue when comments are removed.
*/}
<input id="file-1" type="file" accept="image/*" multiple />
<select>
<option value="">Empty</option>
<option value="item-1">Item 1</option>
</select>
<div id="test-html-string">{testHtmlString}</div>
{/*
Typically in JSX [className] is used for classes, however in general [class] should not causes issues.
This test confirms it and that elements can be used in the ternary operator (cond ? true : false)
*/}
<div className="expect-true">{1 === 1 ? <ShowTrue /> : <ShowFalse />}</div>
<div className="expect-false">{1 === 2 ? <ShowTrue /> : <ShowFalse />}</div>
<testNamespace.Hello />
<TestNamespace.Hello />
{links.length && <div className="link-count">
<span>{'Link Count: ' + links.length}</span>
</div>}
{links.length && <div className="link-count-2">
<span>Link Count 2: {links.length}</span>
</div>}
<table data-sort data-sort-class-odd="row-odd" data-sort-class-even="row-even">
<thead>
<tr>
{columns.map(column => {
return <th key={column}>{column}</th>
})}
</tr>
</thead>
<tbody className="click-to-highlight">
{records.map((record, index) => {
return (<tr key={index}>
{record.values.map((value, valueIndex) => {
return (<td key={valueIndex}>{value}</td>)
})}
</tr>)
})}
</tbody>
</table>
<section>
<Node
indent={0}
id="node-1"
text="node-1"
child={
<Node
indent={1}
id="node-2"
text="node-2"
child={<Node
indent={2}
id="node-3"
text="node-3">
<Node indent={3} id="node-4" text="node-4" />
<Node indent={3} id="node-5" text="node-5"></Node>
<Node indent={3} id="node-6" text="node-6">Test</Node>
<Node indent={3} id="node-7" text="node-7">Test{' Test '}Test{' Test'}</Node>
</Node>}
/>
} />
</section>
<Page pageType="blog">Blog</Page>
<Page pageType="video">{'Video'}</Page>
<NumberDescriber id="odd-number" number={1}></NumberDescriber>
<NumberDescriber id="even-number" number={2}></NumberDescriber>
<DisplayUsers users={users} />
<DisplayUsers2 users={users} />
<ShorthandFragment />
<ShorthandFragment2 />
<Greeting {...user} />
<DisplayProps id="display-prop-1" message="Hello World"></DisplayProps>
<Issue19_App />
<Issue20 />
<Issue21 />
</React.Fragment>
);
}
}
function ShowLoading() {
return <div className="loading">Loading...</div>;
}
function ShowError(props) {
return <div style={{backgroundColor:'red', color:'#fff'}}>{props.error}</div>;
}
function ShowMessage(props) {
return <div>{props.serverMessage}</div>
}
function ShowProps(props) {
return <div id={props.id}>{JSON.stringify(props)}</div>
}
function ShowData(props) {
return <>
<span>{props.data && props.data.serverMessage ? props.data.serverMessage : null}</span>
{props.data && props.data.serverMessage ?
<ShowMessage serverMessage={props.data.serverMessage} />
: null}
</>
}
function onViewUpdated() {
window.testJsonDataHtml = window.testJsonDataHtml || [];
window.testJsonDataHtml.push(document.querySelector('.test-content.json-data').innerHTML);
};
function onErrorViewUpdated() {
window.testJsonDataErrorHtml = window.testJsonDataErrorHtml || [];
window.testJsonDataErrorHtml.push(document.querySelector('.test-content.json-error').innerHTML);
// This line is not tested but helps verify that `updateView()` uses `try { ... } catch (e) { ... }`
throw new Error('This should not affect [JsonData]');
};
function TestJsonDataSuccess() {
return (
<JsonData
url="/unit-testing/page-json-data"
isLoading={<ShowLoading />}
hasError={<ShowError />}
isLoaded={<ShowData />}
onViewUpdated={onViewUpdated} />
)
}
function TestJsonDataError() {
return (
<JsonData
url="/404"
isLoading={<ShowLoading />}
hasError={<ShowError />}
isLoaded={<ShowData />}
onViewUpdated={onErrorViewUpdated} />
)
}