-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjCreateTests.js
More file actions
591 lines (536 loc) · 14.8 KB
/
Copy pathjCreateTests.js
File metadata and controls
591 lines (536 loc) · 14.8 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/// <reference path="../../jcreate.js" />
module('tag');
test("Use tag: 'div' to create a <div>.",
function()
{
var source = { tag: 'div' },
result = jCreate(source);
equal(result.outerHTML, '<div></div>', 'Should create an empty div element.');
}
);
test("Use tag: 'span' to create a <span>.",
function()
{
var source = { tag: 'span' },
result = jCreate(source);
equal(result.outerHTML, '<span></span>', 'Should create an empty span element.');
}
);
test("Can use 'nodeName' instead of 'tag'.",
// The use of 'attr' is consistent with DOM.
function()
{
var source = { nodeName: 'span' },
result = jCreate(source);
equal(result.outerHTML, '<span></span>', 'Should create an empty span element.');
}
);
test("Can use 'tagName' instead of 'tag'.",
// The use of 'attr' is consistent with DOM.
function()
{
var source = { tagName: 'span' },
result = jCreate(source);
equal(result.outerHTML, '<span></span>', 'Should create an empty span element.');
}
);
test("Create div when tag is null.",
function()
{
var source = { tag: null },
result = jCreate(source);
equal(result.outerHTML, '<div></div>', 'Should create an empty div element.');
}
);
module("text");
test("Create text node from string 'test'.",
function()
{
var result = jCreate('test');
equal(result.nodeType, 3, 'Should return a text node (nodeType = 3).');
equal(result.nodeValue, 'test', "nodeValue should be 'test'.");
}
);
test("Create text node from string '<span>test</span>'.",
function()
{
var result = jCreate('<span>test</span>');
equal(result.nodeType, 3, 'Should return a text node (nodeType = 3), not a span element.');
equal(result.nodeValue, '<span>test</span>', "nodeValue should be '<span>test</span>'.");
}
);
module("properties");
test("Use id to set the id.",
function()
{
var source = { id: 'div100' },
result = jCreate(source);
equal(result.outerHTML, '<div id="div100"></div>', 'Should create a div element with the id.');
}
);
test("Use className to set the CSS class.",
function()
{
var source = { className: 'test' },
result = jCreate(source);
equal(result.outerHTML, '<div class="test"></div>', 'Should create a div element with the CSS class.');
}
);
test("Use value to set the value of an input control.",
function()
{
var source = { tag: 'input', type:'textbox', value: 'enter a value' },
result = jCreate(source);
equal(result.outerHTML, '<input type="textbox">', 'Should create a textbox.');
equal(result.value, source.value, 'Should have the correct value.');
}
);
test("Use any property name to set the property.",
function()
{
var source =
{
prop1: 1,
'var': 'can even use a reserved word'
},
result = jCreate(source);
equal(result.outerHTML, '<div></div>', 'Should create an empty div.');
equal(result.prop1, source.prop1, 'Should have value for prop1.');
ok(result.prop1 === source.prop1, 'Should have exact value for prop1 (same data type).');
equal(result['var'], source['var'], 'Should have value for \'var\' property.');
}
);
module("attributes");
test("Use an attributes object to set DOM attributes.",
function()
{
var source =
{
tag: 'textarea',
attributes: { rows: 10, cols: 60 }
},
result = jCreate(source);
equal(result.tagName, 'TEXTAREA', 'Should create a textarea.');
equal(result.getAttribute('rows'), source.attributes.rows, 'Should have a rows attribute.');
equal(result.getAttribute('cols'), source.attributes.cols, 'Should have a cols attribute.');
}
);
test("The attributes object can be used to set unknown (expando) attributes too.",
function()
{
var source =
{
attributes: { prop1: 1, 'var': 'can even use a reserved word' }
},
result = jCreate(source);
equal(result.getAttribute('prop1'), source.attributes.prop1, 'Should have a prop1 attribute.');
equal(result.getAttribute('var'), source.attributes['var'], 'Should have a var attribute.');
}
);
test("Can use 'attr' instead of 'attributes'.",
// The use of 'attr' is consistent with jQuery.
function()
{
var source =
{
tag: 'textarea',
attr: { rows: 10, cols: 60 }
},
result = jCreate(source);
equal(result.tagName, 'TEXTAREA', 'Should create a textarea.');
equal(result.getAttribute('rows'), source.attr.rows, 'Should have a rows attribute.');
equal(result.getAttribute('cols'), source.attr.cols, 'Should have a cols attribute.');
}
);
module("content");
test("Use content property to set the text content.",
function()
{
var source = { tag: 'h1', content: 'Mergers & Acquisitions' },
result = jCreate(source);
equal(result.outerHTML, '<h1>Mergers & Acquisitions</h1>', 'Should create an h1 element with text content.');
}
);
test("Use content property to include a single child node.",
function()
{
var source =
{
className: 'outer',
content: { className: 'inner' }
},
result = jCreate(source);
equal(result.outerHTML, '<div class="outer"><div class="inner"></div></div>', 'Should create an inner div nested in an outer div.');
}
);
test("Use content array to include multiple child nodes.",
function()
{
var source =
{
tag: 'ul',
content:
[
{ tag: 'li' },
{ tag: 'li' },
{ tag: 'li' },
]
},
result = jCreate(source);
equal(result.outerHTML, '<ul><li></li><li></li><li></li></ul>', 'Should create a ul with 3 li');
}
);
test("Can use 'childNodes' instead of 'content'.",
// The use of 'childNodes' is consistent with DOM.
function()
{
var source =
{
tag: 'ul',
content:
[
{ tag: 'li' },
{ tag: 'li' },
{ tag: 'li' },
]
},
result = jCreate(source);
equal(result.outerHTML, '<ul><li></li><li></li><li></li></ul>', 'Should create a ul with 3 li');
}
);
test("Use nested content to create a DOM tree.",
function()
{
var source =
{
tag: 'table',
content:
[
{
tag: 'tr',
content :
[
{ tag: 'td', content: 'A1' },
{ tag: 'td', content: 'B1' }
]
},
{
tag: 'tr',
content:
[
{ tag: 'td', content: 'A2' },
{ tag: 'td', content: 'B2' }
]
},
]
},
result = jCreate(source);
equal(result.outerHTML, '<table><tr><td>A1</td><td>B1</td></tr><tr><td>A2</td><td>B2</td></tr></table>', 'Should create a table with 2 rows and 2 columns.');
}
);
test("Can use content that already exists in document.",
function()
{
var $fixture = $('#qunit-fixture'),
$testContent = $('<div id="testContent">TestContent</div>'),
source,
result;
$fixture.append($testContent);
source =
{
tag: 'div',
content: document.getElementById('testContent'),
};
result = jCreate(source);
equal(result.outerHTML, '<div><div id="testContent">TestContent</div></div>', 'Should create a div with the existing content.');
ok(!jQuery.contains($fixture[0], $testContent[0]), 'Should remove the content from where it was in the document.');
}
);
// var $fixture = $( "#qunit-fixture" );
module("style");
test("Use style object to set CSS properties.",
function()
{
var source =
{
style: { width: '400px', height: '300px' }
},
result = jCreate(source);
equal(result.style.width, source.style.width, 'Should set width: 400px.');
equal(result.style.height, source.style.height, 'Should set height: 300px.');
}
);
test("Can set style using a string.",
function()
{
var source =
{
style: "width: 400px; height: 300px"
},
result = jCreate(source);
equal(result.style.width, '400px', 'Should set width: 400px.');
equal(result.style.height, '300px', 'Should set height: 300px.');
}
);
test("Can use DOM CSS property names (e.g. cssFloat, borderColor).",
// See http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-extended
function()
{
var source =
{
style: { cssFloat: 'left', borderColor:'black' }
},
result = jCreate(source);
equal(result.style.cssFloat, source.style.cssFloat, 'Should set float: left.');
equal(result.style.borderColor, 'black', 'Should set border-color: black.');
}
);
test("Can use CSS property names (e.g. 'float', 'border-color').",
function()
{
var source =
{
style: { 'float': 'left', 'border-color': 'black' }
},
result = jCreate(source);
equal(result.style.cssFloat, source.style['float'], 'Should set float: left.');
equal(result.style.borderColor, 'black', 'Should set border-color: black.');
}
);
test("Can use 'css' instead of 'style'.",
// The use of 'css' is consistent with DOM.
function()
{
var source =
{
css: { width: '400px', height: '300px' }
},
result = jCreate(source);
equal(result.style.width, source.css.width, 'Should set width: 400px.');
equal(result.style.height, source.css.height, 'Should set height: 300px.');
}
);
module('events');
test("Use events object to add event handlers.",
function()
{
var message = 'click event did not fire',
source =
{
events:
{
click: function(e)
{
message = 'click event fired';
}
}
},
result = jCreate(source);
// Trigger the click event.
jQuery(result).click();
equal(message, 'click event fired', 'Should set the click event.');
}
);
module("fragment");
test("Create an HTML document fragment from an array.",
function()
{
var div = document.createElement('div'),
source =
[
{ tag: 'h1' },
{ tag: 'p' },
{ tag: 'h2' },
{ tag: 'p' }
],
result = jCreate(source);
equal(result.nodeType, 11, 'Should return an HTML document fragment (nodeType = 11).');
// Document fragments don't have innerHTML or outerHTML properties.
// Add the fragment to an empty div and use div.innerHTML to verify the contents of the fragment.
div.appendChild(result);
equal(div.innerHTML, '<h1></h1><p></p><h2></h2><p></p>', 'Should contain elements: h1, p, h2, p.');
}
);
test("Create an HTML document fragment from array of length one.",
// Note that jCreate does not attempt to "optimize" this and return
// fragment's single child node. An array as input always returns a fragment
// and callers can depend on that behavior.
function()
{
var div = document.createElement('div'),
source = [{ tag: 'span' }],
result = jCreate(source);
equal(result.nodeType, 11, 'Should return an HTML document fragment (nodeType = 11).');
// Document fragments don't have innerHTML or outerHTML properties.
// Add the fragment to an empty div and use div.innerHTML to verify the contents of the fragment.
div.appendChild(result);
equal(div.innerHTML, '<span></span>', 'Should contain a span.');
}
);
test("Create an HTML document fragment from an empty array.",
function()
{
var div = document.createElement('div'),
source = [],
result = jCreate(source);
equal(result.nodeType, 11, 'Should return an HTML document fragment (nodeType = 11).');
// Document fragments don't have innerHTML or outerHTML properties.
// Add the fragment to an empty div and use div.innerHTML to verify the contents of the fragment.
div.appendChild(result);
equal(div.innerHTML, '', 'Should be empty.');
}
);
test("Ignore null array entries.",
function()
{
var div = document.createElement('div'),
source =
[
{ tag: 'h2' },
null, // This null is ignored -- it doesn't cause an error or create an empty div.
{ tag: 'h2' }
],
result = jCreate(source);
equal(result.nodeType, 11, 'Should return an HTML document fragment (nodeType = 11).');
// Document fragments don't have innerHTML or outerHTML properties.
// Add the fragment to an empty div and use div.innerHTML to verify the contents of the fragment.
div.appendChild(result);
equal(div.innerHTML, '<h2></h2><h2></h2>', 'Should contain two h2 elements.');
}
);
module('init');
test("The init function is called when node is created.",
function()
{
var message = 'init function not called',
source =
{
init: function(e)
{
message = 'init function called';
}
},
result = jCreate(source);
equal(message, 'init function called', 'Should call the init function.');
}
);
test("The init function is called when node is created.",
function()
{
var message = 'init function not called',
source =
{
init: function()
{
message = 'init function called';
}
},
result = jCreate(source);
equal(message, 'init function called', 'Should call the init function.');
}
);
test("The init function is called with a source argument.",
function()
{
var sourceResult,
sourceExpected =
{
init: function(source)
{
// Capture the value of the source arg
sourceResult = source;
}
},
result = jCreate(sourceExpected);
ok(sourceResult === sourceExpected, 'Source argument of init function should be same as source object used to create the node.');
}
);
test("The init function is called using created node as 'this'.",
function()
{
var thisResult,
source =
{
init: function()
{
// Capture the value of 'this'
thisResult = this;
}
},
result = jCreate(source);
ok(result === thisResult, "init function should be called using created node as 'this'.");
}
);
test("The init function is called using created node as 'this'.",
function()
{
var tags = [],
outerHTML,
init = function(source)
{
tags.push(source.tag);
outerHTML = this.outerHTML;
},
source =
{
tag: 'div',
init: init,
content:
{
tag: 'span',
init: init
}
},
result = jCreate(source);
equal(tags.join(', '), 'span, div', 'Should init child (span) before parent (div).');
equal(outerHTML, '<div><span></span></div>', 'Should add child (span) to parent (div) before init.');
}
);
module("misc");
test("Create div from new (empty) object.",
function()
{
var result = jCreate({});
equal(result.outerHTML, '<div></div>', 'Should create an empty div element.');
}
);
test("Return null when input is null.",
function()
{
var result = jCreate(null);
equal(result, null, 'Should return null.');
}
);
test("Return the DOM node when input is a DOM node.",
function()
{
var source = document.createElement('p'),
result = jCreate(source);
ok(source === result, 'Should return the same DOM node.');
}
);
test("Return the same object if it has a nodeType property.",
// Don't use the nodeType property because jCreate will assume
// the object is a DOM node and will return it without creating a DOM node.
// This isn't much of an issue because the nodeType property of a DOM node
// is read-only so users are unlikely to attemp to set it.
function()
{
var source = { nodeType: 1 },
result = jCreate(source);
ok(source === result, 'Should return the same JavaScript object.');
}
);
test("Ignore prototype properties.",
function()
{
var SourceObject = function(tag) { this.tag = tag; },
source,
result;
SourceObject.prototype.baseProperty = true;
source = new SourceObject('span');
result = jCreate(source);
ok(source.baseProperty, "source should have a 'baseProperty'.");
ok(typeof result.baseProperty == 'undefined', "result should not have a 'baseProperty'.");
}
);