Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
3815 lines (2814 loc) · 114 KB
/
Copy pathtest.js
File metadata and controls
3815 lines (2814 loc) · 114 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
/*! cal-heatmap v3.3.5 (Wed Oct 16 2013 23:56:23)
* ---------------------------------------------
* Cal-Heatmap is a javascript module to create calendar heatmap to visualize time series data
* https://github.com/kamisama/cal-heatmap
* Licensed under the MIT license
* Copyright 2013 Wan Qi Chen
*/
CalHeatMap.prototype.svg = function() {
return this.root.selectAll(".graph-domain");
};
QUnit.testStart(function( details ) {
$("body").append("<div id='cal-heatmap' style='display:none;'></div>");
});
QUnit.testDone(function( details ) {
$("#cal-heatmap").remove();
});
function createCalendar(settings) {
var cal = new CalHeatMap();
if (!settings.hasOwnProperty("loadOnInit")) {
settings.loadOnInit = false;
}
settings.animationDuration = 0;
if (!settings.hasOwnProperty("paintOnLoad")) {
settings.paintOnLoad = false;
}
cal.init(settings);
return cal;
}
/**
* Mark a test as skipped
*
* @link http://stackoverflow.com/questions/13748129/skipping-a-test-in-qunit
* @return {[type]} [description]
*/
QUnit.testSkip = function() {
QUnit.test(arguments[0] + ' (SKIPPED)', function() {
var li = document.getElementById(QUnit.config.current.id);
QUnit.done(function() {
if (li !== null) {
li.style.background = '#FFFF99';
}
});
ok(true);
});
};
testSkip = QUnit.testSkip;
/*
-----------------------------------------------------------------
SETTINGS
Test that when no legendMargin is set, margin are automatically
computed from legend position and orientation
-----------------------------------------------------------------
*/
module("API: init(legendMargin)");
function __testAutoSetLegendMarginSetting(title, ver, hor, autoMarginIndex) {
test("Automatically add margin to " + title, function() {
expect(1);
var margin = [0, 0, 0, 0];
var cal = createCalendar({ legendVerticalPosition: ver, legendHorizontalPosition: hor });
margin[autoMarginIndex] = cal.DEFAULT_LEGEND_MARGIN;
deepEqual(cal.options.legendMargin, margin, "domainMargin is set to [" + margin.join(", ") + "]");
});
}
__testAutoSetLegendMarginSetting("bottom", "top", "left", 2);
__testAutoSetLegendMarginSetting("bottom", "top", "center", 2);
__testAutoSetLegendMarginSetting("bottom", "top", "right", 2);
__testAutoSetLegendMarginSetting("top", "bottom", "left", 0);
__testAutoSetLegendMarginSetting("top", "bottom", "center", 0);
__testAutoSetLegendMarginSetting("top", "bottom", "right", 0);
__testAutoSetLegendMarginSetting("right", "middle", "left", 1);
__testAutoSetLegendMarginSetting("left", "middle", "right", 3);
/*
-----------------------------------------------------------------
SETTINGS
Test colLimit and rowLimit setting passed to init()
-----------------------------------------------------------------
*/
module("API: init(colLimit)");
function __testcolLimitSetting(title, value, expected) {
test("Set colLimit from " + title, function() {
expect(1);
var cal = createCalendar({ colLimit: value });
deepEqual(cal.options.colLimit, expected, "colLimit is set to " + expected);
});
}
__testcolLimitSetting("null will disable colLimit", null, null);
__testcolLimitSetting("false will disable colLimit", false, null);
__testcolLimitSetting("an invalid value (string) will disable colLimit", false, null);
__testcolLimitSetting("a valid empty integer will disable colLimit", 0, null);
__testcolLimitSetting("a valid non-empty integer will set colLimit", 2, 2);
module("API: init(rowLimit)");
function __testRowLimitSetting(title, value, expected) {
test("Set rowLimit from " + title, function() {
expect(1);
var cal = createCalendar({ rowLimit: value });
deepEqual(cal.options.rowLimit, expected, "rowLimit is set to " + expected);
});
}
__testRowLimitSetting("null will disable rowLimit", null, null);
__testRowLimitSetting("false will disable rowLimit", false, null);
__testRowLimitSetting("an invalid value (string) will disable rowLimit", false, null);
__testRowLimitSetting("a valid empty integer will disable rowLimit", 0, null);
__testRowLimitSetting("a valid non-integer will set rowLimit", 2, 2);
test("RowLimit is disabled when colLimit is set", function() {
expect(1);
var cal = createCalendar({ colLimit: 5, rowLimit: 5 });
deepEqual(cal.options.rowLimit, null, "rowLimit is disabled");
});
/*
-----------------------------------------------------------------
SETTINGS
Test dataType options passed to init()
-----------------------------------------------------------------
*/
module("API: init(dataType)");
test("Allow only valid data type", function() {
var types = ["json", "txt", "csv", "tsv"];
expect(types.length);
var cal = new CalHeatMap();
for(var i = 0, total = types.length; i < total; i++) {
ok(cal.init({range:1, dataType: types[i], loadOnInit: false, paintOnLoad: false}), types[i] + " is a valid domain");
}
});
function _testInvalidDataType(name, input) {
test("Invalid dataType (" + name + ") throws an Error", function() {
expect(1);
throws(function() { cal.init({dataType: input}); });
});
}
_testInvalidDataType("empty string", "");
_testInvalidDataType("null", null);
_testInvalidDataType("false", false);
_testInvalidDataType("undefined", undefined);
_testInvalidDataType("random string", "random string");
_testInvalidDataType("number", 15);
/*
-----------------------------------------------------------------
SETTINGS
Test domain and subDomain options passed to init()
-----------------------------------------------------------------
*/
module("API: init(domain)");
(function() {
function _testValidDomain(d) {
test("Testing that " + d + " is a valid domain", function() {
expect(1);
var cal = createCalendar({domain: d});
strictEqual(cal.options.domain, d);
});
}
var domains = ["hour", "day", "week", "month", "year"];
for(var i = 0, total = domains.length; i < total; i++) {
_testValidDomain(domains[i]);
}
}());
function _testInvalidDomain(name, input) {
test("Invalid domain (" + name + ") throws an Error", function() {
expect(1);
throws(function() { createCalendar({domain: input}); });
});
}
_testInvalidDomain("empty string", "");
_testInvalidDomain("null", null);
_testInvalidDomain("false", false);
_testInvalidDomain("not-valid domain type", "random-value");
_testInvalidDomain("min", "min"); // Min is a valid subDomain but not domain
test("Set default domain and subDomain", function() {
expect(2);
var cal = createCalendar({});
strictEqual(cal.options.domain, "hour", "Default domain is HOUR");
strictEqual(cal.options.subDomain, "min", "Default subDomain is MIN");
});
module("API: init(subDomain)");
(function() {
function _testValidSubDomain(d) {
test("Testing that " + d + " is a valid subDomains", function() {
expect(1);
var cal = createCalendar({subDomains: d});
strictEqual(cal.options.subDomains, d);
});
}
var subDomains = ["min", "x_min", "hour", "x_hour", "day", "x_day", "week", "x_week", "month", "x_month"];
for(var i = 0, total = subDomains.length; i < total; i++) {
_testValidSubDomain(subDomains[i]);
}
}());
function _testInvalidSubDomain(name, input) {
test("Invalid subDomain (" + name + ") throws an Error", function() {
expect(1);
throws(function() { createCalendar({subDomain: input}); });
});
}
_testInvalidSubDomain("empty string", "");
_testInvalidSubDomain("null", null);
_testInvalidSubDomain("false", false);
_testInvalidSubDomain("not-valid subDomain type", "random-value");
_testInvalidSubDomain("year", "year"); // Year is a valid domain but not subDomain
function _testSubDomainSmallerThanDomain(domain, subDomain) {
test(subDomain + " is a valid subDomain for " + domain, function() {
expect(2);
var cal = createCalendar({domain: domain, subDomain: subDomain});
strictEqual(cal.options.domain, domain);
strictEqual(cal.options.subDomain, subDomain);
});
}
_testSubDomainSmallerThanDomain("hour", "min");
_testSubDomainSmallerThanDomain("day", "min");
_testSubDomainSmallerThanDomain("day", "hour");
_testSubDomainSmallerThanDomain("week", "hour");
_testSubDomainSmallerThanDomain("week", "min");
_testSubDomainSmallerThanDomain("week", "day");
_testSubDomainSmallerThanDomain("month", "week");
_testSubDomainSmallerThanDomain("year", "month");
_testSubDomainSmallerThanDomain("year", "week");
function _testInvalidSubDomainForDomain(domain, subDomain) {
test(subDomain + " is not a valid subDomain for " + domain, function() {
expect(1);
throws(function() { createCalendar({domain: domain, subDomain: subDomain}); });
});
}
_testInvalidSubDomainForDomain("hour", "day");
_testInvalidSubDomainForDomain("day", "week");
_testInvalidSubDomainForDomain("week", "month");
_testInvalidSubDomainForDomain("month", "year");
function _testDefaultSubDomain(domain, subDomain) {
test(subDomain + " is the default subDomain for " + domain, function() {
expect(2);
var cal = createCalendar({domain: domain});
strictEqual(cal.options.domain, domain);
strictEqual(cal.options.subDomain, subDomain);
});
}
_testDefaultSubDomain("hour", "min");
_testDefaultSubDomain("day", "hour");
_testDefaultSubDomain("week", "day");
_testDefaultSubDomain("month", "day");
_testDefaultSubDomain("year", "month");
/*
-----------------------------------------------------------------
SETTINGS
Test domainLabelFormat setting passed to init()
-----------------------------------------------------------------
*/
module("API: init(domainLabelFormat)");
test("Passing an empty string", function() {
expect(1);
var cal = createCalendar({ domainLabelFormat: "" });
strictEqual(cal.options.domainLabelFormat, "");
});
test("Passing a non-empty string", function() {
expect(1);
var cal = createCalendar({ domainLabelFormat: "R" });
strictEqual(cal.options.domainLabelFormat, "R");
});
test("Passing a function", function() {
expect(1);
var cal = createCalendar({ domainLabelFormat: function() {} });
ok(typeof cal.options.domainLabelFormat === "function");
});
function _testdomainLabelFormatWithInvalidInput(title, input) {
test("Passing a not-valid input (" + title + ")", function() {
expect(1);
var cal = createCalendar({ domainLabelFormat: input });
strictEqual(cal.options.domainLabelFormat, cal._domainType.hour.format.legend, "Invalid input should fallback to the domain default legend format");
});
}
_testdomainLabelFormatWithInvalidInput("number", 10);
_testdomainLabelFormatWithInvalidInput("undefined", undefined);
_testdomainLabelFormatWithInvalidInput("false", false);
_testdomainLabelFormatWithInvalidInput("null", null);
_testdomainLabelFormatWithInvalidInput("array", []);
_testdomainLabelFormatWithInvalidInput("object", {});
/*
-----------------------------------------------------------------
SETTINGS
Test domainMargin and legendMargin setting passed to init()
-----------------------------------------------------------------
*/
module("API: init(domainMargin)");
function __testDomainMarginExpand(title, margin, expectedMargin) {
test("Test expanding " + title, function() {
expect(1);
var cal = createCalendar({ domainMargin: margin });
deepEqual(cal.options.domainMargin, expectedMargin, (Array.isArray(margin) ? "["+margin.join(", ")+"]" : margin) + " is expanded to [" + expectedMargin.join(", ") + "]");
});
}
__testDomainMarginExpand("a null integer", 0, [0,0,0,0]);
__testDomainMarginExpand("a non-null integer", 10, [10,10,10,10]);
__testDomainMarginExpand("a one-value (zero) array", [0], [0,0,0,0]);
__testDomainMarginExpand("a one-value (five) array", [5], [5,5,5,5]);
__testDomainMarginExpand("a two-value array", [5, 10], [5,10,5,10]);
__testDomainMarginExpand("a three-value array", [5, 10, 15], [5,10,15,10]);
__testDomainMarginExpand("a four-value array", [5, 10, 15, 20], [5,10,15,20]);
__testDomainMarginExpand("a six-value array", [5, 10, 15, 20, 30, 40], [5,10,15,20]);
__testDomainMarginExpand("an invalid (string) value fallback to 0", "string", [0,0,0,0]);
__testDomainMarginExpand("an invalid (empty string) value fallback to 0", "", [0,0,0,0]);
module("API: init(legendMargin)");
function __testDomainLegendExpand(title, margin, expectedMargin) {
test("Test expanding " + title, function() {
expect(1);
var cal = createCalendar({ legendMargin: margin });
deepEqual(cal.options.legendMargin, expectedMargin, (Array.isArray(margin) ? "["+margin.join(", ")+"]" : margin) + " is expanded to [" + expectedMargin.join(", ") + "]");
});
}
__testDomainLegendExpand("a null integer", 0, [0,0,0,0]);
__testDomainLegendExpand("a non-null integer", 10, [10,10,10,10]);
__testDomainLegendExpand("a one-value (zero) array", [0], [0,0,0,0]);
__testDomainLegendExpand("a one-value (five) array", [5], [5,5,5,5]);
__testDomainLegendExpand("a two-value array", [5, 10], [5,10,5,10]);
__testDomainLegendExpand("a three-value array", [5, 10, 15], [5,10,15,10]);
__testDomainLegendExpand("a four-value array", [5, 10, 15, 20], [5,10,15,20]);
__testDomainLegendExpand("a six-value array", [5, 10, 15, 20, 30, 40], [5,10,15,20]);
__testDomainLegendExpand("an invalid (string) value fallback to 0", "string", [0,0,0,0]);
__testDomainLegendExpand("an invalid (empty string) value fallback to 0", "", [0,0,0,0]);
/*
-----------------------------------------------------------------
SETTINGS
Test highlight setting passed to init()
-----------------------------------------------------------------
*/
module("API: init(highlight)");
function __testHighlightSetting(title, highlight, expected) {
test("Test expanding " + title, function() {
expect(1);
var cal = createCalendar({ highlight: highlight });
deepEqual(cal.options.highlight, expected, (Array.isArray(highlight) ? "["+highlight.join(", ")+"]" : highlight) + " is expanded to [" + expected.join(", ") + "]");
});
}
__testHighlightSetting("a null string", "", []);
__testHighlightSetting("a non-valid string", "date", []);
__testHighlightSetting("an empty array", [], []);
__testHighlightSetting("a non-empty array, with no valid data", ["date", 0], []);
__testHighlightSetting("a non-empty array, with one date object", [new Date(2000, 0)], [new Date(2000, 0)]);
__testHighlightSetting("a non-empty array, with multiple date objects", [new Date(2000, 0), new Date(2001, 0)], [new Date(2000, 0), new Date(2001, 0)]);
__testHighlightSetting("null", null, []);
__testHighlightSetting("a boolean", false, []);
test("Test expanding NOW string", function() {
expect(3);
var cal = createCalendar({ highlight: "now" });
ok(Array.isArray(cal.options.highlight));
equal(cal.options.highlight.length, 1);
ok(cal.options.highlight[0] instanceof Date);
});
test("Test expanding NOW string inside an array of valid dates", function() {
expect(4);
var cal = createCalendar({ highlight: ["now", new Date()] });
ok(Array.isArray(cal.options.highlight));
equal(cal.options.highlight.length, 2);
ok(cal.options.highlight[0] instanceof Date);
ok(cal.options.highlight[1] instanceof Date);
});
test("Test expanding NOW string inside an array of invalid dates", function() {
expect(3);
var cal = createCalendar({ highlight: ["now", "tomorrow"] });
ok(Array.isArray(cal.options.highlight));
equal(cal.options.highlight.length, 1);
ok(cal.options.highlight[0] instanceof Date);
});
/*
-----------------------------------------------------------------
SETTINGS
Test itemName setting passed to init()
-----------------------------------------------------------------
*/
module("API: init(itemName)");
function __testItemNameSetting(title, value, expected) {
test("Set itemName from " + title, function() {
expect(1);
var cal = createCalendar({ itemName: value });
deepEqual(cal.options.itemName, expected, "itemName is set to " + expected);
});
}
__testItemNameSetting("null will fallback to default itemName", null, ["item", "items"]);
__testItemNameSetting("false will fallback to default itemName", false, ["item", "items"]);
__testItemNameSetting("an invalid value (number) will fallback to default itemName", 0, ["item", "items"]);
__testItemNameSetting("an empty string will set an empty string for the singular and plural form", "", ["", ""]);
__testItemNameSetting("a string will guess the plural form", "cat", ["cat", "cats"]);
__testItemNameSetting("a 1-value array will guess the plural form", ["cat"], ["cat", "cats"]);
__testItemNameSetting("a 2-value array will do nothing", ["child", "children"], ["child", "children"]);
__testItemNameSetting("a 3-value array will only keeps the first 2 values", ["child", "children", "bomb"], ["child", "children"]);
/*
-----------------------------------------------------------------
SETTINGS
Test itemNamespace setting passed to init()
-----------------------------------------------------------------
*/
module("API: init(itemNamespace)");
function __testItemNamespaceSetting(title, value, expected) {
test(title, function() {
expect(1);
var cal = createCalendar({ itemNamespace: value });
equal(cal.options.itemNamespace, expected, "itemNamespace is set to " + expected);
});
}
__testItemNamespaceSetting("null will fallback to default namespace", null, "cal-heatmap");
__testItemNamespaceSetting("false will fallback to default namespace", false, "cal-heatmap");
__testItemNamespaceSetting("empty string will fallback to default namespace", "", "cal-heatmap");
__testItemNamespaceSetting("invalid value (array) will fallback to default namespace", [], "cal-heatmap");
__testItemNamespaceSetting("invalid value (object) will fallback to default namespace", {}, "cal-heatmap");
__testItemNamespaceSetting("invalid value (number) will fallback to default namespace", 126, "cal-heatmap");
__testItemNamespaceSetting("Setting a valid namespace from a string", "test-namespace", "test-namespace");
/*
-----------------------------------------------------------------
SETTINGS
Test itemSelector options passed to init()
-----------------------------------------------------------------
*/
module("API: init(itemSelector)");
test("itemSelector accept a valid document.querySelector or CSS3 string value", function() {
$("body").append("<div id=test><div id=a></div><div id=b></div><div data=y></div><div class=u></div><div id=last></div></div>");
expect(10);
var cal = new CalHeatMap();
equal(cal.init({itemSelector: "#a", paintOnLoad: false}), true, "#a is a valid itemSelector");
equal($("#a .cal-heatmap-container").length, 1, "Calendar is appended to #a");
equal(cal.init({itemSelector: "#a + #b", paintOnLoad: false}), true, "#a + #b is a valid itemSelector");
equal($("#b .cal-heatmap-container").length, 1, "Calendar is appended to #a + #b");
equal(cal.init({itemSelector: "div[data=y]", paintOnLoad: false}), true, "div[data=y] is a valid itemSelector");
equal($("div[data=y] .cal-heatmap-container").length, 1, "Calendar is appended to div[data=y]");
equal(cal.init({itemSelector: ".u", paintOnLoad: false}), true, ".u is a valid itemSelector");
equal($(".u .cal-heatmap-container").length, 1, "Calendar is appended to .u");
equal(cal.init({itemSelector: "#test > div:last-child", paintOnLoad: false}), true, "#test > div:last-child is a valid itemSelector");
equal($("#last .cal-heatmap-container").length, 1, "Calendar is appended to #test > div:last-child");
$("#test").remove();
});
test("itemSelector accept a valid Element object", function() {
$("body").append("<div id=test><div id=a></div><div id=b></div><div data=y></div><div class=u></div><div id=last></div></div>");
expect(10);
var cal = new CalHeatMap();
equal(cal.init({itemSelector: document.querySelector("#a"), paintOnLoad: false}), true, "document.querySelector(\"#a\") is a valid itemSelector");
equal($("#a .graph").length, 1, "Graph is appended to #a");
equal(cal.init({itemSelector: $("#b")[0], paintOnLoad: false}), true, "$(\"#b\")[0] is a valid itemSelector");
equal($("#b .graph").length, 1, "Graph is appended to #b");
equal(cal.init({itemSelector: document.getElementById("last"), paintOnLoad: false}), true, "document.getElementById(\"last\") is a valid itemSelector");
equal($("#last .graph").length, 1, "Graph is appended to #last");
equal(cal.init({itemSelector: document.getElementsByClassName("u")[0], paintOnLoad: false}), true, "document.getElementsByClassName(\".u\") is a valid itemSelector");
equal($(".u .graph").length, 1, "Graph is appended to .u");
equal(cal.init({itemSelector: d3.select("[data=y]")[0][0], paintOnLoad: false}), true, "d3.select(\"[data=y]\")[0][0] is a valid itemSelector");
equal($("div[data=y] .graph").length, 1, "Graph is appended to div[data=y]");
$("#test").remove();
});
function _testInvalidItemSelector(name, input) {
test("Invalid itemSelector (" + name + ") throws an Error", function() {
expect(1);
throws(function() { createCalendar({itemSelector: input}); });
});
}
_testInvalidItemSelector("empty string", "");
_testInvalidItemSelector("array", []);
_testInvalidItemSelector("number", 15);
_testInvalidItemSelector("function", function() {});
test("itemSelector target does not exists", function() {
expect(1);
throws(function() { createCalendar({itemSelector: "#test"}); }, "Non-existent itemSelector raises an Error");
});
/*
-----------------------------------------------------------------
SETTINGS
Test subDomainDateFormat setting passed to init()
-----------------------------------------------------------------
*/
module("API: init(subDomainDateFormat)");
test("Passing an empty string", function() {
expect(1);
var cal = createCalendar({ subDomainDateFormat: "" });
strictEqual(cal.options.subDomainDateFormat, "");
});
test("Passing a non-empty string", function() {
expect(1);
var cal = createCalendar({ subDomainDateFormat: "R" });
strictEqual(cal.options.subDomainDateFormat, "R");
});
test("Passing a function", function() {
expect(1);
var cal = createCalendar({ subDomainDateFormat: function() {} });
ok(typeof cal.options.subDomainDateFormat === "function");
});
function _testsubDomainDateFormatWithInvalidInput(title, input) {
test("Passing a not-valid input (" + title + ")", function() {
expect(1);
var cal = createCalendar({ subDomainDateFormat: input });
strictEqual(cal.options.subDomainDateFormat, cal._domainType.min.format.date, "Invalid input should fallback to the subDomain default date format");
});
}
_testsubDomainDateFormatWithInvalidInput("number", 10);
_testsubDomainDateFormatWithInvalidInput("undefined", undefined);
_testsubDomainDateFormatWithInvalidInput("false", false);
_testsubDomainDateFormatWithInvalidInput("null", null);
_testsubDomainDateFormatWithInvalidInput("array", []);
_testsubDomainDateFormatWithInvalidInput("object", {});
/*
-----------------------------------------------------------------
SETTINGS
Test subDomainTextFormat setting passed to init()
-----------------------------------------------------------------
*/
module("API: init(subDomainTextFormat)");
test("Passing a non-empty string", function() {
expect(1);
var cal = createCalendar({ subDomainTextFormat: "R" });
strictEqual(cal.options.subDomainTextFormat, "R");
});
test("Passing a function", function() {
expect(1);
var cal = createCalendar({ subDomainTextFormat: function() {} });
ok(typeof cal.options.subDomainTextFormat === "function");
});
function _testsubDomainTextFormatWithInvalidInput(title, input) {
test("Passing a not-valid input (" + title + ")", function() {
expect(1);
var cal = createCalendar({ subDomainTextFormat: input });
strictEqual(cal.options.subDomainTextFormat, null, "Invalid input should fallback to null");
});
}
_testsubDomainTextFormatWithInvalidInput("empty string", "");
_testsubDomainTextFormatWithInvalidInput("number", 10);
_testsubDomainTextFormatWithInvalidInput("undefined", undefined);
_testsubDomainTextFormatWithInvalidInput("false", false);
_testsubDomainTextFormatWithInvalidInput("null", null);
_testsubDomainTextFormatWithInvalidInput("array", []);
_testsubDomainTextFormatWithInvalidInput("object", {});
/*
-----------------------------------------------------------------
SETTINGS
Test options passed to init()
-----------------------------------------------------------------
*/
test("Auto align domain label horizontally", function() {
expect(4);
var cal = new CalHeatMap();
cal.init({label: {position: "top"}, paintOnLoad: false});
equal(cal.options.label.align, "center", "Auto center label when positioned on top");
cal.init({label: {position: "bottom"}, paintOnLoad: false});
equal(cal.options.label.align, "center", "Auto center label when positioned on bottom");
cal.init({label: {position: "left"}, paintOnLoad: false});
equal(cal.options.label.align, "right", "Auto align label on the right when positioned on the left");
cal.init({label: {position: "right"}, paintOnLoad: false});
equal(cal.options.label.align, "left", "Auto align label on the right when positioned on the right");
});
test("Auto align domain label horizontally when rotated", function() {
expect(2);
var cal = new CalHeatMap();
cal.init({label: {rotate: "left"}, paintOnLoad: false});
equal(cal.options.label.align, "right", "Auto align on the right when rotated to the left");
cal.init({label: {rotate: "right"}, paintOnLoad: false});
equal(cal.options.label.align, "left", "Auto align on the left when rotated to the right");
});
/*
-----------------------------------------------------------------
API
-----------------------------------------------------------------
*/
module("API : jumpTo()");
function _testJumpTo(date, reset, expectedReturn, expectedStartDate, title) {
if (arguments.length < 5) {
title = "";
}
test("Jumping to " + date.toDateString() + " " + (reset ? "with" : "without") + " reset " + title, function() {
expect(2);
var cal = createCalendar({
domain: "month",
start: new Date(2000, 2), // March
range: 4,
minDate: new Date(1999, 11), // December
maxDate: new Date(2000, 11) // December
});
equal(cal.jumpTo(date, reset), expectedReturn, "jumpTo() should return " + expectedReturn);
equal(cal.getDomainKeys()[0], +expectedStartDate, "Calendar should start on " + expectedStartDate.toDateString());
});
}
// Without reset --------------------
_testJumpTo(
new Date(2000, 0),
false,
true,
new Date(2000, 0)
);
_testJumpTo(
new Date(2000, 0, 16, 23),
false,
true,
new Date(2000, 0)
);
_testJumpTo(
new Date(2000, 6),
false,
true,
new Date(2000, 3)
);
_testJumpTo(
new Date(2000, 6, 12, 8),
false,
true,
new Date(2000, 3)
);
// Without reset, out-of-bound date
// Jump to minDate instead
_testJumpTo(
new Date(1999, 0),
false,
true,
new Date(1999, 11),
"jump to minDate"
);
// Jump to maxDate - range instead
_testJumpTo(
new Date(2001, 7),
false,
true,
new Date(2000, 8),
"jump to maxDate - range"
);
// Without reset, the wanted domain is already visible
// Don't jump at all
_testJumpTo(
new Date(2000, 2),
false,
false,
new Date(2000, 2),
"already visible, no jump"
);
_testJumpTo(
new Date(2000, 2, 25, 13, 18),
false,
false,
new Date(2000, 2),
"already visible, no jump"
);
_testJumpTo(
new Date(2000, 3),
false,
false,
new Date(2000, 2),
"already visible, no jump"
);
_testJumpTo(
new Date(2000, 4),
false,
false,
new Date(2000, 2),
"already visible, no jump"
);
_testJumpTo(
new Date(2000, 5),
false,
false,
new Date(2000, 2),
"already visible, no jump"
);
_testJumpTo(
new Date(2000, 5, 30, 23, 59),
false,
false,
new Date(2000, 2),
"already visible, no jump"
);
// With reset
_testJumpTo(
new Date(2000, 0),
true,
true,
new Date(2000, 0)
);
_testJumpTo(
new Date(2000, 0, 6, 16),
true,
true,
new Date(2000, 0)
);
_testJumpTo(
new Date(2000, 6, 18),
true,
true,
new Date(2000, 6)
);
// With reset, out-of-bound date
_testJumpTo(
new Date(1999, 0),
true,
true,
new Date(1999, 11),
"jump to minDate"
);
_testJumpTo(
new Date(2001, 7),
true,
true,
new Date(2000, 8),
"jump to maxDate - range"
);
// With reset, the wanted domain is already visible
// Set the calendar first domain to the jumped dated
_testJumpTo(
new Date(2000, 2, 15),
true,
false,
new Date(2000, 2)
);
_testJumpTo(
new Date(2000, 3, 26),
true,
true,
new Date(2000, 3)
);
_testJumpTo(
new Date(2000, 4, 5),
true,
true,
new Date(2000, 4)
);
_testJumpTo(
new Date(2000, 5, 30),
true,
true,
new Date(2000, 5)
);
/*
-----------------------------------------------------------------
API
-----------------------------------------------------------------
*/
module("API : next()");
function _testNext(title, count, expectedReturn, expectedStartDate, startDate, maxDate) {
if (arguments.length < 5) {
startDate = new Date(2000, 0);
}
if (arguments.length < 6) {
maxDate = new Date(2000, 11);
}
test(title, function() {
expect(2);
var cal = createCalendar( {
domain: "month",
start: startDate,
range: 4,
loadOnInit: true,
paintOnLoad: true,
maxDate: maxDate
});
equal((count === null ? cal.next() : cal.next(count)), expectedReturn, "next() should return " + expectedReturn);
equal(cal.getDomainKeys()[0], +expectedStartDate, "Calendar should start on " + expectedStartDate.toDateString());
});
}
/*
Test:
- Calling next() without any argument
- Calling next(n) with n = 1
- Calling next(1) with n > 1
- Calling next() when maxDate is reached
*/
_testNext(
"Shift one domain forward with next()",
null,
true,
new Date(2000, 1)
);
_testNext(
"Shift three domains forward with next(3)",
3,
true,
new Date(2000, 3)
);
_testNext(
"Shift eight domains forward with next(8)",
8,
true,
new Date(2000, 8)
);
_testNext(
"Shifting does not go beyond maxDate",
11,
true,
new Date(2000, 8)
);
_testNext(
"Shifting does not go beyond maxDate",
25,
true,
new Date(2000, 8)
);
_testNext(
"next() do nothing when maxDate === startDate",
null,
false,
new Date(2000, 0),
new Date(2000, 0),
new Date(2000, 0)
);