|
1 |
| -import { E2EPage, newE2EPage } from "@stencil/core/testing"; |
| 1 | +import { newE2EPage } from "@stencil/core/testing"; |
2 | 2 | import { focusable, renders, slots, hidden, t9n } from "../../tests/commonTests";
|
3 | 3 | import { html } from "../../../support/formatting";
|
4 | 4 | import { CSS, SLOTS, DURATIONS } from "./resources";
|
5 |
| -import { isElementFocused, newProgrammaticE2EPage, skipAnimations, waitForAnimationFrame } from "../../tests/utils"; |
| 5 | +import { newProgrammaticE2EPage, skipAnimations } from "../../tests/utils"; |
6 | 6 |
|
7 | 7 | describe("calcite-modal properties", () => {
|
8 | 8 | it("renders", () => renders("calcite-modal", { display: "flex", visible: false }));
|
@@ -303,131 +303,83 @@ describe("opening and closing behavior", () => {
|
303 | 303 |
|
304 | 304 | describe("calcite-modal accessibility checks", () => {
|
305 | 305 | it("traps focus within the modal when open", async () => {
|
306 |
| - const button1Id = "button1"; |
307 |
| - const button2Id = "button2"; |
308 | 306 | const page = await newE2EPage();
|
309 | 307 | await page.setContent(
|
310 |
| - html`<calcite-modal> |
| 308 | + `<calcite-modal> |
311 | 309 | <div slot="content">
|
312 |
| - <button id="${button1Id}">Focus1</button> |
313 |
| - <button id="${button2Id}">Focus2</button> |
| 310 | + <button class="btn-1">Focus1</button> |
| 311 | + <button class="btn-2">Focus1</button> |
314 | 312 | </div>
|
315 | 313 | </calcite-modal>`
|
316 | 314 | );
|
317 |
| - await skipAnimations(page); |
318 |
| - await page.waitForChanges(); |
319 | 315 | const modal = await page.find("calcite-modal");
|
320 |
| - modal.setProperty("open", true); |
| 316 | + let $button1; |
| 317 | + let $button2; |
| 318 | + let $close; |
| 319 | + await page.$eval(".btn-1", (elm) => ($button1 = elm)); |
| 320 | + await page.$eval(".btn-2", (elm) => ($button2 = elm)); |
| 321 | + await page.$eval("calcite-modal", (elm) => { |
| 322 | + $close = elm.shadowRoot.querySelector(".close"); |
| 323 | + }); |
| 324 | + await modal.setProperty("open", true); |
321 | 325 | await page.waitForChanges();
|
322 |
| - |
323 |
| - expect(await isElementFocused(page, `.${CSS.close}`, { shadowed: true })).toBe(true); |
| 326 | + expect(document.activeElement).toEqual($close); |
324 | 327 | await page.keyboard.press("Tab");
|
325 |
| - expect(await isElementFocused(page, `#${button1Id}`)).toBe(true); |
| 328 | + expect(document.activeElement).toEqual($button1); |
326 | 329 | await page.keyboard.press("Tab");
|
327 |
| - expect(await isElementFocused(page, `#${button2Id}`)).toBe(true); |
328 |
| - |
| 330 | + expect(document.activeElement).toEqual($button2); |
329 | 331 | await page.keyboard.press("Tab");
|
330 |
| - expect(await isElementFocused(page, `.${CSS.close}`, { shadowed: true })).toBe(true); |
| 332 | + expect(document.activeElement).toEqual($close); |
331 | 333 | await page.keyboard.down("Shift");
|
332 | 334 | await page.keyboard.press("Tab");
|
333 |
| - expect(await isElementFocused(page, `#${button2Id}`)).toBe(true); |
334 |
| - |
| 335 | + expect(document.activeElement).toEqual($button2); |
335 | 336 | await page.keyboard.press("Tab");
|
336 |
| - expect(await isElementFocused(page, `#${button1Id}`)).toBe(true); |
| 337 | + expect(document.activeElement).toEqual($button1); |
337 | 338 | });
|
338 | 339 |
|
339 | 340 | it("restores focus to previously focused element when closed", async () => {
|
340 |
| - const initiallyFocusedId = "initially-focused"; |
341 |
| - const initiallyFocusedIdSelector = `#${initiallyFocusedId}`; |
342 | 341 | const page = await newE2EPage();
|
343 |
| - await page.setContent( |
344 |
| - html` |
345 |
| - <button id="${initiallyFocusedId}">Focus</button> |
346 |
| - <calcite-modal></calcite-modal> |
347 |
| - ` |
348 |
| - ); |
349 |
| - await skipAnimations(page); |
| 342 | + await page.setContent(`<button>Focus</button><calcite-modal></calcite-modal>`); |
350 | 343 | const modal = await page.find("calcite-modal");
|
351 |
| - await page.$eval(initiallyFocusedIdSelector, (button: HTMLButtonElement) => { |
352 |
| - button.focus(); |
| 344 | + let $button; |
| 345 | + await page.$eval("button", (elm: any) => { |
| 346 | + $button = elm; |
| 347 | + $button.focus(); |
353 | 348 | });
|
354 | 349 | await modal.setProperty("open", true);
|
355 | 350 | await page.waitForChanges();
|
356 | 351 | await modal.setProperty("open", false);
|
357 | 352 | await page.waitForChanges();
|
358 |
| - expect(await isElementFocused(page, initiallyFocusedIdSelector)).toBe(true); |
| 353 | + expect(document.activeElement).toEqual($button); |
359 | 354 | });
|
360 | 355 |
|
361 | 356 | it("traps focus within the modal when open and disabled close button", async () => {
|
362 |
| - const button1Id = "button1"; |
363 |
| - const button2Id = "button2"; |
364 | 357 | const page = await newE2EPage();
|
365 | 358 | await page.setContent(
|
366 |
| - html`<calcite-modal close-button-disabled> |
| 359 | + `<calcite-modal close-button-disabled> |
367 | 360 | <div slot="content">
|
368 |
| - <button id="${button1Id}">Focus1</button> |
369 |
| - <button id="${button2Id}">Focus2</button> |
| 361 | + <button class="btn-1">Focus1</button> |
| 362 | + <button class="btn-2">Focus1</button> |
370 | 363 | </div>
|
371 | 364 | </calcite-modal>`
|
372 | 365 | );
|
373 |
| - await skipAnimations(page); |
374 | 366 | const modal = await page.find("calcite-modal");
|
| 367 | + let $button1; |
| 368 | + let $button2; |
| 369 | + await page.$eval(".btn-1", (elm) => ($button1 = elm)); |
| 370 | + await page.$eval(".btn-2", (elm) => ($button2 = elm)); |
375 | 371 |
|
376 | 372 | await modal.setProperty("open", true);
|
377 | 373 | await page.waitForChanges();
|
378 |
| - expect(await isElementFocused(page, `#${button1Id}`)).toBe(true); |
379 |
| - |
380 | 374 | await page.keyboard.press("Tab");
|
381 |
| - expect(await isElementFocused(page, `#${button2Id}`)).toBe(true); |
382 |
| - |
| 375 | + expect(document.activeElement).toEqual($button1); |
383 | 376 | await page.keyboard.press("Tab");
|
384 |
| - expect(await isElementFocused(page, `#${button1Id}`)).toBe(true); |
| 377 | + expect(document.activeElement).toEqual($button2); |
385 | 378 | await page.keyboard.down("Shift");
|
386 | 379 | await page.keyboard.press("Tab");
|
387 |
| - expect(await isElementFocused(page, `#${button2Id}`)).toBe(true); |
| 380 | + expect(document.activeElement).toEqual($button2); |
388 | 381 | await page.keyboard.press("Tab");
|
389 |
| - expect(await isElementFocused(page, `#${button1Id}`)).toBe(true); |
390 |
| - }); |
391 |
| - |
392 |
| - it("traps focus within the modal when user clicks inside the modal", async () => { |
393 |
| - const page = await newE2EPage(); |
394 |
| - await page.setContent( |
395 |
| - html` |
396 |
| - <calcite-modal aria-labelledby="modal-title" close-button-disabled open> |
397 |
| - <div slot="header" id="modal-title">Modal title</div> |
398 |
| - <div slot="content"> |
399 |
| - <p>Modal content.</p> |
400 |
| - <calcite-button icon-start="plus" id="plus" round></calcite-button> |
401 |
| - </div> |
402 |
| - <calcite-button slot="back" color="neutral" width="full">Back</calcite-button> |
403 |
| - <calcite-button slot="secondary" width="full" appearance="outline">Cancel</calcite-button> |
404 |
| - <calcite-button slot="primary" width="full">Save</calcite-button> |
405 |
| - </calcite-modal> |
406 |
| - ` |
407 |
| - ); |
408 |
| - await skipAnimations(page); |
409 |
| - |
410 |
| - const contentEl = await page.find(`div[slot="content"]`); |
411 |
| - const backButtonEl = await page.find(`calcite-button[slot="back"]`); |
412 |
| - |
413 |
| - await page.keyboard.press("Tab"); |
414 |
| - expect(await isElementFocused(page, `#plus`)).toBe(true); |
415 |
| - |
416 |
| - await page.keyboard.press("Tab"); |
417 |
| - expect(await isElementFocused(page, `[slot="back"]`)).toBe(true); |
418 |
| - |
419 |
| - await page.keyboard.press("Tab"); |
420 |
| - expect(await isElementFocused(page, `[slot="secondary"]`)).toBe(true); |
421 |
| - |
422 |
| - await page.keyboard.press("Tab"); |
423 |
| - expect(await isElementFocused(page, `[slot="primary"]`)).toBe(true); |
424 |
| - |
425 |
| - await contentEl.click(); |
426 |
| - await page.waitForChanges(); |
427 |
| - await backButtonEl.click(); |
428 |
| - await page.waitForChanges(); |
429 |
| - |
430 |
| - expect(await isElementFocused(page, `[slot="back"]`)).toBe(true); |
| 382 | + expect(document.activeElement).toEqual($button1); |
431 | 383 | });
|
432 | 384 |
|
433 | 385 | describe("setFocus", () => {
|
|
0 commit comments