fix(admin): isolate CodeMirror load for chunk-failure tests
- Route CodeEditorField through codeeditor/load so vitest can mock the chunk - Keep registry expectations for colorpicker/codeeditor without importing the editor SFC Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
5
admin/src/components/form/codeeditor/load.ts
Normal file
5
admin/src/components/form/codeeditor/load.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
// Separate module so tests can mock the CodeMirror chunk import without
|
||||
// fighting a prior evaluation of editor.ts in the same vitest worker.
|
||||
export function importCodeEditor(): Promise<typeof import('./editor')> {
|
||||
return import('./editor')
|
||||
}
|
||||
@@ -84,7 +84,7 @@ onMounted(async () => {
|
||||
return
|
||||
}
|
||||
try {
|
||||
module = await import('../codeeditor/editor')
|
||||
module = await (await import('../codeeditor/load')).importCodeEditor()
|
||||
if (disposed) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -235,16 +235,18 @@ describe('CodeEditorField loading and fallback', () => {
|
||||
})
|
||||
|
||||
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.
|
||||
// Hold the chunk 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')
|
||||
})
|
||||
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: codeField(), modelValue: '<p>x</p>', controlId: 'f-html' },
|
||||
@@ -258,7 +260,7 @@ describe('CodeEditorField loading and fallback', () => {
|
||||
release()
|
||||
await flushPromises()
|
||||
wrapper.unmount()
|
||||
vi.doUnmock('../../src/components/form/codeeditor/editor')
|
||||
vi.doUnmock('../../src/components/form/codeeditor/load')
|
||||
})
|
||||
|
||||
it('renders a read-only pre without loading CodeMirror', async () => {
|
||||
@@ -442,12 +444,12 @@ describe('CodeEditorField chunk failure', () => {
|
||||
|
||||
it('shows the load_failed note and a monospace textarea that still emits', async () => {
|
||||
vi.resetModules()
|
||||
vi.doUnmock('../../src/components/form/codeeditor/editor')
|
||||
// Reject the dynamic import itself so a prior suite that already
|
||||
// evaluated the real editor chunk cannot keep createCodeEditor working.
|
||||
vi.doMock('../../src/components/form/codeeditor/editor', () => {
|
||||
throw new Error('chunk failed')
|
||||
})
|
||||
vi.doUnmock('../../src/components/form/codeeditor/load')
|
||||
vi.doMock('../../src/components/form/codeeditor/load', () => ({
|
||||
importCodeEditor: async () => {
|
||||
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, {
|
||||
|
||||
@@ -14,7 +14,6 @@ import MLTextField from '../../src/components/form/fields/MLTextField.vue'
|
||||
import PasswordField from '../../src/components/form/fields/PasswordField.vue'
|
||||
import PermissionEditorField from '../../src/components/form/fields/PermissionEditorField.vue'
|
||||
import ColorPickerField from '../../src/components/form/fields/ColorPickerField.vue'
|
||||
import CodeEditorField from '../../src/components/form/fields/CodeEditorField.vue'
|
||||
import UnsupportedField from '../../src/components/form/fields/UnsupportedField.vue'
|
||||
import WidgetField from '../../src/components/form/fields/WidgetField.vue'
|
||||
import MLPluginField from '../../src/components/form/fields/MLPluginField.vue'
|
||||
@@ -48,12 +47,21 @@ describe('field renderer registry (D-05)', () => {
|
||||
['checkbox', CheckboxField],
|
||||
['relation', RelationField],
|
||||
['colorpicker', ColorPickerField],
|
||||
['codeeditor', CodeEditorField],
|
||||
[RELATION_MANAGER, RelationManager],
|
||||
])('renders %s with its control', (type, component) => {
|
||||
expect(rendererFor(type)).toBe(component)
|
||||
})
|
||||
|
||||
// codeeditor is registered (Phase 35) but not imported here: a static import
|
||||
// of CodeEditorField would evaluate its CodeMirror chunk and break the
|
||||
// mail-smoke chunk-failure mock that relies on vi.resetModules.
|
||||
it('registers codeeditor as an editable control', () => {
|
||||
expect(isRegistered('codeeditor')).toBe(true)
|
||||
expect(needsRecord('codeeditor')).toBe(false)
|
||||
expect(rendererFor('codeeditor')).not.toBe(UnsupportedField)
|
||||
expect(rendererFor('codeeditor').__name).toBe('CodeEditorField')
|
||||
})
|
||||
|
||||
it.each(['notatype', '', 'TEXT', 'constructor'])('falls back to UnsupportedField for %j', (type) => {
|
||||
expect(rendererFor(type)).toBe(UnsupportedField)
|
||||
expect(isRegistered(type)).toBe(false)
|
||||
|
||||
Reference in New Issue
Block a user