|
| 1 | +import { expect, fixture, html } from '@open-wc/testing'; |
| 2 | +import { defineComponents } from '../common/definitions/defineComponents.js'; |
| 3 | +import IgcVisuallyHiddenComponent from './visually-hidden.js'; |
| 4 | + |
| 5 | +describe('VisuallyHidden', () => { |
| 6 | + before(() => { |
| 7 | + defineComponents(IgcVisuallyHiddenComponent); |
| 8 | + }); |
| 9 | + |
| 10 | + it('passes the a11y audit', async () => { |
| 11 | + const el = await fixture<IgcVisuallyHiddenComponent>( |
| 12 | + html`<igc-visually-hidden>Hidden text</igc-visually-hidden>` |
| 13 | + ); |
| 14 | + |
| 15 | + await expect(el).to.be.accessible(); |
| 16 | + await expect(el).shadowDom.to.be.accessible(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('renders slotted content', async () => { |
| 20 | + const el = await fixture<IgcVisuallyHiddenComponent>( |
| 21 | + html`<igc-visually-hidden>Screen reader text</igc-visually-hidden>` |
| 22 | + ); |
| 23 | + |
| 24 | + expect(el).dom.to.have.text('Screen reader text'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('is hidden from visual layout when not focused', async () => { |
| 28 | + const el = await fixture<IgcVisuallyHiddenComponent>( |
| 29 | + html`<igc-visually-hidden>Hidden</igc-visually-hidden>` |
| 30 | + ); |
| 31 | + |
| 32 | + const styles = getComputedStyle(el); |
| 33 | + expect(styles.position).to.equal('absolute'); |
| 34 | + expect(styles.width).to.equal('1px'); |
| 35 | + expect(styles.height).to.equal('1px'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('becomes visible when focus is within', async () => { |
| 39 | + const el = await fixture<IgcVisuallyHiddenComponent>( |
| 40 | + html`<igc-visually-hidden><a href="#">Skip</a></igc-visually-hidden>` |
| 41 | + ); |
| 42 | + |
| 43 | + const link = el.querySelector('a')!; |
| 44 | + link.focus(); |
| 45 | + |
| 46 | + // When focused, :host(:not(:focus-within)) no longer matches, |
| 47 | + // so the element is no longer clipped to 1px |
| 48 | + const styles = getComputedStyle(el); |
| 49 | + expect(styles.position).to.not.equal('absolute'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('renders a slot element in shadow DOM', async () => { |
| 53 | + const el = await fixture<IgcVisuallyHiddenComponent>( |
| 54 | + html`<igc-visually-hidden>text</igc-visually-hidden>` |
| 55 | + ); |
| 56 | + |
| 57 | + expect(el).shadowDom.to.equal('<slot></slot>'); |
| 58 | + }); |
| 59 | +}); |
0 commit comments