|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 Google Inc. |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +import bel from 'bel'; |
| 25 | +import {assert} from 'chai'; |
| 26 | +import {install as installClock} from '../helpers/clock'; |
| 27 | +import * as util from '../../../packages/mdc-snackbar/util'; |
| 28 | +import {strings, numbers} from '../../../packages/mdc-snackbar/constants'; |
| 29 | + |
| 30 | +const {ARIA_LIVE_DELAY_MS} = numbers; |
| 31 | +const {ARIA_LIVE_LABEL_TEXT_ATTR} = strings; |
| 32 | + |
| 33 | +suite('MDCSnackbar - util'); |
| 34 | + |
| 35 | +test('#announce temporarily disables ARIA attributes and then restores them', () => { |
| 36 | + const fixture = bel` |
| 37 | +<div> |
| 38 | + <div class="aria" role="status" aria-live="polite"> |
| 39 | + <div class="label"></div> |
| 40 | + </div> |
| 41 | +</div>`; |
| 42 | + const clock = installClock(); |
| 43 | + |
| 44 | + const ariaEl = fixture.querySelector('.aria'); |
| 45 | + const labelEl = fixture.querySelector('.label'); |
| 46 | + |
| 47 | + const labelText = 'Foo'; |
| 48 | + labelEl.textContent = labelText; |
| 49 | + |
| 50 | + util.announce(ariaEl, labelEl); |
| 51 | + |
| 52 | + // Trim to remove ` ` (see comment in util.js) |
| 53 | + assert.equal(labelEl.textContent.trim(), ''); |
| 54 | + assert.equal(ariaEl.getAttribute('aria-live'), 'off'); |
| 55 | + |
| 56 | + clock.tick(ARIA_LIVE_DELAY_MS); |
| 57 | + |
| 58 | + assert.equal(labelEl.textContent, labelText); |
| 59 | + assert.equal(ariaEl.getAttribute('aria-live'), 'polite'); |
| 60 | +}); |
| 61 | + |
| 62 | +test('#announce prevents flicker by displaying label text on ::before pseudo-element and then removing it', () => { |
| 63 | + const fixture = bel` |
| 64 | +<div> |
| 65 | + <div class="aria" role="status" aria-live="polite"> |
| 66 | + <div class="label"></div> |
| 67 | + </div> |
| 68 | +</div>`; |
| 69 | + const clock = installClock(); |
| 70 | + |
| 71 | + const ariaEl = fixture.querySelector('.aria'); |
| 72 | + const labelEl = fixture.querySelector('.label'); |
| 73 | + |
| 74 | + const labelText = 'Foo'; |
| 75 | + labelEl.textContent = labelText; |
| 76 | + |
| 77 | + util.announce(ariaEl, labelEl); |
| 78 | + |
| 79 | + assert.equal(labelEl.getAttribute(ARIA_LIVE_LABEL_TEXT_ATTR), labelText); |
| 80 | + |
| 81 | + clock.tick(ARIA_LIVE_DELAY_MS); |
| 82 | + |
| 83 | + assert.equal(labelEl.getAttribute(ARIA_LIVE_LABEL_TEXT_ATTR), null); |
| 84 | +}); |
| 85 | + |
| 86 | +test('#announce second argument is optional', () => { |
| 87 | + const fixture = bel` |
| 88 | +<div> |
| 89 | + <div class="aria label" role="status" aria-live="polite"></div> |
| 90 | +</div>`; |
| 91 | + const clock = installClock(); |
| 92 | + |
| 93 | + const ariaEl = fixture.querySelector('.aria'); |
| 94 | + |
| 95 | + const labelText = 'Foo'; |
| 96 | + ariaEl.textContent = labelText; |
| 97 | + |
| 98 | + util.announce(ariaEl); |
| 99 | + |
| 100 | + // Trim to remove ` ` (see comment in util.js) |
| 101 | + assert.equal(ariaEl.textContent.trim(), ''); |
| 102 | + assert.equal(ariaEl.getAttribute(ARIA_LIVE_LABEL_TEXT_ATTR), labelText); |
| 103 | + assert.equal(ariaEl.getAttribute('aria-live'), 'off'); |
| 104 | + |
| 105 | + clock.tick(ARIA_LIVE_DELAY_MS); |
| 106 | + |
| 107 | + assert.equal(ariaEl.textContent, labelText); |
| 108 | + assert.equal(ariaEl.getAttribute(ARIA_LIVE_LABEL_TEXT_ATTR), null); |
| 109 | + assert.equal(ariaEl.getAttribute('aria-live'), 'polite'); |
| 110 | +}); |
| 111 | + |
| 112 | +test('#announce does nothing if textContent is empty', () => { |
| 113 | + const fixture = bel` |
| 114 | +<div> |
| 115 | + <div class="aria" role="status" aria-live="polite"> |
| 116 | + <div class="label"></div> |
| 117 | + </div> |
| 118 | +</div>`; |
| 119 | + |
| 120 | + const ariaEl = fixture.querySelector('.aria'); |
| 121 | + const labelEl = fixture.querySelector('.label'); |
| 122 | + |
| 123 | + util.announce(ariaEl, labelEl); |
| 124 | + |
| 125 | + assert.equal(labelEl.textContent.trim(), ''); |
| 126 | + assert.equal(labelEl.getAttribute(ARIA_LIVE_LABEL_TEXT_ATTR), null); |
| 127 | + assert.equal(ariaEl.getAttribute('aria-live'), 'polite'); |
| 128 | +}); |
0 commit comments