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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
|
describe('jquery-impromptu', function() {
// ====================================================================================
// ====================================================================================
describe('base structure', function(){
// ====================================================================================
describe('basic initialization', function() {
beforeEach(function() {
$.fx.off = true; // for our testing lets turn off fx
});
afterEach(function() {
$.prompt.close();
});
it('should be defined', function() {
expect($.prompt).not.toBeUndefined();
});
it('should generate markup', function() {
var expectedTitle = 'This is a title',
expectedText = 'This is a test';
$.prompt(expectedText, { title: expectedTitle });
expect($('.jqibox')).toExist();
expect($('.jqifade')).toExist();
expect($('.jqi')).toExist();
expect($('.jqi .jqititle')).toHaveText(expectedTitle);
expect($('.jqi .jqimessage')).toHaveText(expectedText);
});
});
// ====================================================================================
describe('button creation', function() {
beforeEach(function() {
$.fx.off = true; // for our testing lets turn off fx
});
afterEach(function() {
$.prompt.close();
});
it('should generate buttons from hash', function() {
$.prompt('This is a test', {
buttons: { Ok:true, Cancel:false }
});
var okBtn = $('#jqi_state0_buttonOk'),
cancelBtn = $('#jqi_state0_buttonCancel');
expect($('.jqibutton')).toHaveLength(2);
expect(okBtn).toExist();
expect(cancelBtn).toExist();
expect(okBtn).toHaveText('Ok');
expect(cancelBtn).toHaveText('Cancel');
expect(okBtn).toHaveValue('true');
expect(cancelBtn).toHaveValue('false');
});
it('should generate buttons from array', function() {
$.prompt('This is a test', {
buttons: [
{ title: 'Ok', value: true },
{ title: 'Cancel', value: false }
]
});
var okBtn = $('#jqi_state0_buttonOk'),
cancelBtn = $('#jqi_state0_buttonCancel');
expect($('.jqibutton')).toHaveLength(2);
expect(okBtn).toExist();
expect(cancelBtn).toExist();
expect(okBtn).toHaveText('Ok');
expect(cancelBtn).toHaveText('Cancel');
expect(okBtn.val()).toBe('true');
expect(cancelBtn.val()).toBe('false');
});
it('should add classes to buttons', function() {
$.prompt('This is a test', {
buttons: [
{ title: 'Ok', value: true, classes: ['ok1','ok2'] },
{ title: 'Cancel', value: false, classes: 'cancel1 cancel2' }
]
});
var okBtn = $('#jqi_state0_buttonOk'),
cancelBtn = $('#jqi_state0_buttonCancel');
expect(okBtn).toHaveClass('ok1');
expect(okBtn).toHaveClass('ok2');
expect(cancelBtn).toHaveClass('cancel1');
expect(cancelBtn).toHaveClass('cancel2');
});
it('should add classes to buttons from classes obj', function() {
$.prompt('This is a test', {
buttons: [
{ title: 'Ok', value: true, classes: ['ok1','ok2'] },
{ title: 'Cancel', value: false, classes: 'cancel1 cancel2' }
],
classes: { button: 'testclass' }
});
var okBtn = $('#jqi_state0_buttonOk'),
cancelBtn = $('#jqi_state0_buttonCancel');
expect(okBtn).toHaveClass('testclass');
expect(cancelBtn).toHaveClass('testclass');
});
it('should default correct button', function() {
$.prompt('This is a test', {
buttons: [
{ title: 'Ok', value: 1 },
{ title: 'Cancel', value: 2 },
{ title: 'Another', value: 3 }
],
focus: 1
});
var okBtn = $('#jqi_state0_buttonOk'),
cancelBtn = $('#jqi_state0_buttonCancel'),
anotherBtn = $('#jqi_state0_buttonAnother');
expect(okBtn).not.toHaveClass('jqidefaultbutton');
expect(cancelBtn).toHaveClass('jqidefaultbutton');
expect(anotherBtn).not.toHaveClass('jqidefaultbutton');
});
it('should default correct button when focus on an input', function() {
$.prompt('This is a test <input type="text" id="testInput" />', {
buttons: [
{ title: 'Ok', value: 1 },
{ title: 'Cancel', value: 2 },
{ title: 'Another', value: 3 }
],
focus: '#testInput',
defaultButton: 1
});
var okBtn = $('#jqi_state0_buttonOk'),
cancelBtn = $('#jqi_state0_buttonCancel'),
anotherBtn = $('#jqi_state0_buttonAnother');
expect(okBtn).not.toHaveClass('jqidefaultbutton');
expect(cancelBtn).toHaveClass('jqidefaultbutton');
expect(anotherBtn).not.toHaveClass('jqidefaultbutton');
});
});
// ====================================================================================
describe('state creation', function() {
beforeEach(function() {
$.fx.off = true; // for our testing lets turn off fx
});
afterEach(function() {
$.prompt.close();
});
it('should create a single state from string', function() {
$.prompt('This is a test');
expect($('.jqistate')).toExist();
});
it('should create states from hash', function() {
var states = {
s1: { html: 'state 1' },
s2: { html: 'state 2' },
s3: { html: 'state 3' }
};
$.prompt(states);
expect($('.jqistate')).toHaveLength(3);
expect($('#jqistate_s1 .jqimessage')).toHaveText(states.s1.html);
expect($('#jqistate_s2 .jqimessage')).toHaveText(states.s2.html);
expect($('#jqistate_s3 .jqimessage')).toHaveText(states.s3.html);
});
it('should create states from array', function() {
var states = [
{ html: 'state 1' },
{ html: 'state 2' },
{ html: 'state 3' }
];
$.prompt(states);
expect($('.jqistate')).toHaveLength(3);
expect($('#jqistate_0 .jqimessage')).toHaveText(states[0].html);
expect($('#jqistate_1 .jqimessage')).toHaveText(states[1].html);
expect($('#jqistate_2 .jqimessage')).toHaveText(states[2].html);
});
it('should show the first state automatically', function() {
// we can't reliably determine which entry is the first with a hash, js doesn't preserve order
var states = [
{ html: 'state 1' },
{ html: 'state 2' },
{ html: 'state 3' }
];
$.prompt(states);
expect($('#jqistate_0')).toHaveCss({display:'block'});
expect($('#jqistate_1')).toHaveCss({display:'none'});
expect($('#jqistate_2')).toHaveCss({display:'none'});
});
it('should name states properly when name specified', function() {
var states = [
{ name: 's1', html: 'state 1' },
{ name: 's2', html: 'state 2' },
{ name: 's3', html: 'state 3' }
];
$.prompt(states);
expect($('#jqistate_s1')).toExist();
expect($('#jqistate_s2')).toExist();
expect($('#jqistate_s3')).toExist();
});
});
}); // base structure
// ====================================================================================
// ====================================================================================
describe('api methods', function() {
var states = [
{ name: 's1', html: 'state 1' },
{ name: 's2', html: 'state 2' },
{ name: 's3', html: 'state 3' }
];
beforeEach(function() {
$.fx.off = true; // for our testing lets turn off fx
});
afterEach(function() {
$.prompt.close();
});
// ====================================================================================
describe('$.prompt.setDefaults()', function() {
it('should change the default values', function() {
var origDefs = $.extend(true, {}, $.prompt.defaults),
overrides = { prefix: 'myjqi', classes: { box: 'boxclass' } };
$.prompt.setDefaults(overrides);
expect($.prompt.defaults.prefix).toBe(overrides.prefix);
expect($.prompt.defaults.classes.box).toBe(overrides.classes.box);
expect($.prompt.defaults.speed).toBe(origDefs.speed);
$.prompt.defaults = origDefs;
});
});
// ====================================================================================
describe('$.prompt.setStateDefaults()', function() {
it('should change the default state values', function() {
var origDefs = $.extend(true, {}, $.prompt.defaults),
overrides = { title: 'My Title', position: { width: 123 } };
$.prompt.setStateDefaults(overrides);
expect($.prompt.defaults.state.title).toBe(overrides.title);
expect($.prompt.defaults.state.position.width).toBe(overrides.position.width);
expect($.prompt.defaults.state.focus).toBe(origDefs.state.focus);
$.prompt.defaults = origDefs;
});
});
// ====================================================================================
describe('$.prompt.get()', function() {
it('should return the prompt jquery object', function() {
$.prompt('This is a test');
var actualResult = $.prompt.get(),
expectedResult = $('.jqi');
expect(actualResult[0]).toBe(expectedResult[0]);
});
});
// ====================================================================================
describe('$.prompt.getState()', function() {
it('should return the state jquery object', function() {
$.prompt(states);
var actualResult = $.prompt.getState('s2'),
expectedResult = $('#jqistate_s2');
expect(actualResult[0]).toBe(expectedResult[0]);
});
});
// ====================================================================================
describe('$.prompt.getCurrentState()', function() {
it('should return the current state jquery object', function() {
$.prompt(states);
var actualResult = $.prompt.getCurrentState(),
expectedResult = $('#jqistate_s1');
expect(actualResult[0]).toBe(expectedResult[0]);
});
it('should return the current state jquery object after a state change', function() {
$.prompt(states);
$.prompt.goToState('s2');
var actualResult = $.prompt.getCurrentState(),
expectedResult = $('#jqistate_s2');
expect(actualResult[0]).toBe(expectedResult[0]);
});
});
// ====================================================================================
describe('$.prompt.getCurrentStateName()', function() {
it('should return the current state name', function() {
$.prompt(states);
var actualResult = $.prompt.getCurrentStateName(),
expectedResult = 's1';
expect(actualResult).toBe(expectedResult);
});
it('should return the current state name after a state change', function() {
$.prompt(states);
$.prompt.goToState('s2');
var actualResult = $.prompt.getCurrentStateName(),
expectedResult = 's2';
expect(actualResult).toBe(expectedResult);
});
});
// ====================================================================================
describe('$.prompt.goToState()', function() {
it('should make the requested state visible', function() {
$.prompt(states);
$.prompt.goToState('s3');
expect($('#jqistate_s1')).toHaveCss({display:'none'});
expect($('#jqistate_s2')).toHaveCss({display:'none'});
expect($('#jqistate_s3')).toHaveCss({display:'block'});
});
it('should do nothing if the state is not available', function() {
$.prompt(states);
$.prompt.goToState('s4');
expect($('#jqistate_s1')).toHaveCss({display:'block'});
expect($('#jqistate_s2')).toHaveCss({display:'none'});
expect($('#jqistate_s3')).toHaveCss({display:'none'});
});
it('should handle substate option', function() {
$.prompt(states);
$.prompt.goToState('s2',true);
expect($('#jqistate_s1')).toHaveCss({display:'block'});
expect($('#jqistate_s2')).toHaveCss({display:'block'});
expect($('#jqistate_s3')).toHaveCss({display:'none'});
expect($('#jqistate_s2')).toHaveClass('jqisubstate');
});
});
// ====================================================================================
describe('$.prompt.nextState()', function() {
it('should make the next state visible', function() {
$.prompt(states);
$.prompt.nextState();
expect($('#jqistate_s1')).toHaveCss({display:'none'});
expect($('#jqistate_s2')).toHaveCss({display:'block'});
expect($('#jqistate_s3')).toHaveCss({display:'none'});
});
it('should do nothing if the state is not available', function() {
$.prompt(states);
$.prompt.goToState('s3');
$.prompt.nextState();
expect($('#jqistate_s1')).toHaveCss({display:'none'});
expect($('#jqistate_s2')).toHaveCss({display:'none'});
expect($('#jqistate_s3')).toHaveCss({display:'block'});
});
});
// ====================================================================================
describe('$.prompt.prevState()', function() {
it('should make the previous state visible', function() {
$.prompt(states);
$.prompt.goToState('s3');
$.prompt.prevState();
expect($('#jqistate_s1')).toHaveCss({display:'none'});
expect($('#jqistate_s2')).toHaveCss({display:'block'});
expect($('#jqistate_s3')).toHaveCss({display:'none'});
});
it('should do nothing if the state is not available', function() {
$.prompt(states);
$.prompt.prevState();
expect($('#jqistate_s1')).toHaveCss({display:'block'});
expect($('#jqistate_s2')).toHaveCss({display:'none'});
expect($('#jqistate_s3')).toHaveCss({display:'none'});
});
});
// ====================================================================================
describe('$.prompt.addState()', function() {
it('should add a new state as the last state', function() {
var newState = {
name: 's4',
title: 's4',
html: 'testing s4',
buttons: { Ok:true,Cancel:false}
};
$.prompt(states);
var $stateobj = $.prompt.addState(newState.name, newState);
// element created?
expect($stateobj).toExist();
// element in the right place?
expect($stateobj.prev()).toHaveId('jqistate_s3');
// element visibility correct?
expect($('#jqistate_s1')).toHaveCss({display:'block'});
expect($stateobj).toHaveCss({display:'none'});
// content generated ok?
expect($stateobj.find('.jqimessage')).toHaveText(newState.html);
expect($stateobj.find('.jqititle')).toHaveText(newState.title);
expect($stateobj.find('.jqibutton')).toHaveLength(2);
});
it('should add a new state after specified state', function() {
var newState = {
name: 's4',
title: 's4',
html: 'testing s4',
buttons: { Ok:true,Cancel:false}
},
afterState = 's2';
$.prompt(states);
var $stateobj = $.prompt.addState(newState.name, newState, afterState);
expect($stateobj.prev()).toHaveId('jqistate_'+afterState);
});
});
// ====================================================================================
describe('$.prompt.removeState()', function() {
it('should remove the specified state', function() {
$.prompt(states);
$.prompt.removeState('s2');
expect($('#jqistate_s2')).not.toExist();
});
it('should display requested state', function() {
$.prompt(states);
$.prompt.removeState('s1','s3');
expect($('#jqistate_s2')).toHaveCss({display:'none'});
expect($('#jqistate_s3')).toHaveCss({display:'block'});
});
it('should display next state', function() {
$.prompt(states);
$.prompt.removeState('s1');
expect($('#jqistate_s2')).toHaveCss({display:'block'});
expect($('#jqistate_s3')).toHaveCss({display:'none'});
});
it('should display previous state', function() {
$.prompt(states);
$.prompt.goToState('s3');
$.prompt.removeState('s3');
expect($('#jqistate_s1')).toHaveCss({display:'none'});
expect($('#jqistate_s2')).toHaveCss({display:'block'});
});
});
}); // end api methods
// ====================================================================================
// ====================================================================================
describe('api events', function() {
var states = [
{ name: 's1', html: 'state 1', buttons: { next: true, cancel: false } },
{ name: 's2', html: 'state 2', buttons: { back: -1, cancel: 0, next: 1 } },
{ name: 's3', html: 'state 3', buttons: { done: true} }
],
maxWaitTime = 100;
beforeEach(function() {
$.fx.off = true; // for our testing lets turn off fx
});
afterEach(function() {
$.prompt.close();
});
// ====================================================================================
describe('impromptu:loaded', function(){
it('should fire event', function(){
var spyEventCalled = false;
$('body').on('impromptu:loaded', '.jqibox', function(){ spyEventCalled = true; });
$.prompt(states);
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
});
});
it('should allow event function as option parameter', function(){
var spyEventCalled = false;
$.prompt(states, { loaded: function(){ spyEventCalled = true; } });
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
});
});
});
// ====================================================================================
describe('impromptu:close', function(){
it('should fire event', function(){
var spyEventCalled = false;
$('body').on('impromptu:close', '.jqibox', function(){ spyEventCalled = true; });
$.prompt(states, {
loaded: function(){
$.prompt.close();
}
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
});
});
it('should allow event function as option parameter', function(){
var spyEventCalled = false;
$.prompt(states, {
loaded: function(){ $.prompt.close(); },
close: function(){ spyEventCalled = true; }
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
});
});
});
// ====================================================================================
describe('impromptu:statechanging', function(){
it('should fire event', function(){
var spyEventCalled = false;
$('body').on('impromptu:statechanging', '.jqibox', function(){ spyEventCalled = true; });
$.prompt(states, {
loaded: function(){
$.prompt.goToState('s2');
}
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
});
});
it('should allow event function as option parameter', function(){
var spyEventCalled = false;
$.prompt(states, {
loaded: function(){
$.prompt.goToState('s2');
},
statechanging: function(){ spyEventCalled = true; }
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
});
});
it('should allow preventDefault', function(){
var spyEvent = spyOnEvent('body', 'impromptu:statechanging');
$.prompt(states, {
loaded: function(){
$.prompt.goToState('s2');
},
statechanging: function(e){
e.preventDefault();
}
});
expect(spyEvent).toHaveBeenTriggered();
expect(spyEvent).toHaveBeenPrevented();
});
});
// ====================================================================================
describe('impromptu:statechanged', function(){
it('should fire event', function(){
var spyEventCalled = false;
$('body').on('impromptu:statechanged', '.jqibox', function(){ spyEventCalled = true; });
$.prompt(states, {
loaded: function(){
$.prompt.goToState('s2');
}
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
});
});
it('should allow event function as option parameter', function(){
var spyEventCalled = false;
$.prompt(states, {
loaded: function(){
$.prompt.goToState('s2');
},
statechanged: function(){ spyEventCalled = true; }
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
});
});
});
// ====================================================================================
describe('impromptu:submit', function(){
it('should fire event', function(){
var spyEventCalled = false;
$('body').on('impromptu:submit', '.jqibox', function(){ spyEventCalled = true; });
$.prompt(states, {
loaded: function(){
$.prompt.getState('s1').find('.jqibutton:first').click();
}
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
});
});
it('should allow event function as option parameter if string message', function(){
var spyEventCalled = false;
$.prompt('Test message', {
loaded: function(){
$('.jqibutton:first').click();
},
submit: function(){ spyEventCalled = true; }
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
});
});
it('should detect button clicked', function(){
var spyEventCalled = false,
btnClicked,
msgReturned,
formVals;
$('body').on('impromptu:submit', '.jqibox', function(e,v,m,f){
btnClicked = v;
msgReturned = m;
formVals = f;
spyEventCalled = true;
});
$.prompt(states, {
loaded: function(){
$.prompt.getState('s1').find('#jqi_s1_buttoncancel').click();
}
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(btnClicked).toBe(false);
});
});
it('should pass the state message', function(){
var spyEventCalled = false,
btnClicked,
msgReturned,
formVals;
$('body').on('impromptu:submit', '.jqibox', function(e,v,m,f){
btnClicked = v;
msgReturned = m;
formVals = f;
spyEventCalled = true;
});
$.prompt(states, {
loaded: function(){
$.prompt.getState('s1').find('#jqi_s1_buttoncancel').click();
}
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(msgReturned).toBe('.jqimessage');
});
});
it('should pass the prompt form values', function(){
var spyEventCalled = false,
tmpStates = [],
btnClicked,
msgReturned,
formVals,
expectedValues = {
textInput: 'my text input',
selectSingle: 'select single 3',
selectMulti: ['select multi 2', 'select multi 3'],
radioInput: 'my radio yes',
chkInput: ['my chk no', 'my chk maybe'],
textareaInput: 'my textarea val'
};
tmpStates[0] = $.extend({}, states[0]);
tmpStates[0].html = '<input type="text" name="textInput" value="my text input" />'+
'<select name="selectSingle"><option value="select single 1">select single 1</option><option value="select single 2">select single 2</option><option value="select single 3" selected>select single 3</option></select>'+
'<select name="selectMulti" multiple><option value="select multi1">select multi 1</option><option value="select multi 2" selected>select multi 2</option><option value="select multi 3" selected>select multi 3</option></select>';
tmpStates[1] = $.extend({}, states[1]);
tmpStates[1].html = '<input type="radio" name="radioInput" value="my radio yes" checked />'+
'<input type="radio" name="radioInput" value="my radio no" />'+
'<input type="checkbox" name="chkInput" value="my chk no" checked />'+
'<input type="checkbox" name="chkInput" value="my chk yes" />'+
'<input type="checkbox" name="chkInput" value="my chk maybe" checked />';
tmpStates[3] = $.extend({}, states[3]);
tmpStates[3].html = '<textarea name="textareaInput">my textarea val</textarea>';
$('body').on('impromptu:submit', '.jqibox', function(e,v,m,f){
btnClicked = v;
msgReturned = m;
formVals = f;
spyEventCalled = true;
});
$.prompt(tmpStates, {
loaded: function(){
$.prompt.getState('s1').find('#jqi_s1_buttonnext').click();
}
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(formVals).toEqual(expectedValues);
});
});
});
}); // end qpi events
// ====================================================================================
// ====================================================================================
describe('native events', function() {
var states = [
{ name: 's1', html: 'state 1', buttons: [{ title:'One', value: 1}, { title:'Two', value: 2}, { title:'Three', value: 3 }], focus: 1 },
{ name: 's2', html: 'state 2', buttons: { back: -1, cancel: 0, next: 1 } },
{ name: 's3', html: 'state 3', buttons: { done: true} }
],
maxWaitTime = 100;
beforeEach(function() {
$.fx.off = true; // for our testing lets turn off fx
});
afterEach(function() {
$.prompt.close();
});
// ====================================================================================
describe('keydown', function(){
it('should not close on escape key if persistent option true', function(){
var spyEventCalled = false;
$.prompt(states, {
loaded: function(){
var e = $.Event('keydown');
e.keyCode = 27;
$.prompt.jqib.trigger(e);
spyEventCalled = true;
},
persistent: true
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
expect($('.jqi')).toExist();
});
});
it('should close on escape key if persistent option false', function(){
var spyEventCalled = false;
$.prompt(states, {
loaded: function(){
var e = $.Event('keydown');
e.keyCode = 27;
$.prompt.jqib.trigger(e);
spyEventCalled = true;
},
persistent: false
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
expect($('.jqi')).not.toExist();
});
});
it('should trigger default button if enter key', function(){
var spyEventCalled = false,
buttonTriggered = null;
$('body').on('impromptu:submit', function(e,v){
spyEventCalled = true;
buttonTriggered = v;
});
$.prompt(states, {
loaded: function(){
var e = $.Event('keydown');
e.keyCode = 13;
$.prompt.jqi.trigger(e);
}
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
expect(buttonTriggered).toBe(2);
});
});
});
// ====================================================================================
describe('click', function(){
it('should not close fade click if persistent option true', function(){
var spyEventCalled = false;
$.prompt(states, {
loaded: function(){
var e = $.Event('click');
$.prompt.jqib.trigger(e);
spyEventCalled = true;
},
persistent: true
});
waitsFor(function(){
return spyEventCalled;
}, 'event should have been called',maxWaitTime);
runs(function(){
expect(spyEventCalled).toBe(true);
expect($('.jqi')).toExist();
});
});
});
});// end native events
});
|