jasmine-jquery-spec.js
18.7 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
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($, 'ajax').andCallFake(function(options) {
options.success(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($.ajax.callCount).toEqual(2);
});
});
it("first-time read should go through AJAX", function() {
jasmine.getFixtures().read(fixtureUrl);
expect($.ajax.callCount).toEqual(1);
});
it("subsequent read from the same URL should go from cache", function() {
jasmine.getFixtures().read(fixtureUrl, fixtureUrl);
expect($.ajax.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/'
readFixtures(fixtureUrl);
expect($.ajax.mostRecentCall.args[0].url).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'
readFixtures(fixtureUrl);
expect($.ajax.mostRecentCall.args[0].url).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("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("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("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("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);
});
it("should pass negated when class not found", function() {
setFixtures(sandbox());
expect($('#sandbox')).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);
});
it("should pass negated if element has no matching attribute", function() {
expect($('#sandbox')).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);
});
it("should pass negated if element has matching attribute but with wrong value", function() {
expect($('#sandbox')).not.toHaveAttr(attributeName, wrongAttributeValue);
});
it("should pass negated if element has no matching attribute", function() {
expect($('#sandbox')).not.toHaveAttr(wrongAttributeName, attributeValue);
});
});
});
describe("toHaveId", function() {
beforeEach(function() {
setFixtures(sandbox());
});
it("should pass if id attribute matches expectation", function() {
expect($('#sandbox')).toHaveId('sandbox');
});
it("should pass negated if id attribute does not match expectation", function() {
expect($('#sandbox')).not.toHaveId('wrongId');
});
it("should pass negated if id attribute is not present", function() {
expect($('<div />')).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);
});
it("should pass negated when html does not match", function() {
expect(element).not.toHaveHtml(wrongHtml);
});
});
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);
});
it("should pass negated when text does not match", function() {
expect(element).not.toHaveText(wrongText);
});
it('should pass when text matches a regex', function() {
expect(element).toHaveText(/some/);
});
it('should pass negated when text does not match a regex', function() {
expect(element).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);
});
it("should pass negated if value does not match expectation", function() {
expect($('#sandbox')).not.toHaveValue(differentValue);
});
it("should pass negated if value attribute is not present", function() {
expect(sandbox()).not.toHaveValue(value);
});
});
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));
});
describe("when only key is provided", function() {
it("should pass if element has matching data key", function() {
expect($('#sandbox')).toHaveData(key);
});
it("should pass negated if element has no matching data key", function() {
expect($('#sandbox')).not.toHaveData(wrongKey);
});
});
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);
});
it("should pass negated if element has matching key but with wrong value", function() {
expect($('#sandbox')).not.toHaveData(key, wrongValue);
});
it("should pass negated if element has no matching key", function() {
expect($('#sandbox')).not.toHaveData(wrongKey, value);
});
});
});
describe("toBeVisible", function() {
it("should pass on visible element", function() {
setFixtures(sandbox());
expect($('#sandbox')).toBeVisible();
});
it("should pass negated on hidden element", function() {
setFixtures(sandbox().hide());
expect($('#sandbox')).not.toBeVisible();
});
});
describe("toBeHidden", function() {
it("should pass on hidden element", function() {
setFixtures(sandbox().hide());
expect($('#sandbox')).toBeHidden();
});
it("should pass negated on visible element", function() {
setFixtures(sandbox());
expect($('#sandbox')).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();
});
it("should pass negated on not selected element", function() {
expect($('#not-selected')).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();
});
it("should pass negated on not checked element", function() {
expect($('#not-checked')).not.toBeChecked();
});
});
describe("toBeEmpty", function() {
it("should pass on empty element", function() {
setFixtures(sandbox());
expect($('#sandbox')).toBeEmpty();
});
it("should pass negated on element with a tag inside", function() {
setFixtures(sandbox().html($('<span />')));
expect($('#sandbox')).not.toBeEmpty();
});
it("should pass negated on element with text inside", function() {
setFixtures(sandbox().text('some text'));
expect($('#sandbox')).not.toBeEmpty();
});
});
describe("toExist", function() {
it("should pass on visible element", function() {
setFixtures(sandbox());
expect($('#sandbox')).toExist();
});
it("should pass on hidden element", function() {
setFixtures(sandbox().hide());
expect($('#sandbox')).toExist();
});
it("should pass negated if element is not present in DOM", function() {
expect($('#non-existent-element')).not.toExist();
});
});
describe("toBe", function() {
beforeEach(function() {
setFixtures(sandbox());
});
it("should pass if object matches selector", function() {
expect($('#sandbox')).toBe('#sandbox');
});
it("should pass negated if object does not match selector", function() {
expect($('#sandbox')).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');
});
it("should pass negated if object does not contain selector", function() {
expect($('#sandbox')).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();
});
it("should pass negated on not selected element", function() {
expect($('#enabled')).not.toBeDisabled();
});
});
describe('toHaveBeenTriggeredOn', function() {
beforeEach(function() {
setFixtures(sandbox().html('<a id="clickme">Click Me</a> <a id="otherlink">Other Link</a>'));
spyOnEvent($('#clickme'), 'click');
});
it('should pass if the event was triggered on the object', function() {
$('#clickme').click();
expect('click').toHaveBeenTriggeredOn($('#clickme'));
});
it('should pass negated if the event was never triggered', function() {
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'));
});
});
});