|
| 1 | +import { beforeEach, describe, expect, test } from 'vitest' |
| 2 | +import { experimental_AstroContainer as AstroContainer } from 'astro/container' |
| 3 | +import ImageFixture from '@components/Image/client/__fixtures__/index.fixture.astro' |
| 4 | +import type { ImageElement as ImageElementInstance } from '@components/Image/client' |
| 5 | +import type { WebComponentModule } from '@components/scripts/@types/webComponentModule' |
| 6 | +import { executeRender } from '@test/unit/helpers/litRuntime' |
| 7 | + |
| 8 | +type ImageModule = WebComponentModule<ImageElementInstance> |
| 9 | + |
| 10 | +const getMainImage = (element: ImageElementInstance): HTMLImageElement | null => |
| 11 | + element.querySelector('[data-image-figure] img') |
| 12 | + |
| 13 | +const getOpenButton = (element: ImageElementInstance): HTMLButtonElement | null => |
| 14 | + element.querySelector('[data-image-open]') |
| 15 | + |
| 16 | +const getDialog = (element: ImageElementInstance): HTMLElement | null => |
| 17 | + element.querySelector('[data-image-dialog]') |
| 18 | + |
| 19 | +const getModalImage = (element: ImageElementInstance): HTMLImageElement | null => |
| 20 | + element.querySelector('[data-image-modal-asset]') |
| 21 | + |
| 22 | +describe('Image web component', () => { |
| 23 | + let container: AstroContainer |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + container = await AstroContainer.create() |
| 27 | + }) |
| 28 | + |
| 29 | + const renderImage = async ( |
| 30 | + props: { |
| 31 | + alt?: string |
| 32 | + variant?: 'left' | 'right' | 'center' | 'full' |
| 33 | + }, |
| 34 | + assertion: (_context: { |
| 35 | + element: ImageElementInstance |
| 36 | + window: Window & typeof globalThis |
| 37 | + }) => Promise<void> | void |
| 38 | + ) => { |
| 39 | + await executeRender<ImageModule>({ |
| 40 | + container, |
| 41 | + component: ImageFixture, |
| 42 | + moduleSpecifier: '@components/Image/client/index', |
| 43 | + args: { props }, |
| 44 | + waitForReady: async element => { |
| 45 | + await element.updateComplete |
| 46 | + }, |
| 47 | + assert: async ({ element, window }) => { |
| 48 | + await assertion({ element, window: window as Window & typeof globalThis }) |
| 49 | + }, |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + test('renders centered image markup by default', async () => { |
| 54 | + await renderImage({}, async ({ element }) => { |
| 55 | + expect(element.getAttribute('data-image-variant')).toBe('center') |
| 56 | + |
| 57 | + const mainImage = getMainImage(element) |
| 58 | + expect(mainImage?.getAttribute('alt')).toBe('Backstage background test image') |
| 59 | + |
| 60 | + const openButton = getOpenButton(element) |
| 61 | + expect(openButton?.getAttribute('aria-label')).toContain('Backstage background test image') |
| 62 | + |
| 63 | + const dialog = getDialog(element) |
| 64 | + expect(dialog?.hasAttribute('hidden')).toBe(true) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + test('opens the modal when the magnify button is activated', async () => { |
| 69 | + await renderImage({ variant: 'right' }, async ({ element, window }) => { |
| 70 | + expect(element.getAttribute('data-image-variant')).toBe('right') |
| 71 | + |
| 72 | + const openButton = getOpenButton(element) |
| 73 | + openButton?.dispatchEvent(new window.Event('click', { bubbles: true })) |
| 74 | + await element.updateComplete |
| 75 | + |
| 76 | + const dialog = getDialog(element) |
| 77 | + expect(dialog?.hasAttribute('hidden')).toBe(false) |
| 78 | + expect(dialog?.getAttribute('aria-label')).toContain('Backstage background test image') |
| 79 | + |
| 80 | + const modalImage = getModalImage(element) |
| 81 | + expect(modalImage?.getAttribute('alt')).toBe('Backstage background test image') |
| 82 | + |
| 83 | + const closeIcon = element.querySelector( |
| 84 | + 'button[aria-label="Close image dialog"] svg' |
| 85 | + ) as SVGElement | null |
| 86 | + expect(closeIcon?.getAttribute('aria-hidden')).toBe('true') |
| 87 | + expect(closeIcon?.getAttribute('focusable')).toBe('false') |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + test('closes the modal on Escape', async () => { |
| 92 | + await renderImage({}, async ({ element, window }) => { |
| 93 | + const openButton = getOpenButton(element) |
| 94 | + openButton?.dispatchEvent(new window.Event('click', { bubbles: true })) |
| 95 | + await element.updateComplete |
| 96 | + |
| 97 | + window.document.dispatchEvent(new window.KeyboardEvent('keydown', { key: 'Escape' })) |
| 98 | + await element.updateComplete |
| 99 | + |
| 100 | + const dialog = getDialog(element) |
| 101 | + expect(dialog?.hasAttribute('hidden')).toBe(true) |
| 102 | + }) |
| 103 | + }) |
| 104 | +}) |
0 commit comments