-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathunit-testing.js
More file actions
2970 lines (2642 loc) · 159 KB
/
unit-testing.js
File metadata and controls
2970 lines (2642 loc) · 159 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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* DataFormsJS Unit Testing
*
* This is the main file for testing the Framework.
*
* Unit Testing runs in the Browser using QUnit.
* See instructions in [../server.js].
*
* NOTE - Vue was added several years after this file was created
* and because the behavior of Vue is different from Templating (Handlebars, etc)
* many tests and checks have to be skipped for Vue. Because many tests
* are skipped it's important that demos be manually tested when
* updates are made and when new versions of Vue are released.
*/
/* Validates with both [jshint] and [eslint] */
/* global QUnit, app, DataFormsJS, tester, nunjucks, Vue */
/* jshint strict: true */
/* eslint-env browser */
/* eslint quotes: ["error", "single", { "avoidEscape": true }] */
/* eslint strict: ["error", "function"] */
/* eslint spaced-comment: ["error", "always"] */
/* eslint no-console: ["error", { allow: ["log", "warn", "error"] }] */
/* eslint no-prototype-builtins: "off" */
(function () {
'use strict';
var isIE = (navigator.userAgent.indexOf('Trident/') !== -1);
var isFirefox = (navigator.userAgent.indexOf('Firefox/') !== -1);
document.addEventListener('DOMContentLoaded', function () {
// Default expected DataFormJS Settings based on the current page
// Controllers and Plugins are variable which allows for different
// builds to be tested. The required pages/plugins are checked at setup.
tester.controllersCount = 19;
tester.modelsCount = 5;
tester.pagesCount = Object.keys(app.pages).length; // min=2
tester.pluginsCount = Object.keys(app.plugins).length; // min=1
tester.compiledTemplates = 1;
tester.submittedRequestCount = 0;
// Run tests in the order that they are defined
QUnit.config.reorder = false;
QUnit.test('DataFormsJS Version, Settings, Automatic Setup, and Initial View Rendering Test', function (assert) {
assert.ok(app === DataFormsJS, 'Global Variables [app] and [DataFormsJS] are Defined');
assert.equal(app.settings.viewSelector, '#view', 'Default Settings for app.settings.viewSelector: ' + app.settings.viewSelector);
assert.equal(app.settings.defaultRoute, '/', 'Default Settings for app.settings.defaultRoute: ' + app.settings.defaultRoute);
assert.equal(app.settings.logFetchRequests, false, 'Default Settings for app.settings.logFetchRequests: ' + app.settings.logFetchRequests);
assert.deepEqual(app.settings.requestHeaders, {}, 'Default Settings for app.settings.requestHeaders: ' + JSON.stringify(app.settings.requestHeaders));
assert.deepEqual(app.settings.requestHeadersByHostName, {}, 'Default Settings for app.settings.requestHeadersByHostName: ' + JSON.stringify(app.settings.requestHeadersByHostName));
assert.equal(app.settings.errors.pageLoading, 'Error loading the current page because the previous page is still loading and is taking a long time. Please refresh the page and try again.', 'Default settings for app.settings.errors.pageLoading: ' + app.settings.errors.pageLoading);
assert.equal(Object.keys(app.settings).length, 12, 'Number of properties in app.settings');
var version = app.version.match(/^5.\d+.\d+$/);
assert.ok(version !== null, 'app.version is major version 5, full version: ' + version);
switch (app.viewEngine()) {
case 'Handlebars':
case 'Nunjucks':
case 'Underscore':
case 'Vue':
assert.ok(true, 'app.viewEngine() - ' + app.viewEngine());
break;
default:
assert.ok(false, 'app.viewEngine() - ' + app.viewEngine());
}
switch (app.viewEngine()) {
case 'Handlebars':
tester.controllersCount++;
break;
case 'Nunjucks':
tester.controllersCount += 2;
break;
default:
break;
}
tester.setup();
tester.checkCounts(assert);
// Required Pages/Plugins
// Tests will fail if required files are not included in the build
assert.ok(app.pages.jsonData !== undefined, 'Checking for required page: app.pages.jsonData');
assert.ok(app.pages.unitTestEventOrder !== undefined, 'Checking for required page: app.pages.unitTestEventOrder');
assert.ok(app.plugins.unitTestEventOrder !== undefined, 'Checking for required plugin: app.plugins.unitTestEventOrder');
var text = document.getElementById('view').textContent.trim();
var expectedText = 'Initial view has loaded';
assert.equal(text, expectedText, 'Checking for Initial Template: ' + text);
assert.equal(app.activeController.settings.content, expectedText, 'app.activeController.settings.content: ' + app.activeController.settings.content);
var template0 = document.querySelector('#template0[data-route="/"]');
var isValid = (template0 !== null && template0.tagName === 'SCRIPT');
assert.ok(isValid, 'Checking assigned Template ID');
assert.equal(template0.getAttribute('data-content'), expectedText, 'Template [data-content]: ' + template0.getAttribute('data-content'));
// Verify that [app.viewEngine()] is being determined by <script [type="*"]> only.
// This is done because each of the pages that call this file use one one type of
// view engine but include all View Engines on the page
assert.ok((window.Handlebars !== undefined), 'Including Handlebars on this page');
assert.ok((window.nunjucks !== undefined), 'Including Nunjucks on this page');
assert.ok((window._ !== undefined), 'Including Underscore on this page');
// Error Default Properties
var expectedCss = '.dataformsjs-error,.dataformsjs-fatal-error{color:#fff;background-color:red;box-shadow:0 1px 5px 0 rgba(0,0,0,.5);background-image:linear-gradient(#e00,#c00);/*white-space:pre;*/text-align:left;}.dataformsjs-error{padding:10px;font-size:1em;margin:5px;display:inline-block;}.dataformsjs-fatal-error{z-index:1000000;padding:20px;font-size:1.5em;margin:20px;position:fixed;top:10px;}@media only screen and (min-width:1000px){.dataformsjs-fatal-error{max-width:1000px;left:calc(50% - 520px);}}.dataformsjs-fatal-error span{padding:5px 10px;float:right;border:1px solid darkred;cursor:pointer;margin-left:10px;box-shadow:0 0 2px 1px rgba(0,0,0,0.3);background-image:linear-gradient(#c00,#a00);border-radius:5px;}';
assert.equal(app.errorClass, 'dataformsjs-error', 'Default Settings for app.errorClass: ' + app.errorClass);
assert.equal(app.fatalErrorClass, 'dataformsjs-fatal-error', 'Default Settings for app.fatalErrorClass: ' + app.fatalErrorClass);
assert.equal(app.errorStyleId, 'dataformsjs-style-errors', 'Default Settings for app.errorStyleId: ' + app.errorStyleId);
assert.equal(app.errorCss, expectedCss, 'Default Settings for app.errorClass: ' + app.errorCss);
// Test setup() when a script with the attribute [data-route] is defined but the value is missing
// Normally this would be a fatal error which is why the script type is defined and re-defined after testing.
var expectedMessage = 'TypeError: A script element <script id="template-invalid-path" type="text/x-template" data-engine="null"> has the attribute [data-route] defined however the attribute value is empty. This error must be fixed, scripts that include a route path attribute must have the value defined.';
var script = document.getElementById('template-invalid-path');
var startingScriptType = script.type;
script.type = 'text/x-template';
app.setup();
// Check error alert and close it
var div = document.querySelector('.dataformsjs-fatal-error');
assert.ok(div.childNodes[1].textContent, expectedMessage);
div.querySelector('span').click();
script.type = startingScriptType;
});
QUnit.test('Check Polyfills from [js/scripts/polyfills.js]', function (assert) {
assert.equal('\t\n test '.trimLeft(), 'test ', 'String.prototype.trimLeft()');
assert.equal('\t\n test '.trimStart(), 'test ', 'String.prototype.trimStart()');
assert.equal(' test \t\n'.trimRight(), ' test', 'String.prototype.trimRight()');
assert.equal(' test \t\n'.trimEnd(), ' test', 'String.prototype.trimEnd()');
});
QUnit.test('Redirect with [app.settings.defaultRoute] with [/404] Route', function (assert) {
// Asynchronous test
var done = assert.async();
// Define hash to a view that doesn't exist
var hash = '#/404';
// Make sure that content get's overwritten on the redirect
var html = 'This will be overwritten';
document.getElementById('view').innerHTML = html;
tester.checkElement('view', html, assert);
// Define a Global App Function to check the page after it is rendered
app.onUpdateViewComplete = function () {
// Check the URL Hash and View Contents
var expectedHtml = 'Initial view has loaded';
assert.equal(window.location.hash, '#' + app.settings.defaultRoute, 'Current URL Hash [' + window.location.hash + '] after setting [' + hash + ']');
tester.checkElement('view', expectedHtml, assert);
// Reset Global App Function
app.onUpdateViewComplete = null;
// Mark the test as complete
done();
};
// Change the hash
window.location.hash = hash;
});
// Test when controller/script for [app.settings.defaultRoute] doesn't exist
QUnit.test('Redirect with [app.settings.defaultRoute] with missing [/500] Route', function (assert) {
// Asynchronous test
var done = assert.async();
// These don't exist
var hash = '#/404';
app.settings.defaultRoute = '/500';
// Define a custom hash change event because this type unexpected
// error can't be handled from app routing functions.
function hashChange() {
// Check results
var expected = 'Error - The route [/404] does not have a <script data-route> element or [Controller] defined. Check to make sure that a controller or script for default route [/500] exists.';
assert.equal(window.location.hash, hash, 'Current URL Hash [' + window.location.hash + '] after setting [' + hash + ']');
assert.equal(app.settings.defaultRoute, '/500', 'Current Default Route: ' + app.settings.defaultRoute);
tester.checkElement('view', expected, assert);
// Reset
app.settings.defaultRoute = '/';
window.removeEventListener('hashchange', hashChange);
document.head.removeChild(document.getElementById('dataformsjs-style-errors')); // Created from the error
// Mark the test as complete
done();
}
// Change the hash
window.addEventListener('hashchange', hashChange);
window.location.hash = hash;
});
QUnit.test('Render a <template> View and check properties', function (assert) {
var done = assert.async();
var hash = '#/template-view';
var expectedHtml = 'Template View';
var shouldCompileTemplate = true;
tester.pageTester(hash, shouldCompileTemplate, expectedHtml, assert, done, function () {
// script[data-engine="text"] is for IE
var type = app.getTemplateType(document.querySelector('template[data-route="/template-view"],script[data-engine="text"][data-route="/template-view"]'));
assert.equal(type, 'Text', 'app.getTemplateType(<template>): ' + type);
});
});
QUnit.test('app.routeMatches() Validation', function (assert) {
var routes = [
{
path: '/page1',
routePath: '/page2',
expected: { isMatch: false }
},
{
path: '/show-all',
routePath: '/show-all',
expected: { isMatch: true, args: [], namedArgs: {} }
},
{
path: '/record/123',
routePath: '/record/:id',
expected: { isMatch: true, args: ['123'], namedArgs: { id: '123'} }
},
{
path: '/orders/edit/123',
routePath: '/:record/:view/:id',
expected: {
isMatch: true,
args: ['orders', 'edit', '123'],
namedArgs: {
record: 'orders',
view: 'edit',
id: '123'
}
}
}
];
routes.forEach(function (route) {
var result = app.routeMatches(route.path, route.routePath);
assert.deepEqual(route.expected, result, 'Comparing [' + route.path + '] to [' + route.routePath + ']');
});
});
QUnit.test('app.deepClone() Validation', function (assert) {
var expectedMessage,
expectedResult,
result,
obj1,
obj2,
obj3,
dateValue;
try {
expectedMessage = 'No arguments specified when app.deepClone() was called.';
app.deepClone();
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = 'No arguments specified when app.deepClone() was called.';
app.deepClone();
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = 'Only one argument was specified when app.deepClone() was called.';
app.deepClone({});
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[argument] was not defined as a object when the function app.deepClone() was called';
app.deepClone({}, '');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
expectedResult = { name: 'Conrad' };
obj1 = { name: 'Conrad' };
result = app.deepClone({}, obj1);
assert.deepEqual(expectedResult, result, 'app.deepClone() Test 1');
expectedResult = { name: 'Conrad', numberValue: 123 };
obj1 = { name: 'Conrad' };
obj2 = { numberValue: 123 };
result = app.deepClone({}, obj1, obj2);
assert.deepEqual(expectedResult, result, 'app.deepClone() Test 2');
expectedResult = { name: 'Conrad', numberValue: 456 };
obj1 = { name: 'Conrad' };
obj2 = { numberValue: 123 };
obj3 = { numberValue: 456 };
result = app.deepClone(obj1, obj2, obj3);
assert.deepEqual(expectedResult, result, 'app.deepClone() Test 3');
expectedResult = { name: 'Conrad' };
obj1 = { name: 'Conrad' };
result = app.deepClone(obj1, obj1);
assert.deepEqual(expectedResult, result, 'app.deepClone() Test 4 - Copy object to itself');
dateValue = new Date();
expectedResult = {
type: 'Parent Class',
childObj: {
prop1: 'Value1',
prop2: 'Value2'
},
arrayData: [
{ index: 1 },
{ index: 2 },
{ index: 3 }
],
dateValue: dateValue
};
obj1 = {
type: 'Parent Class',
childObj: {
prop1: 'Value1',
prop2: 'Value2'
},
arrayData: [
{ index: 1 },
{ index: 2 },
{ index: 3 }
],
dateValue: dateValue
};
result = app.deepClone({}, obj1);
assert.deepEqual(expectedResult, result, 'app.deepClone() Test 5 - Deep Copy');
// Make changes to the copied item
result.type = 'Updated';
result.dateValue = new Date(2015, 0, 1);
result.childObj.prop1 = 'abc';
result.arrayData[0].index = 0;
// Make sure the original item was not updated
assert.equal(obj1.type, 'Parent Class', 'Checking result of deep copy - obj1.type');
assert.equal(obj1.dateValue, dateValue, 'Checking result of deep copy - obj1.dateValue');
assert.equal(obj1.childObj.prop1, 'Value1', 'Checking result of deep copy - obj1.childObj.prop1');
assert.equal(obj1.arrayData[0].index, 1, 'Checking result of deep copy - obj1.arrayData[0].index');
});
QUnit.test('app.addModel() Validation', function (assert) {
var expectedMessage;
try {
expectedMessage = '[name] was not defined as a string when the function app.addModel() was called';
app.addModel({});
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[name] must have a value when defined when the function app.addModel() is called';
app.addModel('');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[model] was not defined as a object when the function app.addModel() was called';
app.addModel('Test');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
app.addModel('Test', {});
tester.modelsCount++;
assert.ok(true, 'Add Model Test');
} catch (e) {
assert.ok(false, e);
}
try {
expectedMessage = '[app.models.Test] is already defined';
app.addModel('Test', {});
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
});
QUnit.test('app.addPlugin() Validation', function (assert) {
var expectedMessage;
try {
expectedMessage = '[name] was not defined as a string when the function app.addPlugin() was called';
app.addPlugin({});
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[name] must have a value when defined when the function app.addPlugin() is called';
app.addPlugin('');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[plugin] was not defined as a object when the function app.addPlugin() was called';
app.addPlugin('Test');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
var functions = ['onBeforeRender', 'onRendered', 'onRouteUnload'];
functions.forEach(function(func) {
var expectedMessage;
try {
expectedMessage = 'plugin[Test].' + func + ' is not defined as a function.';
var plugin = {};
plugin[func] = '';
app.addPlugin('Test', plugin);
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
});
try {
expectedMessage = 'plugin[Test] must have one of the following properties defined: onRouteLoad, onBeforeRender, onRendered';
app.addPlugin('Test', { onRouteUnload:function(){} });
assert.ok(true, 'Add Model Test');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
app.addPlugin('Test', { onBeforeRender: function () { } });
assert.ok(true, 'Add Plugin Test');
tester.pluginsCount++;
} catch (e) {
assert.ok(false, e);
}
try {
expectedMessage = '[app.plugins.Test] is already defined';
app.addPlugin('Test', { onBeforeRender: function () { } });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
});
QUnit.test('app.addPage() Validation', function (assert) {
var expectedMessage;
try {
expectedMessage = '[name] was not defined as a string when the function app.addPage() was called';
app.addPage({});
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[name] must have a value when defined when the function app.addPage() is called';
app.addPage('');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = 'Page [Test] must be defined as an object or a class when the function app.addPage() is called';
app.addPage('Test');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
var functions = ['onRouteLoad', 'onBeforeRender', 'onRendered', 'onRouteUnload'];
functions.forEach(function(func) {
var expectedMessage;
try {
expectedMessage = 'page[Test].' + func + ' is not defined as a function.';
var page = {};
page[func] = '';
app.addPage('Test', page);
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
});
try {
expectedMessage = 'page[Test] must have one of the following properties defined: onRouteLoad, onBeforeRender, onRendered';
app.addPage('Test', {});
assert.ok(false, 'Add Model Test');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[page.Test.model] must first be defined before the function app.addPage() is called';
app.addPage('Test', { onBeforeRender: function () { } });
assert.ok(false, 'Add Model Test');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[page.Test.model] was not defined as an object when the function app.addPage() was called';
app.addPage('Test', { onBeforeRender: function () { }, model: function () { } });
assert.ok(false, 'Add Model Test');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
app.addPage('Test', {
onBeforeRender: function () { },
model: {}
});
tester.pagesCount++;
assert.ok(true, 'Add Page Test');
} catch (e) {
assert.ok(false, e);
}
try {
expectedMessage = '[app.pages.Test] is already defined';
app.addPage('Test', {
onBeforeRender: function () { },
model: {}
});
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
tester.checkCounts(assert);
});
QUnit.test('app.addController() Validation', function (assert) {
var expectedMessage;
try {
expectedMessage = '[controller] was not defined as a object when the function app.addController() was called';
app.addController('');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[controller.path] was not defined as a string when the function app.addController() was called';
app.addController({});
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[controller.path] must have a value when defined when the function app.addController() is called';
app.addController({ path: '' });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[controller.viewId] was not defined as a string when the function app.addController(path=Test) was called';
app.addController({ path: 'Test', viewId: {} });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[controller.viewId] must have a value when defined when the function app.addController(path=Test) is called';
app.addController({ path: 'Test', viewId: '' });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[controller.viewUrl] was not defined as a string when the function app.addController(path=Test) was called';
app.addController({ path: 'Test', viewUrl: function () { return 'url'; } });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[controller.viewUrl] must have a value when defined when the function app.addController(path=Test) is called';
app.addController({ path: 'Test', viewUrl: '' });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = 'A controller cannot have both [viewId] and [viewUrl] defined when calling app.addController(path=Test)';
app.addController({ path: 'Test', viewId: 'view', viewUrl: 'url' });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = 'An element was not found on the page with [controller.viewId][id=missing-template] when the function app.addController(path=Test) was called';
app.addController({ path: 'Test', viewId: 'missing-template' });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[controller.modelName] was not defined as a string when the function app.addController(path=Test) was called';
app.addController({ path: 'Test', modelName: {} });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[controller.modelName] must have a value when defined when the function app.addController(path=Test) is called';
app.addController({ path: 'Test', modelName: '' });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = 'When a controller uses the [viewEngine] property either [viewId] or [viewUrl] must also be defined when calling app.addController(path=Test)';
app.addController({ path: 'Test', viewEngine: 'Error' });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = 'Invalid [viewEngine] property when calling app.addController(path=Test). Valid values are: Handlebars, Vue, Nunjucks, Underscore, Text';
app.addController({ path: 'Test', viewUrl: 'Error', viewEngine: 'Error' });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[app.models.MissingModel] must first be defined before the function app.addController(path=Test) is called';
app.addController({ path: 'Test', modelName: 'MissingModel' });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
// First add an invalid model manually without using app.addModel()
// This error would happen if the developer overwrites a model object
// as a different type or if they manually add an invalid model.
app.models.InvalidModel = '';
tester.modelsCount++;
// Run the test
expectedMessage = '[app.models.InvalidModel] was not defined as an object when the function app.addController(path=Test) was called';
app.addController({ path: 'Test', modelName: 'InvalidModel' });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[controller.pageType] was not defined as a string when the function app.addController(path=Test) was called';
app.addController({ path: 'Test', pageType: {} });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[controller.pageType] must have a value when defined when the function app.addController(path=Test) is called';
app.addController({ path: 'Test', pageType: '' });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
var functions = ['onRouteLoad', 'onBeforeRender', 'onRendered', 'onRouteUnload'];
functions.forEach(function(func) {
var expectedMessage;
try {
expectedMessage = 'controller[Test].' + func + ' is not defined as a function.';
var controller = { path:'Test' };
controller[func] = '';
app.addController(controller);
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
});
try {
expectedMessage = 'controller[Test] must have one of the following properties defined: onRouteLoad, onBeforeRender, onRendered, viewId, viewUrl';
app.addController({ path: 'Test', onRouteUnload:function(){} });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
app.addController({
path: 'Test',
onRendered: function () { }
});
tester.controllersCount++;
assert.ok(true, 'Add Page Test');
} catch (e) {
assert.ok(false, e);
}
try {
expectedMessage = '[app.controllers(path=Test)] is already defined';
app.addController({
path: 'Test',
onRendered: function () { }
});
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
app.addController({
path: 'ModelNameTest',
modelName: 'Test',
onRendered: function () { }
});
tester.controllersCount++;
assert.ok(true, 'Add Page with modelName=Test');
} catch (e) {
assert.ok(false, e);
}
try {
app.addController({
path: 'PageTypeTest',
pageType: 'Test'
});
tester.controllersCount++;
assert.ok(true, 'Add Controller with pageType=Test');
} catch (e) {
assert.ok(false, e);
}
try {
var controller = {
path: 'DynamicModel',
onRouteLoad: function () { },
settings: { prop1: '' }
};
app.addController(controller);
tester.controllersCount++;
assert.ok(true, 'Add Controller with Dynamic Model (Settings Defined and without an existing Model)');
// Check the [app.controller()] function
assert.ok(controller === app.controller('DynamicModel'), 'Fetching controller from app.controller(path)');
assert.ok(null === app.controller('Unknown'), 'Returning null from app.controller(path)');
} catch (e) {
assert.ok(false, e);
}
tester.checkCounts(assert);
});
QUnit.test('app.addControl() Validation', function (assert) {
var expectedMessage;
assert.ok(app.controls['unit-test'] === undefined, 'Control [unit-test] must not exist');
try {
expectedMessage = '[name] was not defined as a string when the function app.addControl() was called';
app.addControl({});
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[name] must have a value when defined when the function app.addControl() is called';
app.addControl('');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = 'Control names must be all lower-case. [app.addControl()] was called with: [unitTest]';
app.addControl('unitTest');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = 'Control names must contain a dash [-] character. [app.addControl()] was called with: [unittest]';
app.addControl('unittest');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = 'Control names cannot contain a space. [app.addControl()] was called with: [-unit test]';
app.addControl('-unit test');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = 'Control names cannot contain HTML characters that need to be escaped. Invalid characters are [& < > " \' /]. [app.addControl()] was called with: [<unit-test>]';
app.addControl('<unit-test>');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[control] was not defined as a object when the function app.addControl() was called';
app.addControl('unit-test', '');
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
expectedMessage = '[control.css] was not defined as a string when the function app.addControl() was called';
app.addControl('unit-test', { css:false });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
var functions = ['onLoad', 'html', 'onUnload'];
functions.forEach(function(func) {
var expectedMessage;
try {
expectedMessage = 'control[unit-test].' + func + ' is not defined as a function.';
var controller = {};
controller[func] = '';
app.addControl('unit-test', controller);
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
});
try {
expectedMessage = 'control[unit-test] must have one of the following properties defined: onLoad, html';
app.addControl('unit-test', { onUnload:function() {} });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
try {
app.addControl('unit-test', { onLoad:function() {} });
assert.ok(app.controls['unit-test'] !== undefined, 'Control Added');
} catch (e) {
assert.ok(false, e);
}
try {
expectedMessage = '[app.controls.unit-test] is already defined';
app.addControl('unit-test', { onLoad:function() {} });
assert.ok(false, 'Test should have failed');
} catch (e) {
assert.ok(e instanceof TypeError, 'Execption should be a TypeError');
assert.equal(e.message, expectedMessage, expectedMessage);
}
// Reset
delete app.controls['unit-test'];
assert.ok(app.controls['unit-test'] === undefined, 'Control [unit-test] should be deleted');
});
// Manually trigger an error alert, validate, then close dialog
// Unlike most tests this runs synchronously
QUnit.test('app.showErrorAlert()', function (assert) {
// Make sure the error style element does not yet exist
// It gets created by calls to [showErrorAlert()] and [showError()]
var errorStyle = document.getElementById('dataformsjs-style-errors');
assert.ok(errorStyle === null, 'Checking for Style Element [dataformsjs-style-errors] before [app.showErrorAlert()]');
// Create and show error
var errorText = 'Test';
app.showErrorAlert(errorText);
var div = document.querySelector('.dataformsjs-fatal-error');
var span = div.querySelector('span');
// Make sure error style element was created
errorStyle = document.getElementById('dataformsjs-style-errors');
assert.ok(errorStyle !== null, 'Checking for Style Element [dataformsjs-style-errors] after [app.showErrorAlert()]');
assert.equal(errorStyle.innerHTML, app.errorCss, 'Error Style CSS: ' + errorStyle.innerHTML);
// Basic Element Checks
assert.ok(div !== null, 'Found Element ".dataformsjs-fatal-error"');
assert.ok(span !== null, 'Found Element ".dataformsjs-fatal-error span"');
assert.equal(div.childNodes.length, 2, 'Correct Child Node Count: ' + div.childNodes.length);
assert.equal(div.tagName, 'DIV', 'Correct Element Type for Container: ' + div.tagName);
assert.equal(div.childNodes[1].textContent, 'Error: ' + errorText, 'Div textContent: ' + div.childNodes[1].textContent);
assert.equal(div.className, 'dataformsjs-fatal-error', 'Div className: ' + div.className);
assert.equal(span.tagName, 'SPAN', 'Correct Element Type for [X] Button: ' + span.tagName);
assert.equal(span.textContent, '✕', 'Span textContent: ' + span.textContent);
// Check CSS Computed Properties which get set
// from the dynamically created Style Sheet.
var elements = [
{
el: div,
cssProps: {
'color': 'rgb(255, 255, 255)',
'background-color': 'rgb(255, 0, 0)',
'background-image': 'linear-gradient(rgb(238, 0, 0), rgb(204, 0, 0))',
// Browsers may return [boxShadow] in a different
// format so handle any known valid value.
'box-shadow': [
'0px 1px 5px 0px rgba(0,0,0,0.5)',
'0 1px 5px 0 rgba(0,0,0,.5)',
'rgba(0, 0, 0, 0.5) 0px 1px 5px 0px',
'rgba(0, 0, 0, 0.498039) 0px 1px 5px 0px',
],
'z-index': '1000000',