import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils' import type { FormField } from '../../src/api/types' import { installHotkeys } from '../../src/app/hotkeys' import { setBundle } from '../../src/app/i18n' import CodeEditorField from '../../src/components/form/fields/CodeEditorField.vue' import { langFixture } from '../fixtures/typed' import { press } from '../helpers' // Browser smoke for the codeeditor field (Phase 35 UI-SPEC C1): Tab leaves the // editor, Mod+S and Mod+Enter reach form save hotkeys, Esc does not Cancel. // Needs a real engine so contenteditable focus and key routing match production. enableAutoUnmount(afterEach) const codeeditorBundle = { ...langFixture.data, 'backend::lang.codeeditor.load_failed': { other: 'The code editor could not be loaded. You can still edit the text here.', }, 'backend::lang.codeeditor.toolbar': { other: 'Code editor tools' }, 'backend::lang.list.empty_value': { other: '—' }, } function field(overrides: Partial = {}): FormField { return { name: 'content_html', label: 'HTML', type: 'codeeditor', language: 'html', size: 'large', ...overrides, } as FormField } async function editorReady(root: HTMLElement): Promise { return vi.waitFor(() => { const content = root.querySelector('.cm-content') expect(content).not.toBeNull() return content! }) } beforeEach(() => { setBundle(codeeditorBundle, 'en') }) afterEach(() => { document.body.innerHTML = '' }) describe('codeeditor keyboard in a real browser engine', () => { it('Tab leaves the editor; Mod+S and Mod+Enter save; Esc does not cancel', async () => { const uninstall = installHotkeys() const saveClicks: string[] = [] const cancelClicks: string[] = [] const form = document.createElement('form') form.setAttribute('role', 'form') document.body.appendChild(form) const next = document.createElement('input') next.id = 'next-control' next.type = 'text' const save = document.createElement('button') save.type = 'button' save.setAttribute('data-hotkey', 'mod+s mod+enter') save.addEventListener('click', () => saveClicks.push('save')) const cancel = document.createElement('button') cancel.type = 'button' cancel.setAttribute('data-hotkey', 'escape') cancel.addEventListener('click', () => cancelClicks.push('cancel')) const host = document.createElement('div') form.append(host, next, save, cancel) const wrapper = mount(CodeEditorField, { props: { field: field(), modelValue: 'Hello {{ name }}', controlId: 'f-html', }, attachTo: host, }) await flushPromises() const content = await editorReady(wrapper.element as HTMLElement) content.focus() expect(document.activeElement).toBe(content) // Tab must leave the editor (no indentWithTab / no trap). content.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab', bubbles: true, cancelable: true })) // Native Tab focus movement: advance to the next control. next.focus() expect(document.activeElement).toBe(next) content.focus() expect(press(content, 's', { ctrlKey: true }).defaultPrevented).toBe(true) expect(saveClicks).toEqual(['save']) saveClicks.length = 0 expect(press(content, 'Enter', { ctrlKey: true }).defaultPrevented).toBe(true) expect(saveClicks).toEqual(['save']) expect(press(content, 'Escape').defaultPrevented).toBe(false) expect(cancelClicks).toEqual([]) uninstall() wrapper.unmount() }) it('typing emits update:modelValue with the full document', async () => { const wrapper = mount(CodeEditorField, { props: { field: field({ language: 'text' }), modelValue: 'start', controlId: 'f-text', }, attachTo: document.body, }) await flushPromises() const content = await editorReady(wrapper.element as HTMLElement) content.focus() // Insert at end via beforeinput/input so CodeMirror updates the doc. document.execCommand('selectAll') document.execCommand('insertText', false, 'changed') await flushPromises() const emitted = wrapper.emitted('update:modelValue') expect(emitted).toBeTruthy() expect(emitted!.at(-1)).toEqual(['changed']) }) })