test(35-03): add failing tests for codeeditor field

- Browser smoke: Tab leaves, Mod+S/Mod+Enter save, Esc does not cancel
- Happy-dom: loading box, read-only pre, chunk-failure textarea fallback
- Stub field/editor so imports resolve; assertions fail until GREEN

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-13 13:24:19 +01:00
parent ee0bbc27f8
commit 7e2c01172f
4 changed files with 269 additions and 0 deletions

View File

@@ -10,6 +10,14 @@ import { clearToasts, toasts } from '../../src/state/useToasts'
import { API, mockApi, profile, requestsTo, resetState, type Reply, wait } from '../helpers'
import { langFixture } from '../fixtures/typed'
const codeeditorBundle = {
'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: '—' },
}
const mailPreviewBundle = {
...langFixture.data,
'backend::lang.mail.preview.html': { other: 'HTML' },
@@ -209,3 +217,95 @@ describe('MailPreviewField', () => {
expect(toasts.value[0]?.text).toContain('dev@example.test')
})
})
function codeField(overrides: Partial<FormField> = {}): FormField {
return {
name: 'content_html',
label: 'HTML',
type: 'codeeditor',
language: 'html',
size: 'large',
...overrides,
} as FormField
}
describe('CodeEditorField loading and fallback', () => {
beforeEach(() => {
setBundle({ ...mailPreviewBundle, ...codeeditorBundle }, 'en')
})
it('shows the bg-skel loading box at the size height while the chunk loads', async () => {
// Hold the dynamic import open so the loading box stays visible.
let release!: () => void
const gate = new Promise<void>((resolve) => {
release = resolve
})
vi.resetModules()
vi.doMock('../../src/components/form/codeeditor/editor', async () => {
await gate
return await vi.importActual('../../src/components/form/codeeditor/editor')
})
const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default
const wrapper = mount(Field, {
props: { field: codeField(), modelValue: '<p>x</p>', controlId: 'f-html' },
attachTo: document.body,
})
await flushPromises()
const loading = wrapper.find('[data-codeeditor-loading]')
expect(loading.exists()).toBe(true)
expect(loading.attributes('aria-busy')).toBe('true')
expect((loading.element as HTMLElement).style.height).toBe('320px')
release()
await flushPromises()
wrapper.unmount()
vi.doUnmock('../../src/components/form/codeeditor/editor')
})
it('renders a read-only pre without loading CodeMirror', async () => {
const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default
const wrapper = mount(Field, {
props: {
field: codeField({ readOnly: true }),
modelValue: 'layout {{ name }}',
controlId: 'f-html',
},
attachTo: document.body,
})
await flushPromises()
const pre = wrapper.find('pre')
expect(pre.exists()).toBe(true)
expect(pre.text()).toBe('layout {{ name }}')
expect(wrapper.find('[data-codeeditor-loading]').exists()).toBe(false)
expect(wrapper.find('.cm-editor').exists()).toBe(false)
})
})
// Last in the file: it replaces the editor chunk for the rest of the run.
describe('CodeEditorField chunk failure', () => {
afterEach(() => {
vi.doUnmock('../../src/components/form/codeeditor/editor')
})
it('shows the load_failed note and a monospace textarea that still emits', async () => {
vi.resetModules()
vi.doMock('../../src/components/form/codeeditor/editor', () => {
throw new Error('chunk failed')
})
;(await import('../../src/app/i18n')).setBundle({ ...mailPreviewBundle, ...codeeditorBundle }, 'en')
const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default
const wrapper = mount(Field, {
props: { field: codeField(), modelValue: '<p>Stored</p>', controlId: 'f-html' },
attachTo: document.body,
})
await vi.waitFor(() => {
expect(wrapper.find('[data-codeeditor-load-failed]').exists()).toBe(true)
})
expect(wrapper.find('[data-codeeditor-load-failed]').text()).toContain('could not be loaded')
const textarea = wrapper.find<HTMLTextAreaElement>('textarea')
expect(textarea.exists()).toBe(true)
expect(textarea.element.value).toBe('<p>Stored</p>')
expect(textarea.attributes('id')).toBe('f-html')
await textarea.setValue('<p>Edited</p>')
expect(wrapper.emitted('update:modelValue')).toEqual([['<p>Edited</p>']])
})
})