navigation_spec.js 3.49 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 8 9 10
        function keyPressEvent(key) {
            return $.Event('keydown', { which: key });
        }

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 29 30
            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() {
31 32
                    expect(accordion.find('.button-chapter').first()).toHaveAttr('aria-expanded', 'true');
                    expect(accordion.find('.button-chapter').last()).toHaveAttr('aria-expanded', 'false');
33 34 35
                });

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

            describe('open section with mouse click', function() {

                it('ensures new section is opened and previous section is closed', function() {
43
                    accordion.find('.button-chapter').last().trigger('click');
44

45 46
                    expect(accordion.find('.chapter-content-container').first()).not.toHaveClass('is-open');
                    expect(accordion.find('.chapter-content-container').last()).toHaveClass('is-open');
47

48 49
                    expect(accordion.find('.button-chapter').first()).not.toHaveClass('is-open');
                    expect(accordion.find('.button-chapter').last()).toHaveClass('is-open');
50 51 52
                });

                it('ensure proper aria and attrs', function() {
53 54 55 56
                    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');
57 58 59 60 61 62
                });
            });

            describe('open section with spacebar', function() {

                it('ensures new section is opened and previous section is closed', function() {
63
                    accordion.find('.button-chapter').last().focus().trigger(keyPressEvent(this.KEY.SPACE));
64

65 66
                    expect(accordion.find('.chapter-content-container').first()).not.toHaveClass('is-open');
                    expect(accordion.find('.chapter-content-container').last()).toHaveClass('is-open');
67

68 69
                    expect(accordion.find('.button-chapter').first()).not.toHaveClass('is-open');
                    expect(accordion.find('.button-chapter').last()).toHaveClass('is-open');
70 71 72
                });

                it('ensure proper aria and attrs', function() {
73 74 75 76
                    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');
77 78 79 80 81
                });
            });
        });
    });
});