forked from KidSysco/jquery-ui-month-picker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
1412 lines (1074 loc) · 61.5 KB
/
test.js
File metadata and controls
1412 lines (1074 loc) · 61.5 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
/******* ***************** ********/
/******* Unit Testing Code ********/
/******* ***************** ********/
/*
* Welcome to the jQuery UI Month Picker project.
*
* jQuery UI and QUnit are required for testing.
* Please download node.js, 'cd /jquery-ui-month-picker' and run 'npm install'.
*
* Tests can be ran from the command line using the 'grunt test' command.
*/
var _today = new Date();
// Set's the default animation speed to 1 ms to speed up
// the tests.
$.KidSysco.MonthPicker.prototype.options.Duration = 1;
QUnit.module("Installation");
/***** Test basic the installation for the pre-requisite objects and namespaces *****/
QUnit.test('Status', function (assert) {
assert.ok(true, 'qUnit is installed.');
assert.ok(jQuery, 'jQuery ' + $().jquery + ' is installed.');
assert.ok(jQuery.ui, 'jQuery UI ' + jQuery.ui.version + ' is installed.');
assert.ok(jQuery.widget, 'jQuery UI widget is installed.');
assert.ok(jQuery.ui.button, 'jQuery UI button is installed.');
assert.ok(jQuery.ui.datepicker, 'jQuery UI datepicker is installed.');
assert.ok(jQuery.KidSysco, 'KidSysco namespace exists.');
assert.ok(jQuery.KidSysco.MonthPicker, 'MonthPicker plugin function exists.');
assert.ok(jQuery.KidSysco.MonthPicker.prototype, 'MonthPicker plugin prototype exists.');
});
QUnit.test('Optional Plugin Status', function (assert) {
assert.ok(jQuery.mask, 'Digital Bush Input Mask Plugin is installed.');
assert.ok($.ui.position, 'jQuery UI .position() plugin is installed.');
});
QUnit.module("Functionality");
/***** Icon Demo Tests *****/
QUnit.test('Icon Option Tests', function (assert) {
var _picker = $('#IconDemo').MonthPicker({
Animation: 'none',
ShowIcon: true
});
assert.equal($('#MonthPicker_Button_IconDemo').length, 1, '#IconDemo initialized with icon showing.');
_picker.MonthPicker('option', 'ShowIcon', false);
assert.equal($('#MonthPicker_Button_IconDemo:visible').length, 0, '#IconDemo icon removed after init.');
$('#IconDemo').MonthPicker('destroy');
assert.ok($('#MonthPicker_IconDemo').length === 0, "#IconDemo has been destroyed.");
_picker = $('#IconDemo').MonthPicker({
Animation: 'none',
ShowIcon: false
});
var menu = $(MonthPicker_IconDemo);
_picker.trigger('focus');
assert.ok(menu.css('visibility') !== 'hidden', 'The menu was opened by focusing on the input field');
// Make pressing tab closes the menu.
_picker.trigger($.Event( "keydown", { keyCode: $.ui.keyCode.TAB } ));
assert.notOk(menu.is(':visible'), 'The menu was closed by pressing tab');
assert.equal($('#MonthPicker_Button_IconDemo').length, 0, '#IconDemo initialized without an icon.');
_picker.MonthPicker('option', 'ShowIcon', true);
assert.equal($('#MonthPicker_Button_IconDemo').length, 1, '#IconDemo icon added after init.');
// Make sure focusing on the field doesn't open the menu.
_picker.trigger('focus');
assert.notOk(menu.is(':visible'), 'The menu did not opened by focusing on the input field because the icon is now visible');
});
QUnit.test('HTML 5 & Formatting Tests', function (assert) {
var done;
var _picker = $('#Html5').MonthPicker({
ShowIcon: false,
StartYear: 2027,
OnAfterMenuOpen: function () {
assert.equal(_pickerMenu.css('display'), 'block', '#Html5 responded to a text input click event and showed the menu.');
$('.button-1', _pickerMenu).trigger($.Event('click'));
done();
done = assert.async();
},
OnAfterMenuClose: function () {
assert.equal(_pickerMenu.css('display'), 'none', '#Html5 responded to a button click event by closing the menu.');
assert.equal(_picker.MonthPicker('GetSelectedYear'), '2027', '#Html5 showed and selected the correct override start year of 2027.');
assert.equal(_picker.MonthPicker('GetSelectedMonth'), '01', '#Html5 showed and selected the correct month of 01.');
// destroy the monthpicker and re-create it so it doesnt fire anymore qunit events upon being used.
$('#Html5').MonthPicker('destroy');
assert.ok($('#MonthPicker_Html5').length === 0, "#Html5 has been destroyed.");
$('#Html5').MonthPicker({
ShowIcon: false,
StartYear: 2027
});
done();
}
});
var _pickerMenu = $('#MonthPicker_Html5');
// click the the button to show the monthpicker menu
_picker.trigger($.Event('click'));
assert.equal(_pickerMenu.length, 1, '#Html5 month picker menu exists in the DOM.');
done = assert.async();
});
if ($.ui.position) {
QUnit.test('jQueryUI .position() Tests', function (assert) {
var done;
var _windowWidth = $(window).width();
var _windowHeight = $('body').height();
var _picker = $('#PositionDemo').MonthPicker({
ShowIcon: false,
Position: {
collision: 'fit flip'
},
OnAfterMenuOpen: function () {
var _meun = $(MonthPicker_PositionDemo);
var _lastPointFullyVisibleX = _windowWidth - _meun.width();
var _lastPointFullyVisibleY = _windowHeight - _pickerMenu.height();
assert.equal(_pickerMenu.css('display'), 'block', '#PositionDemo responded to a text input click event and showed the menu.');
assert.ok(_pickerMenu.position().left <= _lastPointFullyVisibleX, "#PositionDemo does not overlap the right window boundary on the X axis.");
assert.ok(_pickerMenu.position().left > 0, "#PositionDemo does not overlap the left window boundary on the X axis.");
assert.ok(_pickerMenu.position().top <= _lastPointFullyVisibleY, "#PositionDemo does not overlap the bottom window boundary on the Y axis.");
assert.ok(_pickerMenu.position().top > 0, "#PositionDemo does not overlap the top window boundary on the Y axis.");
$('.button-1', _pickerMenu).trigger($.Event('click'));
done();
done = assert.async();
},
OnAfterMenuClose: function () {
assert.equal(_pickerMenu.css('display'), 'none', '#PositionDemo responded to a button click event by closing the menu.');
// destroy the monthpicker and re-create it so it doesnt fire anymore qunit events upon being used.
$('#PositionDemo').MonthPicker('destroy');
assert.ok($('#MonthPicker_PositionDemo').length === 0, "#PositionDemo has been destroyed.");
$('#PositionDemo').MonthPicker({
ShowIcon: false,
Position: {
collision: 'fit flip'
}
});
done();
}
});
var _pickerMenu = $('#MonthPicker_PositionDemo');
// click the the button to show the monthpicker menu
_picker.trigger($.Event('click'));
assert.equal(_pickerMenu.length, 1, '#PositionDemo month picker menu exists in the DOM.');
done = assert.async();
});
}
QUnit.test('Override Start Year Tests', function (assert) {
var done;
var _picker = $('#OverrideStartYear').MonthPicker({
ShowIcon: false,
StartYear: 2023,
OnAfterMenuOpen: function () {
assert.equal(_pickerMenu.css('display'), 'block', '#OverrideStartYear responded to a text input click event and showed the menu.');
$('.button-1', _pickerMenu).trigger($.Event('click'));
done();
done = assert.async();
},
OnAfterMenuClose: function () {
assert.equal(_picker.MonthPicker('GetSelectedYear'), '2023', '#OverrideStartYear showed and selected the correct override start date of 2023.');
$('#OverrideStartYear').MonthPicker('destroy');
assert.ok($('#MonthPicker_OverrideStartYear').length === 0, "#OverrideStartYear has been destroyed.");
$('#OverrideStartYear').MonthPicker({
ShowIcon: false,
StartYear: 2023
});
done();
}
});
assert.ok($('#MonthPicker_OverrideStartYear').length === 1, "#OverrideStartYear has been initialized for demo purposes.");
var _pickerMenu = $('#MonthPicker_OverrideStartYear');
_picker.trigger($.Event('click'));
done = assert.async();
});
QUnit.test('Start Year Option Tests', function (assert) {
var done;
var _picker = $('#StartYearDemo').MonthPicker({
ShowIcon: false,
OnAfterMenuOpen: function () {
assert.equal(_pickerMenu.css('display'), 'block', '#StartYearDemo responded to a text input click event and showed the menu.');
assert.equal(parseInt(_picker.MonthPicker('GetSelectedYear'), 10), 2025, '#StartYearDemo defaulted to 2025, the year specified in the textbox.');
_picker.MonthPicker('option', 'StartYear', 2095);
assert.equal(parseInt($('.year', _pickerMenu).text(), 10), 2095, '#StartYearDemo switched the year to 2095 after init.');
$('.button-1', _pickerMenu).trigger($.Event('click'));
assert.equal(parseInt(_picker.MonthPicker('GetSelectedYear'), 10), 2095, '#StartYearDemo selected the correct year, 2095, upon choosing a month.');
done();
done = assert.async();
},
OnAfterMenuClose: function () {
assert.equal(_pickerMenu.css('display'), 'none', '#StartYearDemo responded to a button click event by closing the menu.');
$('#StartYearDemo').MonthPicker('destroy');
assert.ok($('#MonthPicker_StartYearDemo').length === 0, "#StartYearDemo has been destroyed.");
$('#StartYearDemo').val('1/2025').MonthPicker({
ShowIcon: false,
OnAfterMenuOpen: function () {
// get the picker menu again because it was removed from the dom upon destroying StartYearDemo.
_pickerMenu = $('#MonthPicker_StartYearDemo');
assert.equal(_pickerMenu.css('display'), 'block', '#StartYearDemo responded to a text input click event and showed the menu.');
assert.equal(parseInt(_picker.MonthPicker('GetSelectedYear'), 10), 2025, '#StartYearDemo defaulted to 2025, the year specified in the textbox.');
$(document).trigger($.Event('click'));
done();
done = assert.async();
},
OnAfterMenuClose: function () {
assert.equal(_pickerMenu.css('display'), 'none', '#StartYearDemo responded to a button click event by closing the menu.');
_picker.MonthPicker('destroy');
// get the picker menu again because it was removed from the dom upon destroying StartYearDemo.
assert.ok($('#MonthPicker_StartYearDemo').length === 0, "#StartYearDemo has been destroyed again.");
// Setup StartYearDemo so that it will run as a demo without firing off tests.
$('#StartYearDemo').val('1/2025').MonthPicker({
ShowIcon: false
});
assert.ok(_pickerMenu.length === 1, "#StartYearDemo has been re-initialized for demo purposes.");
done();
}
});
assert.ok(_pickerMenu.length === 1, "#StartYearDemo has been re-initialized for more tests.");
done();
done = assert.async();
_picker.trigger($.Event('click'));
}
});
var _pickerMenu = $('#MonthPicker_StartYearDemo');
assert.equal(_pickerMenu.length, 1, '#StartYearDemo month picker menu exists in the DOM.');
done = assert.async();
_picker.trigger($.Event('click'));
});
QUnit.test('API Tests', function (assert) {
var _picker = $('#GetYearDemo').MonthPicker({
ValidationErrorMessage: 'Invalid Date!'
});
assert.ok($('#MonthPicker_GetYearDemo').length === 1, '#GetYearDemo has been initialized.');
assert.equal(jQuery.type(_picker.MonthPicker('GetSelectedDate')), 'date', '#GetYearDemo GetSelectedDate() API call returned a date object.');
assert.equal(_picker.MonthPicker('GetSelectedDate').getFullYear(), 2012, '#GetYearDemo GetSelectedDate() API call returned a date object containing the correct year, 2012.');
assert.equal(_picker.MonthPicker('GetSelectedDate').getMonth(), 1, '#GetYearDemo GetSelectedDate() API call returned a date object containing the correct zero-based array index for February, 1.');
assert.equal(_picker.MonthPicker('GetSelectedMonth'), 2, '#GetYearDemo GetSelectedMonth() API call returned the correct month, 2.');
assert.equal(_picker.MonthPicker('GetSelectedYear'), 2012, '#GetYearDemo GetSelectedYear() API call returned the correct year, 2012.');
assert.equal(_picker.MonthPicker('GetSelectedMonthYear'), '2/2012', '#GetYearDemo GetSelectedMonthYear() API call returned the correct month/year, 2/2012.');
_picker.val('aa');
assert.equal(_picker.MonthPicker('GetSelectedMonthYear'), null, '#GetYearDemo GetSelectedMonthYear() API call detected a bad date and returned null.');
assert.ok(isNaN(_picker.MonthPicker('GetSelectedMonth')), '#GetYearDemo GetSelectedMonth() API call detected a bad date and returned NaN.');
assert.ok(isNaN(_picker.MonthPicker('GetSelectedYear')), '#GetYearDemo GetSelectedYear() API call detected a bad date and returned NaN.');
assert.equal(_picker.MonthPicker('GetSelectedDate'), null, '#GetYearDemo GetSelectedDate() API call detected a bad date and returned a null.');
assert.equal($('#MonthPicker_Validation_GetYearDemo').css('display'), 'inline', '#GetYearDemo showed a validation message about a bad date.');
_picker.MonthPicker('Clear').val('02/2012');
assert.equal($('#MonthPicker_Validation_GetYearDemo').css('display'), 'none', '#GetYearDemo Clear() API call reset the validation warning.');
_picker = $('#EnableDisableDemo').MonthPicker({
Disabled: true
});
assert.equal(_picker.prop('disabled'), true, '#EnableDisableDemo was initialized into a disabled state.');
_picker.MonthPicker('option', 'Disabled', false);
assert.equal(_picker.prop('disabled'), false, '#EnableDisableDemo was enabled by changing the Disabled option.');
_picker.MonthPicker('Disable');
assert.equal(_picker.prop('disabled'), true, '#EnableDisableDemo was disabled using the Disable() API call.');
assert.ok(_picker.is('.month-picker-disabled'), 'The input field has the month-picker-disabled class');
_picker.MonthPicker('Enable');
assert.equal(_picker.prop('disabled'), false, '#EnableDisableDemo was enabled using the Disable() API call.');
assert.notOk(_picker.is('.month-picker-disabled'), 'The month-picker-disabled class was removed from the input field');
});
QUnit.test('Digital Bush Tests', function (assert) {
var _picker = $('#DigitalBush').MonthPicker({
UseInputMask: true
});
_picker.focus();
$('#DigitalBushBoth').MonthPicker({
UseInputMask: true,
ValidationErrorMessage: 'Invalid Date!'
});
assert.equal($('#DigitalBushBoth').MonthPicker('GetSelectedMonthYear'), null, '#DigitalBushBoth GetSelectedMonthYear API call returned null as expected.');
assert.equal($('#MonthPicker_Validation_DigitalBushBoth').css('display'), 'inline', '#DigitalBushBoth showed a validation message about a bad date.');
$('#DigitalBushBoth').MonthPicker('Clear');
assert.equal($('#MonthPicker_Validation_DigitalBushBoth').css('display'), 'none', '#DigitalBushBoth cleared the validation error message using the Clear() API call.');
});
QUnit.test('Only one open menu', function (assert) {
$([FirstField, SecondField]).MonthPicker({
Animation: 'none' // Disable animation to make sure opening and closing the menu is synchronous.
});
$(FirstField).MonthPicker('Open');
$(SecondField).MonthPicker('Open');
assert.equal($('.month-picker').filter(':visible').length, 1, 'There is only one menu opened.');
});
QUnit.test('Keydown handling', function (assert) {
var field = $(EventsField).val('').MonthPicker({
Animation: 'none' // Disable animation to make sure opening and closing the menu is synchronous.
});
field.MonthPicker('Open');
field.trigger($.Event('keydown', {keyCode: $.ui.keyCode.ENTER}));
assert.equal(field.val(), _today.getMonth()+1 + '/' + _today.getFullYear(), 'Pressing enter selected todays month');
var menu = $(MonthPicker_EventsField);
assert.ok(!menu.is(':visible'), 'Pressing enter closed the menu');
var mayOfNextYear = '05/' + (_today.getFullYear()+1);
field.MonthPicker('Open');
field.val( mayOfNextYear );
field.trigger($.Event('keydown', {keyCode: $.ui.keyCode.ENTER}));
assert.ok(!menu.is(':visible'), 'Pressing enter closed the menu');
assert.equal(field.val(), mayOfNextYear, "Pressing enter didn't override the month the user entered");
field.MonthPicker('Open');
field.trigger($.Event('keydown', {keyCode: $.ui.keyCode.ESCAPE}));
assert.ok(!menu.is(':visible'), 'Pressing escape closed the menu');
field.MonthPicker('Destroy');
});
QUnit.test('MonthFormat Option Tests', function (assert) {
// Create a month picker with a funky month format.
$(FormatField).val('10---> {2010}').MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
MonthFormat: 'm---> {yy}'
});
// Make sure the plugin parsed the month correctly (the date value is undefined).
var selectedDate = $(FormatField).MonthPicker('GetSelectedDate');
assert.equal(selectedDate.getFullYear(), 2010, 'The selected year is 2010');
assert.equal(selectedDate.getMonth() + 1, 10, 'The selected month is October');
// Change the format after init to sonething more appropriate.
$(FormatField).MonthPicker('option', 'MonthFormat', 'MM yy');
assert.equal($(FormatField).val(), 'October 2010', 'the text was updated to reflect the new format');
// Make sure the plugin parsed the month according to the new format.
selectedDate = $(FormatField).MonthPicker('GetSelectedDate');
assert.equal(selectedDate.getFullYear(), 2010, 'The selected year is still 2010');
assert.equal(selectedDate.getMonth() + 1, 10, 'The selected month is still October');
$(FormatField).val('January 2012');
selectedDate = $(FormatField).MonthPicker('GetSelectedDate');
assert.equal(selectedDate.getFullYear(), 2012, 'The selected year changed to 2012');
assert.equal(selectedDate.getMonth() + 1, 1, 'The selected month changed to January');
// Open the menu and choose April 2012.
$(FormatField).MonthPicker('Open');
$('.button-4', MonthPicker_FormatField).trigger('click');
// Make sure set the right text in the input field.
assert.equal($(FormatField).val(), 'April 2012', 'The text field has the value April 2012');
});
QUnit.test('Inline menu', function(assert) {
var field = $(InlineMenuDiv).MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
});
var menu = $(MonthPicker_InlineMenuDiv);
assert.ok(menu.width() <= 200, 'The menu is visible and has the expected width');
$(document.body).trigger('click');
assert.ok(menu.is(':visible'), 'The menu is still visible after clicking outside the menu');
menu.find('.button-1').trigger('click');
assert.ok(menu.is(':visible'), 'The menu is still visible after choosing a month');
assert.notOk($("#MonthPicker_Button_InlineMenuDiv").length, 'The default button was not created');
//field.MonthPicker('destroy');
});
QUnit.test('SelectedMonth option', function(assert) {
var field = $(InlineMenuDiv).MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
SelectedMonth: 0
});
var menu = $(MonthPicker_InlineMenuDiv);
var date = field.MonthPicker('GetSelectedDate');
assert.equal( date.getMonth(), _today.getMonth(), 'The correct month was selected');
assert.equal( date.getFullYear(), _today.getFullYear(), 'The correct year was selected');
assert.ok( menu.find('.button-' + (_today.getMonth()+1)).is('.ui-state-active'), 'The correct button is highlighted');
field.MonthPicker('option', 'SelectedMonth', '01/2015');
var date = field.MonthPicker('GetSelectedDate');
assert.ok( menu.find('.button-1').is('.ui-state-active'), 'January is selected after changing the SelectedMonth');
field.MonthPicker('Destroy');
});
// Makes sure that all events are triggered as expected.
// Perhaps we should consider removing some of these events.
QUnit.test('Events and context', function (assert) { // A.k.a duplicate code test.
// Good luck figuring out which callback is causing the
// problem if this test fails.
assert.expect(31);
var field = $(EventsField).MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
ShowOn: 'both'
});
var menu = $(MonthPicker_EventsField);
var OnBeforeMenuOpenTriggred = false;
// This event should be triggered twice, the first time it is prevented
// and the sceond time it goes through.
field.MonthPicker('option', 'OnBeforeMenuOpen', function(event) {
OnBeforeMenuOpenTriggred = true;
assert.equal( this, EventsField, 'OnBeforeMenuOpen was called in the right context' );
// Prevent opening if the event was triggered on the input field.
if (event.target === EventsField) {
event.preventDefault();
}
});
// Make sure the open event triggered by clicking the field was prevented.
field.trigger('click');
assert.ok(OnBeforeMenuOpenTriggred, 'The OnBeforeMenuOpen event was triggered');
assert.notOk( menu.is(':visible'), 'The open event was prevented when clicking on the field' );
var OnAfterMenuOpenTriggred = false;
field.MonthPicker('option', 'OnAfterMenuOpen', function() {
OnAfterMenuOpenTriggred = true;
assert.equal( this, EventsField, 'OnAfterMenuOpen was called in the right context' );
});
// Make sure calling the open method doesn't get prevented.
field.MonthPicker('Open');
assert.ok(OnAfterMenuOpenTriggred, 'The OnAfterMenuOpen event was triggered');
assert.ok( menu.is(':visible'), 'The menu was opend by calling the Open method' );
// Start duplicate code
var OnAfterNextYearTriggered = false;
field.MonthPicker('option', 'OnAfterNextYear', function() {
OnAfterNextYearTriggered = true;
assert.equal( this, EventsField, 'OnAfterNextYear was called in the right context' );
});
var nextYearButton = menu.find('.next-year>a');
nextYearButton.trigger('click');
assert.ok(OnAfterNextYearTriggered, 'Clicking the next button triggered OnAfterNextYear');
var OnAfterPreviousYearTriggerd = false;
field.MonthPicker('option', 'OnAfterPreviousYear', function() {
OnAfterPreviousYearTriggerd = true;
assert.equal( this, EventsField, 'OnAfterPreviousYear was called in the right context' );
});
var previousYearButton = menu.find('.previous-year>a');
previousYearButton.trigger('click');
assert.ok(OnAfterPreviousYearTriggerd, 'Clciking rhe previous button triggered OnAfterPreviousYear');
var OnAfterChooseYearsTriggerd = false;
field.MonthPicker('option', 'OnAfterChooseYears', function() {
OnAfterChooseYearsTriggerd = true;
assert.equal( this, EventsField, 'OnAfterChooseYears was called in the right context' );
});
var showYearsButton = menu.find('.year-container-all');
showYearsButton.trigger('click');
assert.ok(OnAfterChooseYearsTriggerd, 'Clicking the show years button triggered OnAfterChooseYears');
var OnAfterNextYearsTriggered = false;
field.MonthPicker('option', 'OnAfterNextYears', function() {
OnAfterNextYearsTriggered = true;
assert.equal( this, EventsField, 'OnAfterNextYears was called in the right context' );
});
nextYearButton.trigger('click');
assert.ok(OnAfterChooseYearsTriggerd, 'Clicking the next button triggered the OnAfterNextYears event');
var OnAfterPreviousYearsTriggered = false;
field.MonthPicker('option', 'OnAfterPreviousYears', function() {
OnAfterPreviousYearsTriggered = true;
assert.equal( this, EventsField, 'OnAfterPreviousYears was called in the right context' );
});
previousYearButton.trigger('click');
assert.ok(OnAfterPreviousYearsTriggered, 'Clicking the prev button triggered the OnAfterPreviousYears event');
var OnAfterChooseYearTriggered = false;
field.MonthPicker('option', 'OnAfterChooseYear', function() {
OnAfterChooseYearTriggered = true;
assert.equal( this, EventsField, 'OnAfterChooseYear was called in the right context' );
});
menu.find('.button-1').trigger('click');
assert.ok(OnAfterChooseYearTriggered, 'Clicking a year triggered OnAfterChooseYear');
var OnAfterChooseMonthTriggered = false;
field.MonthPicker('option', 'OnAfterChooseMonth', function(date) {
assert.ok(date instanceof Date, 'A date value was passed to OnAfterChooseMonth as the first argument');
OnAfterChooseMonthTriggered = true;
assert.equal( this, EventsField, 'OnAfterChooseMonth was called in the right context' );
});
// End duplicate code
// This event should be triggered twice, the first time it is prevented
// and the sceond time it goes through.
field.MonthPicker('option', 'OnBeforeMenuClose', function(event) {
assert.equal( this, EventsField, 'OnBeforeMenuClose was called in the right context' );
var target = event.target, btn1 = menu.find('.button-1')[0];
if (event.target === btn1) {
event.preventDefault();
}
});
// Click January which should triger the OnAfterChooseMonth event
// but the OnBeforeMenuClose will be prevented.
menu.find('.button-1').trigger('click');
assert.ok(OnAfterChooseMonthTriggered, 'Clicking January triggered OnAfterChooseMonth');
assert.ok( menu.is(':visible'), 'The close event was canceled' );
field.MonthPicker('option', 'OnAfterChooseMonth', $.noop);
var OnAfterMenuCloseTriggered = false;
field.MonthPicker('option', 'OnAfterMenuClose', function(event) {
OnAfterMenuCloseTriggered = true;
assert.equal( this, EventsField, 'OnAfterMenuClose was called in the right context' );
});
// Clicking May should not be prevented.
menu.find('.button-5').trigger('click');
assert.ok( !menu.is(':visible'), 'Clicking May closed the menu' );
assert.ok(OnAfterMenuCloseTriggered, 'Clicking May triggered the OnAfterMenuClose event');
field.MonthPicker('option', 'OnAfterSetDisabled', function(disabled) {
assert.equal( this, EventsField, 'OnAfterSetDisabled was called in the right context' );
assert.ok(disabled, 'Disabling the field passed true as the first argument to the OnAfterSetDisabled callback');
});
field.MonthPicker('Disable');
field.MonthPicker('option', 'OnAfterSetDisabled', function(disabled) {
assert.notOk(disabled, 'Enabling the field passed false as the first argument to the OnAfterSetDisabled callback');
});
field.MonthPicker('Enable');
field.MonthPicker('destroy');
});
QUnit.test('AltField and AltFormat tests', function( assert ) {
var hiddenField = $('<input type="hidden" name="AltField" />');
var field = $(MainAltField).MonthPicker({
SelectedMonth: '05/2010',
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
AltField: hiddenField,
AltFormat: 'yy-mm'
});
assert.equal(hiddenField.val(), '2010-05', "The secondary field has the main field's value in the alt format");
field.MonthPicker('Open');
var menu = $(MonthPicker_MainAltField);
menu.find('.button-1').trigger('click');
assert.equal( field.val(), '01/2010', 'The main field was populated');
assert.equal( hiddenField.val(), '2010-01', 'The secondary field was populated with a different format');
field.MonthPicker('option', 'AltFormat', null);
assert.equal( hiddenField.val(), '01/2010', 'Clearing AltFormat set the format to the MonthFormat');
field.MonthPicker('option', 'AltField', '#SecondaryAltField');
assert.equal( $(SecondaryAltField).val(), '01/2010', 'Changing the altField after init assigned the current value');
field.val('11/2015').trigger('change');
assert.equal( $(SecondaryAltField).val(), '11/2015', 'Triggering a change event on the main field updated the secondary field');
});
QUnit.test('Right to left', function (assert) {
var field = $(RTLField).MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
Position: {collision: 'none'}, // Ensure the menu opens to the right.
IsRTL: true
});
field.MonthPicker('Open');
var menu = $(MonthPicker_RTLField);
var nextYearButton = menu.find('.next-year>a');
var previousYearButton = menu.find('.previous-year>a');
// Make sure the buttons are pointing in the right (opposite) direction.
assert.ok(previousYearButton.find('span.ui-icon-circle-triangle-e').length, 'Previous button is pointed east');
assert.ok(nextYearButton.find('span.ui-icon-circle-triangle-w').length, 'Next button is pointed west');
// Make sure the menu opens to the right of the field.
//alert(field.position().left - menu.position().left);
var opendToTheRight = (field.position().left - menu.position().left) > 100;
assert.ok(opendToTheRight, 'The menu opened to the right of rhe field');
//field.MonthPicker('Close');
});
QUnit.test('Toggle method', function (assert) {
var field = $(ToggleField).MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
});
field.MonthPicker('Toggle');
var menu = $(MonthPicker_ToggleField);
assert.ok(menu.is(':visible'), 'The menu was opened');
field.MonthPicker('Toggle');
assert.ok(!menu.is(':visible'), 'The menu was closed');
field.MonthPicker('Toggle');
assert.ok(menu.is(':visible'), 'The menu was again');
field.MonthPicker('Close');
});
QUnit.module("Button option");
QUnit.test('Plain HTML Button test', function (assert) {
// Creats a month picker with a button similar to Datepicker's default button.
$(PlainButtonField).MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
Button: '<button class="PlainButton" id="PlainButton">...</button>'
});
// Make the button was properly construted and is visible in the expected location.
assert.ok($(PlainButton).is(':visible'), 'The plain button is present in the DOM and is visible.');
assert.equal($(PlainButton).prev()[0], PlainButtonField, 'The button was placed near the associated input field');
assert.equal(PlainButton.tagName, 'BUTTON', 'PlainButton is a button tag');
assert.equal($(PlainButton).text(), '...', 'The button has the correct label');
// Click the button and make sure the menu opens.
$(PlainButton).trigger('click');
assert.ok($(MonthPicker_PlainButtonField).is(':visible'), 'Clicking the plain button opened the correct menu.');
assert.equal($('.month-picker').filter(':visible').length, 1, 'There is only one menu opened.');
// Disable the field (which implicitly closes the menu)
// and make sure the button is also disabled.
$(PlainButtonField).MonthPicker('Disable');
assert.ok($(PlainButtonField).is(':disabled'), 'The input field was disabled.');
assert.ok($(PlainButton).is(':disabled'), 'The button was disabled.');
assert.ok($(MonthPicker_PlainButtonField).is(':hidden'), 'The menu was closed.');
assert.equal($('.PlainButton').length, 1, "There's only one plain button in the DOM.");
// Make sure clicking the button doesn't open the menu.
$(PlainButton).trigger('click');
assert.ok($(MonthPicker_PlainButtonField).is(':hidden'), "Clicking the button didn't open the menu.");
$(PlainButtonField).MonthPicker({
Button: '<input id="InputBtn" type="button" value="Click me" />'
});
assert.ok($(InputBtn).is(':disabled'), 'The new button is disabled');
});
QUnit.test('Img tag tests', function (assert) {
// Creats a month picker with an image as a button.
// The image should change according to the Disabled state.
$(ImgButtonField).MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
Button: function (options) {
var src = 'calendar' + (options.Disabled ? '-disabled' : '') + '.gif';
return '<img class="ImgButton" id="ImgButton" src="' + src + '" />';
}
});
// Make the image was properly construted and is visible in the expected location.
// NOTE: We don't check that the image is visible because it isn't visible
// until it was loaded.
assert.equal($(ImgButton).prev()[0], ImgButtonField, 'The image button was placed near the associated input field.');
assert.equal($(ImgButton).attr('src'), 'calendar.gif', 'The button has the enabled image.');
// Click the image and make sure the menu opens.
$(ImgButton).trigger('click');
assert.ok($(MonthPicker_ImgButtonField).is(":visible"), 'Clicking the plain button opened the correct menu.');
assert.equal($('.month-picker').filter(':visible').length, 1, 'There is only one menu opened.');
// Disable the field (which implicitly closes the menu)
// and make sure the button is also disabled.
$(ImgButtonField).MonthPicker('Disable');
assert.notStrictEqual($('.ImgButton').prop('disabled'), true, "The plugin didn't try to disable the img button");
assert.ok($(ImgButtonField).is(':disabled'), 'The input field was disabled.');
assert.ok($(MonthPicker_ImgButtonField).is(':hidden'), 'The menu was closed.');
assert.equal($(ImgButton).attr('src'), 'calendar-disabled.gif', 'The button has the disabled image.');
assert.equal($('.ImgButton').length, 1, "There's only one image button in the DOM.");
// Make sure clicking the button doesn't open the menu.
$(ImgButton).trigger('click');
assert.ok($(MonthPicker_ImgButtonField).is(':hidden'), "Clicking the button didn't open the menu.");
$(ImgButtonField).MonthPicker('Destroy');
assert.equal($('#MonthPicker_ExistingButtonField').length, 0, 'The meun was removed from the DOM');
assert.equal($('.ImgButton').length, 0, 'The button was removed from the DOM');
});
QUnit.test('Existing element tests', function (assert) {
// Creats a month picker with an existing external element as a button.
$(ExistingButtonField).MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
Button: function (options) {
var enabledButton = $(this).next().hide();
var disabledButton = $(enabledButton).next().hide();
return options.Disabled ? disabledButton.show() : enabledButton.show();
}
});
// Make sure only the enabled element is visible.
assert.ok($(ExistingButton).is(':visible'), 'The enabled element is visible.');
assert.ok($(DisabledExistingButton).is(':hidden'), 'The disabled element is hidden.');
// Click the element and make sure the menu opens.
$(ExistingButton).trigger('click');
assert.ok($(MonthPicker_ExistingButtonField).is(":visible"), 'Clicking the plain button opened the correct menu.');
assert.equal($('.month-picker').filter(':visible').length, 1, 'There is only one menu opened.');
// Disable the field (which implicitly closes the menu)
// and make sure the button is also disabled.
$(ExistingButtonField).MonthPicker('Disable');
assert.ok($(ExistingButtonField).is(':disabled'), 'The input field was disabled.');
assert.ok($(ExistingButton).is(':hidden'), 'The enabled element is hidden.');
assert.ok($(DisabledExistingButton).is(':visible'), 'The disabled element is visible.');
assert.ok($(MonthPicker_ExistingButtonField).is(':hidden'), 'The menu was closed.');
// Make sure clicking the disabled element doesn't open the menu.
$(DisabledExistingButton).trigger('click');
assert.ok($(MonthPicker_ExistingButtonField).is(':hidden'), "Clicking the disabled element didn't open the menu.");
// Re-enable the menu and make sure it opens when you click on the fit
$(ExistingButtonField).MonthPicker('Enable');
$(ExistingButton).trigger('click');
assert.ok($(MonthPicker_ExistingButtonField).is(':visible'), "Clicking the enabled element after re-enabling opend the menu");
// Close the menu to make sure that clicking the button the click event.
// In case it wasen't removed triggering a click should throw an error.
$(ExistingButtonField).MonthPicker('Close');
// Make sure we don't remove existing elements when the plugin is destroyed.
$(ExistingButtonField).MonthPicker('destroy');
assert.equal($("#MonthPicker_ExistingButtonField").length, 0, 'The meun was removed from the DOM');
assert.ok(ExistingButton, 'The existing button was not removed from the DOM');
// Make sure that clicking the existing element after destroying
// the plugin doesn't thorw an error.
$(ExistingButton).trigger('click');
});
QUnit.test('Change selector after init', function (assert) {
// In this test we are going to change the Button option to a jQuery selector
// aftet the plugin was initialized.
$(SelectorButtonField).MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
Button: '#SelectorButton'
});
// Click the element and make sure the menu opens.
$(SelectorButton).trigger('click');
assert.ok($(MonthPicker_SelectorButtonField).is(":visible"), 'Clicking the button opened the correct menu.');
assert.equal($('.month-picker').filter(':visible').length, 1, 'There is only one menu opened.');
// Close the menu and change the button after initialization.
$(SelectorButtonField).MonthPicker('Close');
$(SelectorButtonField).MonthPicker('option', 'Button', '#OtherSelectorButton');
// Make sure the buttons remain untouched.
assert.ok($(SelectorButton).is(':visible'), 'The enabled element was not touched.');
// Make sure clicking the old button doesn't open the menu (the event listeners were removed).
$(SelectorButton).trigger('click');
assert.ok($(MonthPicker_SelectorButtonField).is(":hidden"), "Clicking the old button didn't open the menu");
// Make sure clicking the new button opens the menu.
$(OtherSelectorButton).trigger('click');
assert.ok($(MonthPicker_SelectorButtonField).is(":visible"), 'Clicking the other button opened the correct menu.');
// Close the menu to make sure that clicking the button the click event.
// In case it wasen't removed triggering a click should throw an error.
$(SelectorButtonField).MonthPicker('Close');
// Destroy the plugin and make sure the event listeners were removed.
$(SelectorButtonField).MonthPicker('destroy');
assert.equal($("#MonthPicker_SelectorButtonField").length, 0, 'The month picker menu was removed.');
$(OtherSelectorButton).trigger('click');
});
QUnit.test('Disable button', function (assert) {
// Create a month picker and make sure that clicking the input field opens the menu.
var field = $(NoButtonField).MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
Button: false
});
assert.equal($('#MonthPicker_Button_NoButtonField').length, 0, "The plugin didn't create a button");
var menu = $(MonthPicker_NoButtonField);
// Make sure focusing on the field opens the menu.
field.trigger('focus');
assert.ok(menu.css('visibility') !== 'hidden', 'The menu was opened by focusing on the input field');
// Make pressing tab closes the menu.
field.trigger($.Event( "keydown", { keyCode: $.ui.keyCode.TAB } ));
assert.notOk(menu.is(':visible'), 'The menu was closed by pressing tab');
// Make sure that clicking the input field opens the menu.
$(NoButtonField).trigger('click');
assert.ok(menu.is(':visible'), 'Clicking the input field opened the correct menu.');
// Don't leave the menu open (not really necessary).
$(NoButtonField).MonthPicker('Disable');
// Make sure clicking the input field when it's disabled doesn't open the menu.
$(NoButtonField).trigger('click');
assert.ok(menu.is(':hidden'), 'Clicking the input field did not open the menu.');
// Don't leave the menu open (not really necessary).
$(NoButtonField).MonthPicker('Enable');
// Fails the test if the menu was closed after clicking the input field again.
$(NoButtonField).MonthPicker({
OnBeforeMenuClose: function () {
assert.notOk(true, "The menu shouldn't closed after clicking the input field again.");
}
});
// Make sure that clicking the input field still opens the menu.
$(NoButtonField).trigger('click');
assert.ok(menu.is(':visible'), 'Clicking the input field still openes the correct menu.');
$(NoButtonField).trigger('click');
// Don't leave the menu open (not really necessary).
$(NoButtonField).MonthPicker('ClearAllCallbacks');
$(NoButtonField).MonthPicker('Close');
});
QUnit.test('ShowOn both', function (assert) {
var field = $(ShowOnBothField).MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
Button: '<button id="ShowOnBtn">...</button>',
ShowOn: 'both'
});
field.trigger('click');
var menu = $(MonthPicker_ShowOnBothField);
assert.ok(menu.is(':visible'), 'The menu was opened by clicking on the input field');
field.MonthPicker('Close');
assert.ok(!menu.is(':visible'), 'The menu was closed');
$(ShowOnBtn).trigger('click');
assert.ok(menu.is(':visible'), 'The menu was opened by clicking on the button');
field.MonthPicker('Close');
assert.notOk(menu.is(':visible'), 'The menu was closed');
// Make sure focusing on the field opens the menu.
field.trigger('focus');
assert.ok(menu.is(':visible'), 'The menu was opened by focusing on the input field');
// Make pressing tab closes the menu.
field.trigger($.Event( "keydown", { keyCode: $.ui.keyCode.TAB } ));
assert.notOk(menu.is(':visible'), 'The menu was closed by pressing tab');
});
QUnit.module("Min/MaxMonth");
QUnit.test('Month buttons are disabled', function (assert) {
var field = $(RistrictMonthField).val('12/2015').MonthPicker({
Animation: 'none', // Disable animation to make sure opening and closing the menu is synchronous.
MinMonth: '10/2015'
});
field.MonthPicker('Open');
var menu = $(MonthPicker_RistrictMonthField);
var previousYearButton = menu.find('.previous-year>a');
var nextYearButton = menu.find('.next-year>a');
// Try to click the disabled buttons.
var buttons = menu.find('.month-picker-month-table button');
$(buttons.slice(0, 8)).trigger('click');
assert.ok(previousYearButton.is('.ui-button-disabled'), 'The previous year button is disabled');
// Make sure that the date didn't change as a
// result of clicking the disabled button.
var selectedMonth = field.MonthPicker('GetSelectedDate');
assert.equal( selectedMonth.getFullYear(), 2015, 'The selected year is still 2015');
assert.equal( selectedMonth.getMonth() + 1, 12, 'The selected month is still December');
assert.equal( field.val(), '12/2015', 'The input field still has the value 12/2015' );
// Make sure we can still go to the next year.
nextYearButton.trigger('click');
var pickerYear = parseInt(menu.find('.year').text(), 10);
assert.equal(pickerYear, 2016, 'Clicking next year changed the year to 2016');
// Make none of the buttons are disabled.
assert.ok( !menu.find('button').is('.ui-button-disabled'), 'None of the buttons are disabled');
// Make sure clicking the first button selected January 2016.
menu.find('.button-1').trigger('click');
var selectedMonth = field.MonthPicker('GetSelectedDate');
assert.equal( selectedMonth.getFullYear(), 2016, 'The selected year is still 2016');
assert.equal( selectedMonth.getMonth() + 1, 1, 'The selected month is still January');
assert.equal( field.val(), '01/2016', 'The input field has the value 01/2016' );
// Make sure we can only go back to 2015 a.k.a the minimum year.
previousYearButton.trigger('click');
previousYearButton.trigger('click');
var pickerYear = parseInt(menu.find('.year').text(), 10);
assert.equal(pickerYear, 2015, 'clicking previous year tweice keept the year at 2015');
// Make sure that month buttons before October (the minimum month)
// are still disabled after navigating back to 2015
// by clicking the buttons and checking that the
// selected date didn't change.
$(buttons.slice(0, 8)).trigger('click');
var selectedMonth = field.MonthPicker('GetSelectedDate');
assert.equal( selectedMonth.getFullYear(), 2016, "Clciking the buttons didn't change the selected year");
assert.equal( selectedMonth.getMonth() + 1, 1, "Clciking the buttons didn't change the selected month");
assert.equal( field.val(), '01/2016', "Clciking the buttons didn't change the fields value" );
// Make sure the buttons after October (the minumum month) are enabled.
var buttonsDisabled = $( buttons.slice(9) ).is('.ui-button-disabled');
assert.notOk(buttonsDisabled, 'All buttons after the minumum month are enabled');