test(admin): unit and browser tests for the mail editor fields
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
119
admin/tests/form/CodeEditorField.test.ts
Normal file
119
admin/tests/form/CodeEditorField.test.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
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 { setBundle } from '../../src/app/i18n'
|
||||
import { langFixture } from '../fixtures/typed'
|
||||
import { resetState } from '../helpers'
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
resetState()
|
||||
setBundle(bundle, 'en')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
document.body.innerHTML = ''
|
||||
vi.doUnmock('../../src/components/form/codeeditor/load')
|
||||
vi.resetModules()
|
||||
})
|
||||
|
||||
enableAutoUnmount(afterEach)
|
||||
|
||||
describe('CodeEditorField', () => {
|
||||
it('shows the loading box at the size height while the chunk loads', async () => {
|
||||
let release!: () => void
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
release = resolve
|
||||
})
|
||||
vi.resetModules()
|
||||
vi.doMock('../../src/components/form/codeeditor/load', () => ({
|
||||
importCodeEditor: 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: field(), 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()
|
||||
})
|
||||
|
||||
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: field({ readOnly: true }),
|
||||
modelValue: 'layout {{ name }}',
|
||||
controlId: 'f-html',
|
||||
},
|
||||
attachTo: document.body,
|
||||
})
|
||||
await flushPromises()
|
||||
expect(wrapper.find('pre').text()).toBe('layout {{ name }}')
|
||||
expect(wrapper.find('.cm-editor').exists()).toBe(false)
|
||||
})
|
||||
|
||||
it('falls back to a monospace textarea when the chunk fails', async () => {
|
||||
vi.resetModules()
|
||||
vi.doMock('../../src/components/form/codeeditor/load', () => ({
|
||||
importCodeEditor: async () => {
|
||||
throw new Error('chunk failed')
|
||||
},
|
||||
}))
|
||||
const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default
|
||||
const wrapper = mount(Field, {
|
||||
props: { field: field(), modelValue: '<p>Stored</p>', controlId: 'f-html' },
|
||||
attachTo: document.body,
|
||||
})
|
||||
await vi.waitFor(() => {
|
||||
expect(wrapper.find('[data-codeeditor-load-failed]').exists()).toBe(true)
|
||||
})
|
||||
const textarea = wrapper.find<HTMLTextAreaElement>('textarea')
|
||||
expect(textarea.element.value).toBe('<p>Stored</p>')
|
||||
await textarea.setValue('<p>Edited</p>')
|
||||
expect(wrapper.emitted('update:modelValue')).toEqual([['<p>Edited</p>']])
|
||||
})
|
||||
|
||||
it('replaces an external modelValue without emitting', async () => {
|
||||
const Field = (await import('../../src/components/form/fields/CodeEditorField.vue')).default
|
||||
const wrapper = mount(Field, {
|
||||
props: {
|
||||
field: field({ language: 'text', size: 'small' }),
|
||||
modelValue: 'one',
|
||||
controlId: 'f-text',
|
||||
},
|
||||
attachTo: document.body,
|
||||
})
|
||||
await flushPromises()
|
||||
await wrapper.setProps({ modelValue: 'two' })
|
||||
await flushPromises()
|
||||
expect(wrapper.emitted('update:modelValue') ?? []).toEqual([])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user