cloudflare/a11y-focus-scope
Publicmirrored fromhttps://github.com/cloudflare/a11y-focus-scope
test.js
59lines · modecode
10 years ago
| 1 | 'use strict'; |
| 2 | |
| 3 | var expect = require('chai').expect; |
| 4 | var focusScope = require('./'); |
| 5 | |
| 6 | function buildElement(tagName, attributes, innerHTML) { |
| 7 | var element = document.createElement(tagName); |
| 8 | for (var attr in attributes) { |
| 9 | element.setAttribute(attr, attributes[attr]); |
| 10 | } |
| 11 | element.innerHTML = innerHTML || ''; |
| 12 | return element; |
| 13 | } |
| 14 | |
| 15 | describe('focusScope', function() { |
| 16 | it('should focus the element immediately', function() { |
| 17 | var element = buildElement('div', { tabindex: '-1' }); |
| 18 | document.body.appendChild(element); |
| 19 | |
| 20 | focusScope.scopeFocus(element); |
| 21 | |
| 22 | expect(document.activeElement).to.equal(element); |
| 23 | |
| 24 | focusScope.unscopeFocus(); |
| 25 | element.remove(); |
| 26 | }); |
| 27 | |
| 28 | it('should refocus when focus leaves the element', function() { |
| 29 | var element = buildElement('div', { tabindex: '-1' }); |
| 30 | var button = buildElement('button'); |
| 31 | document.body.appendChild(element); |
| 32 | document.body.appendChild(button); |
| 33 | |
| 34 | focusScope.scopeFocus(element); |
| 35 | button.focus(); |
| 36 | |
| 37 | expect(document.activeElement).to.equal(element); |
| 38 | |
| 39 | focusScope.unscopeFocus(); |
| 40 | element.remove(); |
| 41 | button.remove(); |
| 42 | }); |
| 43 | |
| 44 | it('should stop refocusing when unscopeFocus is called', function() { |
| 45 | var element = buildElement('div', { tabindex: '-1' }); |
| 46 | var button = buildElement('button'); |
| 47 | document.body.appendChild(element); |
| 48 | document.body.appendChild(button); |
| 49 | |
| 50 | focusScope.scopeFocus(element); |
| 51 | focusScope.unscopeFocus(); |
| 52 | button.focus(); |
| 53 | |
| 54 | expect(document.activeElement).to.equal(button); |
| 55 | |
| 56 | element.remove(); |
| 57 | button.remove(); |
| 58 | }); |
| 59 | }); |