accessibility_tools_spec.js 4.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
describe('Tests for accessibility_tools.js', function() {
    describe('Tests for accessible modals', function() {
        var pressTabOnLastElt = function(firstElt, lastElt) {
            firstElt.focus();
        };

        var pressShiftTabOnFirstElt = function(firstElt, lastElt) {
            lastElt.focus();
        };

        var pressEsc = function(closeModal) {
            closeModal.click();
        };

        beforeEach(function() {
Eric Fischer committed
16
            var $focusedElementBeforeModal;
17 18 19 20 21 22
            loadFixtures('js/fixtures/dashboard-fixture.html');
            accessible_modal('#trigger', '#close-modal', '#modalId', '#mainPageId');
            $('#trigger').click();
        });

        it('sets focusedElementBeforeModal to trigger', function() {
Eric Fischer committed
23
            expect($focusedElementBeforeModal).toHaveAttr('id', 'trigger');
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        });

        it('sets main page aria-hidden attr to true', function() {
            expect($('#mainPageId')).toHaveAttr('aria-hidden', 'true');
        });

        it('sets modal aria-hidden attr to false', function() {
            expect($('#modalId')).toHaveAttr('aria-hidden', 'false');
        });

        it("sets the close-modal button's tab index to 1", function() {
            expect($('#close-modal')).toHaveAttr('tabindex', '1');
        });

        it("sets the focussable elements' tab indices to 2", function() {
            expect($('#text-input')).toHaveAttr('tabindex', '2');
            expect($('#submit')).toHaveAttr('tabindex', '2');
        });
42 43 44

    // for some reason, toBeFocused tests don't pass with js-test-tool
    // (they do when run locally on browsers), so we're skipping them temporarily
45 46 47
        xit('shifts focus to close-modal button', function() {
            expect($('#close-modal')).toBeFocused();
        });
48 49 50

    // for some reason, toBeFocused tests don't pass with js-test-tool
    // (they do when run locally on browsers), so we're skipping them temporarily
51 52 53 54 55
        xit('tab on last element in modal returns to the close-modal button', function() {
            $('#submit').focus();
            pressTabOnLastElt($('#close-modal'), $('#submit'));
            expect($('#close-modal')).toBeFocused();
        });
56 57 58

    // for some reason, toBeFocused tests don't pass with js-test-tool
    // (they do when run locally on browsers), so we're skipping them temporarily
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
        xit('shift-tab on close-modal element in modal returns to the last element in modal', function() {
            $('#close-modal').focus();
            pressShiftTabOnFirstElt($('#close-modal'), $('#submit'));
            expect($('#submit')).toBeFocused();
        });

        it("pressing ESC calls 'click' on close-modal element", function() {
            var clicked = false;
            $('#close-modal').click(function(theEvent) {
                clicked = true;
            });
            pressEsc($('#close-modal'));
            expect(clicked).toBe(true);
        });

        describe('When modal is closed', function() {
            beforeEach(function() {
                $('#close-modal').click();
            });

            it('sets main page aria-hidden attr to false', function() {
                expect($('#mainPageId')).toHaveAttr('aria-hidden', 'false');
            });

            it('sets modal aria-hidden attr to true', function() {
                expect($('#modalId')).toHaveAttr('aria-hidden', 'true');
            });
86 87 88

      // for some reason, toBeFocused tests don't pass with js-test-tool
      // (they do when run locally on browsers), so we're skipping them temporarily
89 90 91 92
            xit('returns focus to focusedElementBeforeModal', function() {
                expect(focusedElementBeforeModal).toBeFocused();
            });
        });
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

    describe('Tests for SR region', function() {
        var getSRText = function() {
            return $('#reader-feedback').html();
        };

        beforeEach(function() {
            loadFixtures('js/fixtures/sr-fixture.html');
        });

        it('has the sr class and is aria-live', function() {
            var $reader = $('#reader-feedback');
            expect($reader.hasClass('sr')).toBe(true);
            expect($reader.attr('aria-live')).toBe('polite');
        });

        it('supports the setting of simple text', function() {
            window.SR.readText('Simple Text');
            expect(getSRText()).toContain('<p>Simple Text</p>');
        });

        it('supports the setting of an array of text', function() {
            window.SR.readTexts(['One', 'Two']);
            expect(getSRText()).toContain('<p>One</p>\n<p>Two</p>');
        });

        it('supports setting an array of elements', function() {
            window.SR.readElts($('.status'));
            expect(getSRText()).toContain(
                '<p>Yes!<span>Your answer is correct!</span></p>\n<p>No!<span>Your answer is wrong!</span></p>'
            );
        });
    });
127
});