This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
/
jasmine-jquery-spec.js
1487 lines (1229 loc) · 52.1 KB
/
jasmine-jquery-spec.js
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
describe("jasmine.Fixtures", function() {
var ajaxData = 'some ajax data'
var fixtureUrl = 'some_url'
var anotherFixtureUrl = 'another_url'
var fixturesContainer = function() {
return $('#' + jasmine.getFixtures().containerId)
}
var appendFixturesContainerToDom = function() {
$('body').append('<div id="' + jasmine.getFixtures().containerId + '">old content</div>')
}
beforeEach(function() {
jasmine.getFixtures().clearCache()
spyOn(jasmine.Fixtures.prototype, 'loadFixtureIntoCache_').andCallFake(function(relativeUrl){
this.fixturesCache_[relativeUrl] = ajaxData
})
})
describe("default initial config values", function() {
it("should set 'jasmine-fixtures' as the default container id", function() {
expect(jasmine.getFixtures().containerId).toEqual('jasmine-fixtures')
})
it("should set 'spec/javascripts/fixtures' as the default fixtures path", function() {
expect(jasmine.getFixtures().fixturesPath).toEqual('spec/javascripts/fixtures')
})
})
describe("cache", function() {
describe("clearCache", function() {
it("should clear cache and in effect force subsequent AJAX call", function() {
jasmine.getFixtures().read(fixtureUrl)
jasmine.getFixtures().clearCache()
jasmine.getFixtures().read(fixtureUrl)
expect(jasmine.Fixtures.prototype.loadFixtureIntoCache_.callCount).toEqual(2)
})
})
it("first-time read should go through AJAX", function() {
jasmine.getFixtures().read(fixtureUrl)
expect(jasmine.Fixtures.prototype.loadFixtureIntoCache_.callCount).toEqual(1)
})
it("subsequent read from the same URL should go from cache", function() {
jasmine.getFixtures().read(fixtureUrl, fixtureUrl)
expect(jasmine.Fixtures.prototype.loadFixtureIntoCache_.callCount).toEqual(1)
});
})
describe("read", function() {
it("should return fixture HTML", function() {
var html = jasmine.getFixtures().read(fixtureUrl)
expect(html).toEqual(ajaxData)
})
it("should return duplicated HTML of a fixture when its url is provided twice in a single call", function() {
var html = jasmine.getFixtures().read(fixtureUrl, fixtureUrl)
expect(html).toEqual(ajaxData + ajaxData)
})
it("should return merged HTML of two fixtures when two different urls are provided in a single call", function() {
var html = jasmine.getFixtures().read(fixtureUrl, anotherFixtureUrl)
expect(html).toEqual(ajaxData + ajaxData)
})
it("should have shortcut global method readFixtures", function() {
var html = readFixtures(fixtureUrl, anotherFixtureUrl)
expect(html).toEqual(ajaxData + ajaxData)
})
it("should use the configured fixtures path concatenating it to the requested url (without concatenating a slash if it already has an ending one)", function() {
jasmine.getFixtures().fixturesPath = 'a path ending with slash/'
expect(jasmine.getFixtures().makeFixtureUrl_(fixtureUrl)).toEqual('a path ending with slash/'+fixtureUrl)
})
it("should use the configured fixtures path concatenating it to the requested url (concatenating a slash if it doesn't have an ending one)", function() {
jasmine.getFixtures().fixturesPath = 'a path without an ending slash'
expect(jasmine.getFixtures().makeFixtureUrl_(fixtureUrl)).toEqual('a path without an ending slash/'+fixtureUrl)
})
})
describe("load", function() {
it("should insert fixture HTML into container", function() {
jasmine.getFixtures().load(fixtureUrl)
expect(fixturesContainer().html()).toEqual(ajaxData)
})
it("should insert duplicated fixture HTML into container when the same url is provided twice in a single call", function() {
jasmine.getFixtures().load(fixtureUrl, fixtureUrl)
expect(fixturesContainer().html()).toEqual(ajaxData + ajaxData)
})
it("should insert merged HTML of two fixtures into container when two different urls are provided in a single call", function() {
jasmine.getFixtures().load(fixtureUrl, anotherFixtureUrl)
expect(fixturesContainer().html()).toEqual(ajaxData + ajaxData)
})
it("should have shortcut global method loadFixtures", function() {
loadFixtures(fixtureUrl, anotherFixtureUrl)
expect(fixturesContainer().html()).toEqual(ajaxData + ajaxData)
})
describe("when fixture container does not exist", function() {
it("should automatically create fixtures container and append it to DOM", function() {
jasmine.getFixtures().load(fixtureUrl)
expect(fixturesContainer().size()).toEqual(1)
});
})
describe("when fixture container exists", function() {
beforeEach(function() {
appendFixturesContainerToDom()
})
it("should replace it with new content", function() {
jasmine.getFixtures().load(fixtureUrl)
expect(fixturesContainer().html()).toEqual(ajaxData)
})
})
describe("when fixture contains an inline <script> tag", function(){
beforeEach(function(){
ajaxData = "<div><a id=\"anchor_01\"></a><script>$(function(){ $('#anchor_01').addClass('foo')});</script></div>"
})
it("should execute the inline javascript after the fixture has been inserted into the body", function(){
jasmine.getFixtures().load(fixtureUrl)
expect($("#anchor_01")).toHaveClass('foo')
})
})
})
describe("appendLoad", function() {
beforeEach(function(){
ajaxData = 'some ajax data'
})
it("should insert fixture HTML into container", function() {
jasmine.getFixtures().appendLoad(fixtureUrl)
expect(fixturesContainer().html()).toEqual(ajaxData)
})
it("should insert duplicated fixture HTML into container when the same url is provided twice in a single call", function() {
jasmine.getFixtures().appendLoad(fixtureUrl, fixtureUrl)
expect(fixturesContainer().html()).toEqual(ajaxData + ajaxData)
})
it("should insert merged HTML of two fixtures into container when two different urls are provided in a single call", function() {
jasmine.getFixtures().appendLoad(fixtureUrl, anotherFixtureUrl)
expect(fixturesContainer().html()).toEqual(ajaxData + ajaxData)
})
it("should have shortcut global method loadFixtures", function() {
appendLoadFixtures(fixtureUrl, anotherFixtureUrl)
expect(fixturesContainer().html()).toEqual(ajaxData + ajaxData)
})
it("should automatically create fixtures container and append it to DOM", function() {
jasmine.getFixtures().appendLoad(fixtureUrl)
expect(fixturesContainer().size()).toEqual(1)
})
describe("with a prexisting fixture",function(){
beforeEach(function() {
jasmine.getFixtures().appendLoad(fixtureUrl)
})
it("should add new content", function() {
jasmine.getFixtures().appendLoad(fixtureUrl)
expect(fixturesContainer().html()).toEqual(ajaxData + ajaxData)
})
it("should not add a new fixture container", function(){
jasmine.getFixtures().appendLoad(fixtureUrl)
expect(fixturesContainer().size()).toEqual(1)
})
})
describe("when fixture contains an inline <script> tag", function(){
beforeEach(function(){
ajaxData = "<div><a id=\"anchor_01\"></a><script>$(function(){ $('#anchor_01').addClass('foo')});</script></div>"
})
it("should execute the inline javascript after the fixture has been inserted into the body", function(){
jasmine.getFixtures().appendLoad(fixtureUrl)
expect($("#anchor_01")).toHaveClass('foo')
})
})
})
describe("preload", function() {
describe("read after preload", function() {
it("should go from cache", function() {
jasmine.getFixtures().preload(fixtureUrl, anotherFixtureUrl)
jasmine.getFixtures().read(fixtureUrl, anotherFixtureUrl)
expect(jasmine.Fixtures.prototype.loadFixtureIntoCache_.callCount).toEqual(2)
})
it("should return correct HTMLs", function() {
jasmine.getFixtures().preload(fixtureUrl, anotherFixtureUrl)
var html = jasmine.getFixtures().read(fixtureUrl, anotherFixtureUrl)
expect(html).toEqual(ajaxData + ajaxData)
})
})
it("should not preload the same fixture twice", function() {
jasmine.getFixtures().preload(fixtureUrl, fixtureUrl)
expect(jasmine.Fixtures.prototype.loadFixtureIntoCache_.callCount).toEqual(1)
})
it("should have shortcut global method preloadFixtures", function() {
preloadFixtures(fixtureUrl, anotherFixtureUrl)
jasmine.getFixtures().read(fixtureUrl, anotherFixtureUrl)
expect(jasmine.Fixtures.prototype.loadFixtureIntoCache_.callCount).toEqual(2)
})
})
describe("set", function() {
var html = '<div>some HTML</div>'
it("should insert HTML into container", function() {
jasmine.getFixtures().set(html)
expect(fixturesContainer().html()).toEqual(jasmine.JQuery.browserTagCaseIndependentHtml(html))
})
it("should insert jQuery element into container", function() {
jasmine.getFixtures().set($(html))
expect(fixturesContainer().html()).toEqual(jasmine.JQuery.browserTagCaseIndependentHtml(html))
})
it("should have shortcut global method setFixtures", function() {
setFixtures(html)
expect(fixturesContainer().html()).toEqual(jasmine.JQuery.browserTagCaseIndependentHtml(html))
})
describe("when fixture container does not exist", function() {
it("should automatically create fixtures container and append it to DOM", function() {
jasmine.getFixtures().set(html)
expect(fixturesContainer().size()).toEqual(1)
})
})
describe("when fixture container exists", function() {
beforeEach(function() {
appendFixturesContainerToDom()
})
it("should replace it with new content", function() {
jasmine.getFixtures().set(html)
expect(fixturesContainer().html()).toEqual(jasmine.JQuery.browserTagCaseIndependentHtml(html))
})
})
})
describe("appendSet",function(){
var html = '<div>some HTML</div>'
it("should insert HTML into container", function() {
jasmine.getFixtures().appendSet(html)
expect(fixturesContainer().html()).toEqual(jasmine.JQuery.browserTagCaseIndependentHtml(html))
})
it("should insert jQuery element into container", function() {
jasmine.getFixtures().appendSet($(html))
expect(fixturesContainer().html()).toEqual(jasmine.JQuery.browserTagCaseIndependentHtml(html))
})
it("should have shortcut global method setFixtures", function() {
appendSetFixtures(html)
expect(fixturesContainer().html()).toEqual(jasmine.JQuery.browserTagCaseIndependentHtml(html))
})
describe("when fixture container does not exist", function() {
it("should automatically create fixtures container and append it to DOM", function() {
jasmine.getFixtures().appendSet(html)
expect(fixturesContainer().size()).toEqual(1)
})
})
describe("when fixture container exists", function() {
beforeEach(function() {
jasmine.getFixtures().appendSet(html)
})
it("should add new content", function() {
jasmine.getFixtures().appendSet(html)
expect(fixturesContainer().html()).toEqual(jasmine.JQuery.browserTagCaseIndependentHtml(html)+jasmine.JQuery.browserTagCaseIndependentHtml(html))
})
})
})
describe("sandbox", function() {
describe("with no attributes parameter specified", function() {
it("should create DIV with id #sandbox", function() {
expect(jasmine.getFixtures().sandbox().html()).toEqual($('<div id="sandbox" />').html())
})
})
describe("with attributes parameter specified", function() {
it("should create DIV with attributes", function() {
var attributes = {
attr1: 'attr1 value',
attr2: 'attr2 value'
}
var element = $(jasmine.getFixtures().sandbox(attributes))
expect(element.attr('attr1')).toEqual(attributes.attr1)
expect(element.attr('attr2')).toEqual(attributes.attr2)
})
it("should be able to override id by setting it as attribute", function() {
var idOverride = 'overridden'
var element = $(jasmine.getFixtures().sandbox({id: idOverride}))
expect(element.attr('id')).toEqual(idOverride)
})
})
it("should have shortcut global method sandbox", function() {
var attributes = {
id: 'overridden'
}
var element = $(sandbox(attributes))
expect(element.attr('id')).toEqual(attributes.id)
})
})
describe("cleanUp", function() {
it("should remove fixtures container from DOM", function() {
appendFixturesContainerToDom()
jasmine.getFixtures().cleanUp()
expect(fixturesContainer().size()).toEqual(0)
})
})
// WARNING: this block requires its two tests to be invoked in order!
// (Really ugly solution, but unavoidable in this specific case)
describe("automatic DOM clean-up between tests", function() {
// WARNING: this test must be invoked first (before 'SECOND TEST')!
it("FIRST TEST: should pollute the DOM", function() {
appendFixturesContainerToDom()
})
// WARNING: this test must be invoked second (after 'FIRST TEST')!
it("SECOND TEST: should see the DOM in a blank state", function() {
expect(fixturesContainer().size()).toEqual(0)
})
})
})
describe("jasmine.Fixtures using real AJAX call", function() {
var defaultFixturesPath
beforeEach(function() {
defaultFixturesPath = jasmine.getFixtures().fixturesPath
jasmine.getFixtures().fixturesPath = 'spec/fixtures'
})
afterEach(function() {
jasmine.getFixtures().fixturesPath = defaultFixturesPath
})
describe("when fixture file exists", function() {
var fixtureUrl = "real_non_mocked_fixture.html"
it("should load content of fixture file", function() {
var fixtureContent = jasmine.getFixtures().read(fixtureUrl)
expect(fixtureContent).toEqual('<div id="real_non_mocked_fixture"></div>')
})
})
/* TODO : start throwing again
describe("when fixture file does not exist", function() {
var fixtureUrl = "not_existing_fixture"
it("should throw an exception", function() {
expect(function() {
jasmine.getFixtures().read(fixtureUrl)
}).toThrow()
})
})
*/
})
describe("jQuery matchers", function() {
describe("when jQuery matcher hides original Jasmine matcher", function() {
describe("and tested item is jQuery object", function() {
it("should invoke jQuery version of matcher", function() {
expect($('<div />')).toBe('div')
})
})
describe("and tested item is not jQuery object", function() {
it("should invoke original version of matcher", function() {
expect(true).toBe(true)
})
})
describe("and tested item is a dom object", function() {
it("should invoke jquery version of matcher", function() {
expect($('<div />').get(0)).toBe('div')
})
})
})
describe("when jQuery matcher does not hide any original Jasmine matcher", function() {
describe("and tested item in not jQuery object", function() {
it("should pass negated", function() {
expect({}).not.toHaveClass("some-class")
})
})
})
describe("when invoked multiple times on the same fixture", function() {
it("should not reset fixture after first call", function() {
setFixtures(sandbox())
expect($('#sandbox')).toExist()
expect($('#sandbox')).toExist()
})
})
describe("toHaveClass", function() {
var className = "some-class"
it("should pass when class found", function() {
setFixtures(sandbox({'class': className}))
expect($('#sandbox')).toHaveClass(className)
expect($('#sandbox').get(0)).toHaveClass(className)
})
it("should pass negated when class not found", function() {
setFixtures(sandbox())
expect($('#sandbox')).not.toHaveClass(className)
expect($('#sandbox').get(0)).not.toHaveClass(className)
})
it("should not crash when documentElement provided", function(){
var doc = $(document.documentElement).addClass(className)
expect(doc).toHaveClass(className)
doc.removeClass(className)
expect(doc).not.toHaveClass(className)
})
})
describe("toHaveAttr", function() {
var attributeName = 'attr1'
var attributeValue = 'attr1 value'
var wrongAttributeName = 'wrongName'
var wrongAttributeValue = 'wrong value'
beforeEach(function() {
var attributes = {}
attributes[attributeName] = attributeValue
setFixtures(sandbox(attributes))
})
describe("when only attribute name is provided", function() {
it("should pass if element has matching attribute", function() {
expect($('#sandbox')).toHaveAttr(attributeName)
expect($('#sandbox').get(0)).toHaveAttr(attributeName)
})
it("should pass negated if element has no matching attribute", function() {
expect($('#sandbox')).not.toHaveAttr(wrongAttributeName)
expect($('#sandbox').get(0)).not.toHaveAttr(wrongAttributeName)
})
})
describe("when both attribute name and value are provided", function() {
it("should pass if element has matching attribute with matching value", function() {
expect($('#sandbox')).toHaveAttr(attributeName, attributeValue)
expect($('#sandbox').get(0)).toHaveAttr(attributeName, attributeValue)
})
it("should pass negated if element has matching attribute but with wrong value", function() {
expect($('#sandbox')).not.toHaveAttr(attributeName, wrongAttributeValue)
expect($('#sandbox').get(0)).not.toHaveAttr(attributeName, wrongAttributeValue)
})
it("should pass negated if element has no matching attribute", function() {
expect($('#sandbox')).not.toHaveAttr(wrongAttributeName, attributeValue)
expect($('#sandbox').get(0)).not.toHaveAttr(wrongAttributeName, attributeValue)
})
})
})
describe("toHaveProp", function() {
var propertyName = 'prop1'
var propertyValue = 'prop1 value'
var wrongPropertyName = 'wrongName'
var wrongPropertyValue = 'wrong value'
beforeEach(function() {
setFixtures(sandbox())
var element = $('#sandbox')[0]
element[propertyName] = propertyValue
})
describe("when only property name is provided", function() {
it("should pass if element has matching property", function() {
expect($('#sandbox')).toHaveProp(propertyName)
})
it("should pass negated if element has no matching property", function() {
expect($('#sandbox')).not.toHaveProp(wrongPropertyName)
})
})
describe("when both property name and value are provided", function() {
it("should pass if element has matching property with matching value", function() {
expect($('#sandbox')).toHaveProp(propertyName, propertyValue)
})
it("should pass negated if element has matching property but with wrong value", function() {
expect($('#sandbox')).not.toHaveProp(propertyName, wrongPropertyValue)
})
it("should pass negated if element has no matching property", function() {
expect($('#sandbox')).not.toHaveProp(wrongPropertyName, propertyValue)
})
})
})
describe("toHaveCss", function(){
beforeEach(function(){
setFixtures(sandbox())
})
it("should pass if the element has matching css", function(){
$("#sandbox").css("display", "none")
$("#sandbox").css("margin-left", "10px")
expect($("#sandbox")).toHaveCss({display: "none", "margin-left": "10px"})
})
it("should be able to check a subset of element's css", function(){
$("#sandbox").css("display", "none")
$("#sandbox").css("margin-left", "10px")
expect($("#sandbox")).toHaveCss({"margin-left": "10px"})
})
it("should fail if the element doesn't have matching css", function(){
$("#sandbox").css("display", "none")
$("#sandbox").css("margin-left", "20px")
expect($("#sandbox")).not.toHaveCss({display: "none", "margin-left": "10px"})
})
})
describe("toHaveId", function() {
beforeEach(function() {
setFixtures(sandbox())
})
it("should pass if id attribute matches expectation", function() {
expect($('#sandbox')).toHaveId('sandbox')
expect($('#sandbox').get(0)).toHaveId('sandbox')
})
it("should pass negated if id attribute does not match expectation", function() {
expect($('#sandbox')).not.toHaveId('wrongId')
expect($('#sandbox').get(0)).not.toHaveId('wrongId')
})
it("should pass negated if id attribute is not present", function() {
expect($('<div />')).not.toHaveId('sandbox')
expect($('<div />').get(0)).not.toHaveId('sandbox')
})
})
describe("toHaveHtml", function() {
var html = '<div>some text</div>'
var wrongHtml = '<span>some text</span>'
var element
beforeEach(function() {
element = $('<div/>').append(html)
})
it("should pass when html matches", function() {
expect(element).toHaveHtml(html)
expect(element.get(0)).toHaveHtml(html)
})
it("should pass negated when html does not match", function() {
expect(element).not.toHaveHtml(wrongHtml)
expect(element.get(0)).not.toHaveHtml(wrongHtml)
})
})
describe("toContainHtml", function(){
beforeEach(function(){
setFixtures(sandbox())
})
it("should pass when the element contains given html", function(){
$("#sandbox").html("<div><ul></ul><h1>foo</h1></div>")
expect($("#sandbox")).toContainHtml("<ul></ul>")
})
it("should fail when the element doesn't contain given html", function(){
$("#sandbox").html("<div><h1>foo</h1></div>")
expect($("#sandbox")).not.toContainHtml("<ul></ul>")
})
})
describe("toHaveText", function() {
var text = 'some text'
var wrongText = 'some other text'
var element
beforeEach(function() {
element = $('<div/>').append(text)
})
it("should pass when text matches", function() {
expect(element).toHaveText(text)
expect(element.get(0)).toHaveText(text)
})
it("should ignore surrounding whitespace", function() {
element = $('<div>\n' + text + '\n</div>')
expect(element).toHaveText(text)
expect(element.get(0)).toHaveText(text)
})
it("should pass negated when text does not match", function() {
expect(element).not.toHaveText(wrongText)
expect(element.get(0)).not.toHaveText(wrongText)
})
it('should pass when text matches a regex', function() {
expect(element).toHaveText(/some/)
expect(element.get(0)).toHaveText(/some/)
})
it('should pass negated when text does not match a regex', function() {
expect(element).not.toHaveText(/other/)
expect(element.get(0)).not.toHaveText(/other/)
})
})
describe("toHaveValue", function() {
var value = 'some value'
var differentValue = 'different value'
beforeEach(function() {
setFixtures($('<input id="sandbox" type="text" />').val(value))
})
it("should pass if value matches expectation", function() {
expect($('#sandbox')).toHaveValue(value)
expect($('#sandbox').get(0)).toHaveValue(value)
})
it("should pass negated if value does not match expectation", function() {
expect($('#sandbox')).not.toHaveValue(differentValue)
expect($('#sandbox').get(0)).not.toHaveValue(differentValue)
})
it("should pass negated if value attribute is not present", function() {
expect(sandbox()).not.toHaveValue(value)
expect(sandbox().get(0)).not.toHaveValue(value)
})
it("should not coerce types", function(){
setFixtures($('<input id="sandbox" type="text" />').val(""))
expect($('#sandbox')).not.toHaveValue(0)
})
})
describe("toHaveData", function() {
var key = 'some key'
var value = 'some value'
var wrongKey = 'wrong key'
var wrongValue = 'wrong value'
beforeEach(function() {
setFixtures(sandbox().data(key, value))
})
var failing_spec = it("a mock spec just to test expectation failure messages", function() { });
describe("when only key is provided", function() {
it("should pass if element has matching data key", function() {
expect($('#sandbox')).toHaveData(key)
expect($('#sandbox').get(0)).toHaveData(key)
})
it("should pass negated if element has no matching data key", function() {
expect($('#sandbox')).not.toHaveData(wrongKey)
expect($('#sandbox').get(0)).not.toHaveData(wrongKey)
})
it("should give a message that helps understanding how the expectation was not met", function() {
var node = $("#sandbox");
var matcher = failing_spec.expect(node);
matcher.toHaveData(wrongKey);
var result = failing_spec.results().getItems()[0];
expect(result.message).toEqual("Expected '"+node.get(0).outerHTML+"' to have data 'wrong key'.");
})
})
describe("when both key and value are provided", function() {
it("should pass if element has matching key with matching value", function() {
expect($('#sandbox')).toHaveData(key, value)
expect($('#sandbox').get(0)).toHaveData(key, value)
})
it("should pass negated if element has matching key but with wrong value", function() {
expect($('#sandbox')).not.toHaveData(key, wrongValue)
expect($('#sandbox').get(0)).not.toHaveData(key, wrongValue)
})
it("should pass negated if element has no matching key", function() {
expect($('#sandbox')).not.toHaveData(wrongKey, value)
expect($('#sandbox').get(0)).not.toHaveData(wrongKey, value)
})
it("should give a message that helps understanding how the expectation was not met", function() {
var node = $("#sandbox");
var matcher = failing_spec.expect(node.get(0));
matcher.toHaveData(key, wrongValue);
var result = failing_spec.results().getItems()[0];
expect(result.message).toEqual("Expected '"+node.get(0).outerHTML+"' to have data 'wrong key'.");
})
})
})
describe("toBeVisible", function() {
it("should pass on visible element", function() {
setFixtures(sandbox())
expect($('#sandbox')).toBeVisible()
expect($('#sandbox').get(0)).toBeVisible()
})
it("should pass negated on hidden element", function() {
setFixtures(sandbox().hide())
expect($('#sandbox')).not.toBeVisible()
expect($('#sandbox').get(0)).not.toBeVisible()
})
})
describe("toBeHidden", function() {
it("should pass on hidden element", function() {
setFixtures(sandbox().hide())
expect($('#sandbox')).toBeHidden()
expect($('#sandbox').get(0)).toBeHidden()
})
it("should pass negated on visible element", function() {
setFixtures(sandbox())
expect($('#sandbox')).not.toBeHidden()
expect($('#sandbox').get(0)).not.toBeHidden()
})
})
describe("toBeSelected", function() {
beforeEach(function() {
setFixtures('\
<select>\n\
<option id="not-selected"></option>\n\
<option id="selected" selected="selected"></option>\n\
</select>')
})
it("should pass on selected element", function() {
expect($('#selected')).toBeSelected()
expect($('#selected').get(0)).toBeSelected()
})
it("should pass negated on not selected element", function() {
expect($('#not-selected')).not.toBeSelected()
expect($('#not-selected').get(0)).not.toBeSelected()
})
})
describe("toBeChecked", function() {
beforeEach(function() {
setFixtures('\
<input type="checkbox" id="checked" checked="checked" />\n\
<input type="checkbox" id="not-checked" />')
})
it("should pass on checked element", function() {
expect($('#checked')).toBeChecked()
expect($('#checked').get(0)).toBeChecked()
})
it("should pass negated on not checked element", function() {
expect($('#not-checked')).not.toBeChecked()
expect($('#not-checked').get(0)).not.toBeChecked()
})
})
describe("toBeEmpty", function() {
it("should pass on empty element", function() {
setFixtures(sandbox())
expect($('#sandbox')).toBeEmpty()
expect($('#sandbox').get(0)).toBeEmpty()
})
it("should pass negated on element with a tag inside", function() {
setFixtures(sandbox().html($('<span />')))
expect($('#sandbox')).not.toBeEmpty()
expect($('#sandbox').get(0)).not.toBeEmpty()
})
it("should pass negated on element with text inside", function() {
setFixtures(sandbox().text('some text'))
expect($('#sandbox')).not.toBeEmpty()
expect($('#sandbox').get(0)).not.toBeEmpty()
})
})
describe("toExist", function() {
it("should pass on visible element", function() {
setFixtures(sandbox())
expect($('#sandbox')).toExist()
expect($('#sandbox').get(0)).toExist()
})
it("should pass on hidden element", function() {
setFixtures(sandbox().hide())
expect($('#sandbox')).toExist()
expect($('#sandbox').get(0)).toExist()
})
it("should pass negated if element is not present in DOM", function() {
expect($('#non-existent-element')).not.toExist()
expect($('#non-existent-element').get(0)).not.toExist()
})
it("should pass on negated removed element", function(){
setFixtures(sandbox())
var el = $("#sandbox")
el.remove()
expect(el).not.toExist()
})
})
describe("toHaveLength", function() {
it("should pass on an object with more than zero items", function() {
var $three = $('<div>').add('<span>').add("<pre>")
expect($three.length).toBe(3)
expect($three).toHaveLength(3)
})
it("should pass negated on an object with more than zero items", function() {
var $three = $('<div>').add('<span>').add("<pre>")
expect($three.length).toBe(3)
expect($three).not.toHaveLength(2)
})
it("should pass on an object with zero items", function() {
var $zero = $()
expect($zero.length).toBe(0)
expect($zero).toHaveLength(0)
})
it("should pass negated on an object with zero items", function() {
var $zero = $()
expect($zero.length).not.toBe(1)
expect($zero).not.toHaveLength(1)
})
})
describe("toBe", function() {
beforeEach(function() {
setFixtures(sandbox())
})
it("should pass if object matches selector", function() {
expect($('#sandbox')).toBe('#sandbox')
expect($('#sandbox').get(0)).toBe('#sandbox')
})
it("should pass negated if object does not match selector", function() {
expect($('#sandbox')).not.toBe('#wrong-id')
expect($('#sandbox').get(0)).not.toBe('#wrong-id')
})
})
describe("toContain", function() {
beforeEach(function() {
setFixtures(sandbox().html('<span />'))
})
it("should pass if object contains selector", function() {
expect($('#sandbox')).toContain('span')
expect($('#sandbox').get(0)).toContain('span')
})
it("should pass negated if object does not contain selector", function() {
expect($('#sandbox')).not.toContain('div')
expect($('#sandbox').get(0)).not.toContain('div')
})
})
describe("toBeDisabled", function() {
beforeEach(function() {
setFixtures('\
<input type="text" disabled="disabled" id="disabled"/>\n\
<input type="text" id="enabled"/>')
})
it("should pass on disabled element", function() {
expect($('#disabled')).toBeDisabled()
expect($('#disabled').get(0)).toBeDisabled()
})
it("should pass negated on not selected element", function() {
expect($('#enabled')).not.toBeDisabled()
expect($('#enabled').get(0)).not.toBeDisabled()
})
})
describe("toBeFocused", function() {
beforeEach(function() {
setFixtures('<input type="text" id="focused"/>')
})
it("should pass on focused element", function() {
var el = $("#focused").focus()
expect(el).toBeFocused()
})
it("should pass negated on not focused element", function() {
var el = $("#focused")
expect(el).not.toBeFocused()
})
})
describe('toHaveBeenTriggeredOn', function() {
beforeEach(function() {
setFixtures(sandbox().html('<a id="clickme">Click Me</a> <a id="otherlink">Other Link</a>'))
spyOnEvent($('#clickme'), 'click')
spyOnEvent(document, 'click')
spyOnEvent($('#otherlink'), 'click')
})
it('should pass if the event was triggered on the object', function() {
$('#clickme').click()
expect('click').toHaveBeenTriggeredOn($('#clickme'))
expect('click').toHaveBeenTriggeredOn('#clickme')
})
it('should pass if the event was triggered on document', function() {
$(document).click()
expect('click').toHaveBeenTriggeredOn($(document))
expect('click').toHaveBeenTriggeredOn(document)
})
it('should pass if the event was triggered on a descendant of document', function() {
$('#clickme').click()
expect('click').toHaveBeenTriggeredOn($(document))
expect('click').toHaveBeenTriggeredOn(document)
})
it('should pass negated if the event was never triggered', function() {
expect('click').not.toHaveBeenTriggeredOn($('#clickme'))
expect('click').not.toHaveBeenTriggeredOn('#clickme')
})
it('should pass negated if the event was triggered on another non-descendant object', function() {
$('#otherlink').click()
expect('click').not.toHaveBeenTriggeredOn($('#clickme'))
expect('click').not.toHaveBeenTriggeredOn('#clickme')
})
})
describe('toHaveBeenTriggered', function() {
var spyEvents = {}
beforeEach(function() {
setFixtures(sandbox().html('<a id="clickme">Click Me</a> <a id="otherlink">Other Link</a>'))
spyEvents['#clickme'] = spyOnEvent($('#clickme'), 'click')
spyEvents['#otherlink'] = spyOnEvent($('#otherlink'), 'click')
})
it('should pass if the event was triggered on the object', function() {
$('#clickme').click()
expect(spyEvents['#clickme']).toHaveBeenTriggered()
})
it('should pass negated if the event was never triggered', function() {
expect(spyEvents['#clickme']).not.toHaveBeenTriggered()
})
it('should pass negated if the event was triggered on another non-descendant object', function() {
$('#otherlink').click()
expect(spyEvents['#clickme']).not.toHaveBeenTriggered()
})
it('should pass negated if the spy event was reset', function(){
$('#clickme').click()
expect('click').toHaveBeenTriggeredOn($('#clickme'))
expect('click').toHaveBeenTriggeredOn('#clickme')
expect(spyEvents['#clickme']).toHaveBeenTriggered()
spyEvents['#clickme'].reset()
expect('click').not.toHaveBeenTriggeredOn($('#clickme'))
expect('click').not.toHaveBeenTriggeredOn('#clickme')
expect(spyEvents['#clickme']).not.toHaveBeenTriggered()