navigation_spec.js 3.48 KB
Newer Older
1 2 3 4
define(['jquery', 'js/utils/navigation'], function($) {
    'use strict';

    describe('Course Navigation Accordion', function() {
5
        var accordion, chapterMenu;
6

7
        function keyPressEvent(key) {
8
            return $.Event('keydown', {which: key});
9 10
        }

11 12 13 14
        beforeEach(function() {
            loadFixtures('js/fixtures/accordion.html');

            accordion = $('.accordion');
15
            chapterMenu = accordion.children('.chapter-content-container').children('.chapter-menu');
16

17
            this.KEY = $.ui.keyCode;
18
            spyOn($.fn, 'focus').and.callThrough();
19 20 21 22 23 24 25 26 27 28
            edx.util.navigation.init();
        });

        describe('constructor', function() {
            describe('always', function() {
                it('ensures accordion is present', function() {
                    expect(accordion.length).toBe(1);
                });

                it('ensures aria attributes are present', function() {
29 30
                    expect(accordion.find('.button-chapter').first()).toHaveAttr('aria-expanded', 'true');
                    expect(accordion.find('.button-chapter').last()).toHaveAttr('aria-expanded', 'false');
31 32 33
                });

                it('ensures only one active item', function() {
34
                    expect($(chapterMenu).find('.active').length).toBe(1);
35 36 37 38 39
                });
            });

            describe('open section with mouse click', function() {
                it('ensures new section is opened and previous section is closed', function() {
40
                    accordion.find('.button-chapter').last().trigger('click');
41

42 43
                    expect(accordion.find('.chapter-content-container').first()).not.toHaveClass('is-open');
                    expect(accordion.find('.chapter-content-container').last()).toHaveClass('is-open');
44

45 46
                    expect(accordion.find('.button-chapter').first()).not.toHaveClass('is-open');
                    expect(accordion.find('.button-chapter').last()).toHaveClass('is-open');
47 48 49
                });

                it('ensure proper aria and attrs', function() {
50 51 52 53
                    accordion.find('.button-chapter').last().trigger('click');

                    expect(accordion.find('.button-chapter').first()).toHaveAttr('aria-expanded', 'false');
                    expect(accordion.find('.button-chapter').last()).toHaveAttr('aria-expanded', 'true');
54 55 56 57 58
                });
            });

            describe('open section with spacebar', function() {
                it('ensures new section is opened and previous section is closed', function() {
59
                    accordion.find('.button-chapter').last().focus().trigger(keyPressEvent(this.KEY.SPACE));
60

61 62
                    expect(accordion.find('.chapter-content-container').first()).not.toHaveClass('is-open');
                    expect(accordion.find('.chapter-content-container').last()).toHaveClass('is-open');
63

64 65
                    expect(accordion.find('.button-chapter').first()).not.toHaveClass('is-open');
                    expect(accordion.find('.button-chapter').last()).toHaveClass('is-open');
66 67 68
                });

                it('ensure proper aria and attrs', function() {
69 70 71 72
                    accordion.find('.button-chapter').last().focus().trigger(keyPressEvent(this.KEY.SPACE));

                    expect(accordion.find('.button-chapter').first()).toHaveAttr('aria-expanded', 'false');
                    expect(accordion.find('.button-chapter').last()).toHaveAttr('aria-expanded', 'true');
73 74 75 76
                });
            });
        });
    });
77
});