Skip to content

Commit a084f4e

Browse files
marissahuysentruytrise-erpelding
authored andcommitted
test(tray): add tab and close() keyboard tests
1 parent 6662f31 commit a084f4e

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

1st-gen/packages/tray/test/tray.test.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('Tray', () => {
106106
});
107107

108108
describe('Dismiss helpers', () => {
109-
it('renders visually-hidden, keyboard-accessible dismiss helpers when no buttons detected', async () => {
109+
it('renders visually-hidden dismiss helpers when no buttons detected', async () => {
110110
const el = await fixture<Tray>(html`
111111
<sp-tray open>
112112
<sp-menu style="width: 100%">
@@ -128,6 +128,66 @@ describe('Tray', () => {
128128
expect(buttons[0].getAttribute('tabindex')).to.be.null;
129129
});
130130

131+
it('allows focusing dismiss helper buttons', async () => {
132+
const el = await fixture<Tray>(html`
133+
<sp-tray open>
134+
<p>Content without buttons</p>
135+
</sp-tray>
136+
`);
137+
await elementUpdated(el);
138+
139+
const dismissButton = el.shadowRoot.querySelector(
140+
'.visually-hidden button'
141+
) as HTMLButtonElement;
142+
expect(dismissButton).to.exist;
143+
144+
dismissButton.focus();
145+
await elementUpdated(el);
146+
147+
expect(document.activeElement).to.equal(el);
148+
expect(
149+
el.shadowRoot.activeElement,
150+
'dismiss button is focused in shadow root'
151+
).to.equal(dismissButton);
152+
});
153+
154+
it('closes tray when Enter key is pressed on dismiss button', async () => {
155+
const test = await fixture<HTMLElement>(html`
156+
<sp-theme system="spectrum" scale="medium" color="dark">
157+
<sp-tray open>
158+
<p>Content without buttons</p>
159+
</sp-tray>
160+
</sp-theme>
161+
`);
162+
163+
const el = test.querySelector('sp-tray') as Tray;
164+
await nextFrame(); // allows for animation to complete
165+
await nextFrame();
166+
await elementUpdated(el);
167+
expect(el.open).to.be.true;
168+
169+
const dismissButton = el.shadowRoot.querySelector(
170+
'.visually-hidden button'
171+
) as HTMLButtonElement;
172+
173+
dismissButton.focus();
174+
await elementUpdated(el);
175+
176+
const closed = oneEvent(el, 'close');
177+
const enterEvent = new KeyboardEvent('keydown', {
178+
key: 'Enter',
179+
code: 'Enter',
180+
bubbles: true,
181+
cancelable: true,
182+
});
183+
dismissButton.dispatchEvent(enterEvent);
184+
// Trigger the default button behavior
185+
dismissButton.click();
186+
await closed;
187+
188+
expect(el.open).to.be.false;
189+
});
190+
131191
it('does not render dismiss helpers when sp-button is detected', async () => {
132192
const el = await fixture<Tray>(html`
133193
<sp-tray open>

0 commit comments

Comments
 (0)