2 describe('Deck JS', function() {
3 describe('standard html structure', function() {
4 beforeEach(function() {
5 loadFixtures('standard.html');
6 if (Modernizr
.history
) {
7 history
.replaceState({}, "", "#")
10 window
.location
.hash
= '#';
14 describe('init(options.selectors.slides)', function() {
15 it('should create slides', function() {
21 expect($.deck('getSlides').length
).toEqual($('.slide3').length
);
25 describe('init(selector)', function() {
26 it('should create slides', function() {
28 expect($.deck('getSlides').length
).toEqual($('.slide').length
);
32 describe('init([selectors])', function() {
33 it('should create slides', function() {
41 expect($.deck('getSlides').length
).toEqual($('.slide').length
);
45 describe('navigation functions', function() {
46 beforeEach(function() {
50 describe('go(i)', function() {
51 it('should go to the i slide (0 based index)', function() {
53 expect($.deck('getSlide')).toHaveClass('slide4');
56 it('should go to the slide with specified id', function() {
57 $.deck('go', 'custom-id');
58 expect($.deck('getSlide')).toHaveId('custom-id');
61 it('should go nowhere if i is out of bounds', function() {
63 expect($.deck('getSlide')).toHaveClass('slide1');
66 it('should go nowhere if id does not exist', function() {
67 $.deck('go', 'i-dont-exist');
68 expect($.deck('getSlide')).toHaveClass('slide1');
71 describe('aria attribute updates', function() {
72 beforeEach(function() {
73 loadFixtures('nesteds.html');
78 it('should set offscreen slides to hidden true', function() {
80 '.toplevel.deck-before:not(.deck-child-current)',
81 '.toplevel.deck-previous:not(.deck-child-current)',
84 ].join(', ')).each(function() {
85 expect($(this)).toHaveAttr('aria-hidden', 'true');
89 it('should set onscreen slides to hidden false', function() {
91 '.deck-child-current.slide',
92 '.deck-child-current .deck-before',
93 '.deck-child-current .deck-previous',
95 ].join(', ')).each(function() {
96 expect($(this)).toHaveAttr('aria-hidden', 'false');
102 describe('next()', function() {
103 it('should go to the next slide', function() {
105 expect($.deck('getSlide')).toHaveClass('slide2');
108 it('should go nowhere if on the last slide', function() {
111 expect($.deck('getSlide')).toHaveClass('slide5');
115 describe('prev()', function() {
116 it('should go to the previous slide', function() {
119 expect($.deck('getSlide')).toHaveClass('slide2');
122 it('should go nowhere if on the first slide', function() {
124 expect($.deck('getSlide')).toHaveClass('slide1');
129 describe('getters', function() {
130 beforeEach(function() {
134 describe('getSlide()', function() {
135 it('should get the current slide', function() {
136 expect($.deck('getSlide')).toHaveClass('slide1');
138 expect($.deck('getSlide')).toHaveClass('slide3');
142 describe('getSlide(i)', function() {
143 it('should get slide number i (0 based index)', function() {
144 expect($.deck('getSlide', 1)).toHaveClass('slide2');
145 expect($.deck('getSlide', 3)).toHaveClass('slide4');
148 it('should return null if i is NaN', function() {
149 expect($.deck('getSlide', 'barfoo')).toBeNull();
152 it('should return null if i is out of bounds', function() {
153 expect($.deck('getSlide', 6)).toBeNull();
157 describe('getSlides()', function() {
158 it('should return an array of jQuery objects for each slide', function() {
159 var expectation
= [];
160 var slides
= $.deck('getSlides');
161 $('.slide').each(function() {
162 expectation
.push($(this));
164 expect(slides
).toEqual(expectation
);
168 describe('getContainer()', function() {
169 it('should return a jQuery object with the container element(s)', function() {
170 expect($.deck('getContainer')).toBe(defaults
.selectors
.container
);
174 describe('getOptions()', function() {
175 it('should return the current options object', function() {
176 expect($.deck('getOptions')).toEqual(defaults
);
180 describe('getTopLevelSlides()', function() {
181 it('should return only root slides', function() {
182 loadFixtures('nesteds.html');
184 var expectation
= [];
185 var topLevelSlides
= $.deck('getTopLevelSlides');
186 $('.toplevel').each(function() {
187 expectation
.push($(this));
189 expect(topLevelSlides
).toEqual(expectation
);
193 describe('getNestedSlides()', function() {
194 it('should return nested slides for current slide', function() {
195 loadFixtures('nesteds.html');
198 var expectation
= [];
199 var nestedSlides
= $.deck('getNestedSlides');
200 $.deck('getSlide').find('.slide').each(function() {
201 expectation
.push($(this));
203 expect(nestedSlides
).toEqual(expectation
);
208 describe('container states', function() {
209 beforeEach(function() {
213 it('should start at state 0', function() {
214 expect($(defaults
.selectors
.container
)).toHaveClass(defaults
.classes
.onPrefix
+ '0');
217 it('should change states with the slide number', function() {
219 expect($(defaults
.selectors
.container
)).toHaveClass(defaults
.classes
.onPrefix
+ '1');
221 expect($(defaults
.selectors
.container
)).toHaveClass(defaults
.classes
.onPrefix
+ '3');
223 expect($(defaults
.selectors
.container
)).toHaveClass(defaults
.classes
.onPrefix
+ '2');
227 describe('options object', function() {
228 var $d
= $(document
);
230 beforeEach(function() {
231 $.deck('.alt-slide', {
234 before
: 'alt-before',
235 current
: 'alt-current',
242 container
: '.alt-container'
252 describe('classes', function() {
253 it('should use the specified after class', function() {
254 expect($('.alt-slide3, .alt-slide4, .alt-slide5')).toHaveClass('alt-after');
257 it('should use the specified before class', function() {
259 expect($('.alt-slide1, .alt-slide2, .alt-slide3')).toHaveClass('alt-before');
262 it('should use the specified container class', function() {
264 expect($('.alt-container')).toHaveClass('alt-on-2');
267 it('should use the specified current class', function() {
268 expect($.deck('getSlide')).toHaveClass('alt-current');
271 it('should use the specified next class', function() {
272 expect($('.alt-slide2')).toHaveClass('alt-next');
275 it('should use the specified previous class', function() {
277 expect($('.alt-slide1')).toHaveClass('alt-prev');
281 describe('key bindings', function() {
284 beforeEach(function() {
285 e
= jQuery
.Event('keydown.deck');
288 it('should go to the next slide using the specified key', function() {
291 expect($.deck('getSlide')).toHaveClass('alt-slide2');
294 it('should go to the previous slide using the specified key', function() {
298 expect($.deck('getSlide')).toHaveClass('alt-slide1');
301 it('should not trigger events that originate within editable elements', function() {
302 var $outside
= $('<input type="text" />').appendTo('body');
303 e
= jQuery
.Event('keydown');
306 expect($.deck('getSlide')).toHaveClass('alt-slide1');
312 describe('events', function() {
315 beforeEach(function() {
319 describe('deck.change', function() {
322 beforeEach(function() {
325 $d
.one('deck.change', function(event
, from, to
) {
331 it('should fire on go(i)', function() {
333 expect(index
).toEqual(3);
336 it('should fire on next()', function() {
338 expect(index
).toEqual(2);
341 it('should fire on prev()', function() {
343 expect(index
).toEqual(0);
346 it('should pass parameters with from and to indices', function() {
348 expect(index
).toEqual(3);
349 expect(oldIndex
).toEqual(1);
352 it('should not fire if default prevented in beforeChange', function() {
353 $d
.bind('deck.beforeChange', false);
355 expect($.deck('getSlide')).toEqual($.deck('getSlide', 1));
356 $d
.unbind('deck.beforeChange', false);
360 describe('deck.init', function() {
361 it('should fire on deck initialization', function() {
363 expect($.deck('getSlides').length
).toBeGreaterThan(0);
367 describe('deck.beforeInit', function() {
370 beforeEach(function() {
372 $d
.on('deck.beforeInit', function() {
377 it('should fire on deck initialization', function() {
379 expect(beforeHit
).toBeTruthy();
382 it('should have populated the slides array', function() {
384 expect($.deck('getSlides').length
).toEqual($('.slide').length
);
387 $d
.bind('deck.beforeInit', f
);
389 $d
.unbind('deck.beforeInit', f
);
392 it('should prevent the init event if lockInit is called', function() {
394 var f = function(event
) {
401 $d
.bind('deck.beforeInit', f
);
402 $d
.bind('deck.init', g
);
404 $d
.unbind('deck.beforeInit', f
);
405 $d
.unbind('deck.init', g
);
406 expect(initHit
).toBeFalsy();
409 it('should warn if locked without release', function() {
411 var f = function(event
) {
414 var warn
= console
.warn
;
415 window
.console
.warn = function() {
419 $d
.bind('deck.beforeInit', f
);
423 $d
.unbind('deck.beforeInit', f
);
425 waitsFor(function() {
430 window
.console
.warn
= warn
;
434 it('should fire init event once releaseInit is called', function() {
435 var f = function(event
) {
437 window
.setTimeout(function() {
443 $d
.bind('deck.beforeInit', f
);
445 $d
.unbind('deck.beforeInit', f
);
448 waitsFor(function() {
449 return $.deck('getSlides').length
> 0;
450 }, 'lock to release', 2000);
455 describe('hash/id assignments', function() {
456 beforeEach(function() {
460 it('should assign ids to slides that do not have them', function() {
461 var slides
= $.deck('getSlides');
462 $.each(slides
, function(i
, $e
) {
463 expect($e
.attr('id')).toBeTruthy();
467 it('should reassign ids on reinitialization', function() {
468 var $firstSlide
= $.deck('getSlide', 0);
469 var firstID
= $firstSlide
.attr('id');
471 $firstSlide
.before('<div class="slide"></div>');
473 expect($firstSlide
).not
.toHaveId(firstID
);
476 it('should update container with a state class including the slide id', function() {
477 var $c
= $.deck('getContainer');
478 var osp
= defaults
.classes
.onPrefix
;
480 expect($c
).toHaveClass(osp
+ $.deck('getSlide', 0).attr('id'));
482 expect($c
).toHaveClass(osp
+ $.deck('getSlide', 1).attr('id'));
484 expect($c
).not
.toHaveClass(osp
+ $.deck('getSlide', 1).attr('id'));
485 expect($c
).toHaveClass(osp
+ $.deck('getSlide', 2).attr('id'));
488 it('should use existing ids if they exist', function() {
489 expect($('#custom-id')).toExist();
492 it('should update the URL on slide change (if supported)', function() {
493 if (Modernizr
.history
) {
495 expect(window
.location
.hash
).toEqual('#slide-3');
499 it('should deep link to slide on deck init', function() {
500 window
.location
.hash
= "#slide-3";
502 waitsFor(function() {
503 return $.deck('getSlide').attr('id') === 'slide-3';
507 it('should follow internal hash links using hashchange (if supported)', function() {
508 window
.location
.hash
= "#slide-3";
509 // Hashchange event doesn't fire right when the hash changes?
510 waitsFor(function() {
511 return $.deck('getSlide').attr('id') === 'slide-3';
512 }, 'hash to change to slide-3', 2000);
517 describe('empty deck', function() {
518 beforeEach(function() {
519 loadFixtures('empty.html');
523 describe('getSlide()', function() {
524 it('should not error on init', $.noop
);