test(admin): unit and browser tests for the mail editor fields

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-13 14:23:28 +01:00
parent 45017039b2
commit 1ae1a9553f
12 changed files with 1009 additions and 9 deletions

View File

@@ -0,0 +1,161 @@
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'
enableAutoUnmount(afterEach)
const bundle = {
...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> = {}): FormField {
return {
name: 'content_html',
label: 'HTML',
type: 'codeeditor',
language: 'html',
size: 'large',
...overrides,
} as FormField
}
async function editorReady(root: HTMLElement): Promise<HTMLElement> {
return vi.waitFor(
() => {
const content = root.querySelector<HTMLElement>('.cm-content')
expect(content, root.innerHTML.slice(0, 500)).not.toBeNull()
return content!
},
{ timeout: 15000 },
)
}
beforeEach(() => {
setBundle(bundle, 'en')
})
afterEach(() => {
document.body.innerHTML = ''
})
describe('codeeditor keyboard and wrap (browser)', () => {
it('Tab leaves; 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(form)
content.focus()
content.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab', bubbles: true, cancelable: true }))
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([])
// Go action decoration on {{ .Name }}
expect(form.querySelector('.cm-editor')?.textContent).toContain('{{ .Name }}')
uninstall()
wrapper.unmount()
})
it('text wraps; html does not enable line wrapping', async () => {
const textHost = document.createElement('div')
document.body.appendChild(textHost)
const text = mount(CodeEditorField, {
props: { field: field({ language: 'text', size: 'small' }), modelValue: 'a'.repeat(200), controlId: 'f-text' },
attachTo: textHost,
})
await flushPromises()
await editorReady(textHost)
expect(textHost.querySelector('.cm-lineWrapping')).not.toBeNull()
text.unmount()
const htmlHost = document.createElement('div')
document.body.appendChild(htmlHost)
const html = mount(CodeEditorField, {
props: { field: field({ language: 'html' }), modelValue: '<div>' + 'x'.repeat(200) + '</div>', controlId: 'f-html' },
attachTo: htmlHost,
})
await flushPromises()
await editorReady(htmlHost)
expect(htmlHost.querySelector('.cm-lineWrapping')).toBeNull()
html.unmount()
})
it('undo and redo restore history through the contenteditable surface', async () => {
const wrapper = mount(CodeEditorField, {
props: { field: field({ language: 'text' }), modelValue: 'start', controlId: 'f-text' },
attachTo: document.body,
})
await flushPromises()
const content = await editorReady(document.body)
content.focus()
const selection = window.getSelection()
const range = document.createRange()
range.selectNodeContents(content)
selection?.removeAllRanges()
selection?.addRange(range)
document.execCommand('insertText', false, 'changed')
await flushPromises()
await vi.waitFor(() => {
expect(wrapper.emitted('update:modelValue')?.at(-1)).toEqual(['changed'])
})
expect(press(content, 'z', { ctrlKey: true }).defaultPrevented).toBe(true)
await flushPromises()
await vi.waitFor(() => {
expect(wrapper.emitted('update:modelValue')?.at(-1)).toEqual(['start'])
})
expect(press(content, 'z', { ctrlKey: true, shiftKey: true }).defaultPrevented).toBe(true)
await flushPromises()
await vi.waitFor(() => {
expect(wrapper.emitted('update:modelValue')?.at(-1)).toEqual(['changed'])
})
})
})