import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils' import type { FormField } from '../../src/api/types' import { setBundle } from '../../src/app/i18n' import MailImageField from '../../src/components/form/fields/MailImageField.vue' import { langFixture } from '../fixtures/typed' import { API, mockApi, resetState } from '../helpers' const bundle = { ...langFixture.data, 'backend::lang.mailimage.empty': { other: 'No image chosen' }, 'backend::lang.mailimage.choose': { other: 'Choose image' }, 'backend::lang.mailimage.change': { other: 'Change image' }, 'backend::lang.mailimage.remove': { other: 'Remove image' }, 'backend::lang.mailimage.missing': { other: 'This image is no longer in the library. Choose another one.', }, 'backend::lang.mail.picker.choose_title': { other: 'Choose an image' }, 'backend::lang.mail.picker.search': { other: 'Search images' }, 'backend::lang.mail.picker.use': { other: 'Use this image' }, 'backend::lang.mail.picker.upload': { other: 'Upload image' }, 'backend::lang.mail.picker.empty_title': { other: 'No images yet' }, 'backend::lang.mail.picker.empty_pick_only': { other: 'Images are added in Settings.' }, 'backend::lang.mail.picker.load_failed': { other: 'The image library could not be loaded.' }, 'backend::lang.form.cancel': { other: 'Cancel' }, } beforeEach(() => { resetState() setBundle(bundle, 'en') }) afterEach(() => { document.body.innerHTML = '' }) enableAutoUnmount(afterEach) describe('MailImageField (UI-SPEC C11)', () => { it('shows the missing line when the stored id is gone', async () => { mockApi({ [`GET ${API}/mail/images/3`]: { status: 404, body: { error: { code: 'not_found', message: 'gone', details: {} } }, }, }) const wrapper = mount(MailImageField, { props: { field: { name: 'logo', label: 'Logo', type: 'mailimage' } as FormField, modelValue: 3, controlId: 'f-logo', }, attachTo: document.body, }) await flushPromises() expect(wrapper.find('[data-mailimage-missing]').exists()).toBe(true) expect(wrapper.find('[data-mailimage-missing]').text()).toContain('no longer in the library') }) it('shows empty copy when no image is chosen', async () => { const wrapper = mount(MailImageField, { props: { field: { name: 'logo', label: 'Logo', type: 'mailimage' } as FormField, modelValue: null, controlId: 'f-logo', }, attachTo: document.body, }) await flushPromises() expect(wrapper.text()).toContain('No image chosen') expect(wrapper.find('[data-mailimage-missing]').exists()).toBe(false) }) })