|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 OneLiteFeather |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect, afterEach } from 'vitest' |
| 18 | +import { mount, flushPromises, type VueWrapper } from '@vue/test-utils' |
| 19 | +import { h } from 'vue' |
| 20 | +import AddMemberDialog from './AddMemberDialog.vue' |
| 21 | +import ConfirmActionDialog from './ConfirmActionDialog.vue' |
| 22 | +import NewMachineDialog from './NewMachineDialog.vue' |
| 23 | +import NewTeamDialog from './NewTeamDialog.vue' |
| 24 | +import NewUserDialog from './NewUserDialog.vue' |
| 25 | +import ResetPasswordDialog from './ResetPasswordDialog.vue' |
| 26 | +import SsoProviderDialog from './SsoProviderDialog.vue' |
| 27 | + |
| 28 | +const noop = () => Promise.resolve({ data: {} }) |
| 29 | +const api = { create: noop, setPassword: noop } as never |
| 30 | + |
| 31 | +const dialogs: Array<[string, object, Record<string, unknown>]> = [ |
| 32 | + ['AddMemberDialog', AddMemberDialog, { teamName: 'qa' }], |
| 33 | + ['ConfirmActionDialog', ConfirmActionDialog, { title: 'Remove it?', confirmLabel: 'Remove' }], |
| 34 | + ['NewMachineDialog', NewMachineDialog, { api }], |
| 35 | + ['NewTeamDialog', NewTeamDialog, { api }], |
| 36 | + ['NewUserDialog', NewUserDialog, { api }], |
| 37 | + ['ResetPasswordDialog', ResetPasswordDialog, { api, principal: { name: 'alice' } }], |
| 38 | + ['SsoProviderDialog', SsoProviderDialog, { productionUrl: 'https://repo.example.org' }] |
| 39 | +] |
| 40 | + |
| 41 | +let wrapper: VueWrapper | null = null |
| 42 | +let opener: HTMLButtonElement | null = null |
| 43 | + |
| 44 | +const openDialog = async (component: object, props: Record<string, unknown>) => { |
| 45 | + wrapper = mount(component, { props: { modelValue: false, ...props }, attachTo: document.body }) |
| 46 | + await wrapper.setProps({ modelValue: true }) |
| 47 | + await flushPromises() |
| 48 | +} |
| 49 | + |
| 50 | +const dialog = () => document.querySelector<HTMLElement>('[role="dialog"], [role="alertdialog"]')! |
| 51 | + |
| 52 | +afterEach(() => { |
| 53 | + wrapper?.unmount() |
| 54 | + wrapper = null |
| 55 | + opener?.remove() |
| 56 | + opener = null |
| 57 | + document.body.innerHTML = '' |
| 58 | +}) |
| 59 | + |
| 60 | +describe.each(dialogs)('%s dialog contract', (_name, component, props) => { |
| 61 | + it('is named by its heading', async () => { |
| 62 | + await openDialog(component, props) |
| 63 | + |
| 64 | + const name = document.getElementById(dialog().getAttribute('aria-labelledby') ?? '') |
| 65 | + expect(name?.textContent?.trim(), 'the dialog is named by a non-empty heading').toBeTruthy() |
| 66 | + }) |
| 67 | + |
| 68 | + it('moves focus into the dialog when it opens', async () => { |
| 69 | + await openDialog(component, props) |
| 70 | + |
| 71 | + expect(dialog().contains(document.activeElement), 'focus is inside the dialog').toBe(true) |
| 72 | + }) |
| 73 | + |
| 74 | + it('returns focus to the opener when it closes', async () => { |
| 75 | + opener = document.createElement('button') |
| 76 | + document.body.append(opener) |
| 77 | + opener.focus() |
| 78 | + await openDialog(component, props) |
| 79 | + |
| 80 | + await wrapper!.setProps({ modelValue: false }) |
| 81 | + await flushPromises() |
| 82 | + |
| 83 | + expect(document.activeElement, 'focus returns to the opener').toBe(opener) |
| 84 | + }) |
| 85 | + |
| 86 | + it('asks to close on Escape', async () => { |
| 87 | + await openDialog(component, props) |
| 88 | + |
| 89 | + ;(document.activeElement as HTMLElement).dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })) |
| 90 | + await flushPromises() |
| 91 | + |
| 92 | + expect(wrapper!.emitted('update:modelValue'), 'Escape asks the parent to close').toEqual([[false]]) |
| 93 | + }) |
| 94 | + |
| 95 | + it('has at most one filled button', async () => { |
| 96 | + await openDialog(component, props) |
| 97 | + |
| 98 | + const filled = dialog().querySelectorAll('[data-variant="filled"]') |
| 99 | + expect(filled.length, 'only the confirm action is filled').toBeLessThanOrEqual(1) |
| 100 | + }) |
| 101 | +}) |
| 102 | + |
| 103 | +describe('NewMachineDialog heading id', () => { |
| 104 | + it('differs between two mounted instances', async () => { |
| 105 | + wrapper = mount( |
| 106 | + { render: () => h('div', [h(NewMachineDialog, { modelValue: true, api }), h(NewMachineDialog, { modelValue: true, api })]) }, |
| 107 | + { attachTo: document.body } |
| 108 | + ) |
| 109 | + await flushPromises() |
| 110 | + |
| 111 | + const ids = [...document.querySelectorAll('[role="dialog"]')].map(element => element.getAttribute('aria-labelledby')) |
| 112 | + expect(new Set(ids).size, 'each instance owns its heading id').toBe(2) |
| 113 | + expect(ids, 'no fixed heading id').not.toContain('new-machine-title') |
| 114 | + }) |
| 115 | +}) |
0 commit comments