import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { enableAutoUnmount, flushPromises } from '@vue/test-utils' import { setBundle } from '../../src/app/i18n' import { toasts } from '../../src/state/useToasts' import { clone, formSchemaFixture, langFixture, recordFixture } from '../fixtures/typed' import { API, mountApp, resetState, type Reply, type Route } from '../helpers' const BASE = `${API}/summercms/cabana/mailtemplates` const RECORD = `${BASE}/7` const strings = { 'backend::lang.form.action_confirm': { other: 'Run “:action” on this record?' }, 'backend::lang.form.action_done': { other: 'Action completed.' }, 'backend::lang.form.action_stale': { other: 'This action no longer applies to this record.' }, 'backend::lang.list.action_forbidden': { other: 'You do not have permission to run this action.' }, 'backend::lang.extension.action_failed': { other: 'The action could not be completed.' }, 'backend::lang.form.delete_confirm': { other: 'Delete this record?' }, } function mailSchema() { const envelope = clone(formSchemaFixture) envelope.data.fields = [ { name: 'subject', label: 'Subject', type: 'text' }, { name: 'body', label: 'Body', type: 'textarea' }, ] as typeof envelope.data.fields return { body: envelope } } function mailRecord(meta: Record = {}): Reply { const envelope = clone(recordFixture) envelope.data = { id: 7, subject: 'Hello', body: 'Body' } envelope.meta = { ...envelope.meta, actions: [{ name: 'reset', label: 'Reset to file', confirm: 'Discard customizations?', danger: true }], deletable: false, ...meta, } return { body: envelope } } function routes(overrides: Record = {}): Record { return { [`GET ${BASE}/schema/form`]: mailSchema(), [`GET ${RECORD}`]: mailRecord(), ...overrides, } } beforeEach(() => { resetState() setBundle({ ...langFixture.data, ...strings }, 'en') }) afterEach(() => { document.body.innerHTML = '' }) enableAutoUnmount(afterEach) describe('FormView mail footer actions and meta.deletable', () => { it('offers reset with a danger confirm, then toasts and reloads', async () => { let gets = 0 const { wrapper } = await mountApp( '/summercms/cabana/mailtemplates/7', routes({ [`GET ${RECORD}`]: () => { gets++ if (gets === 1) { return mailRecord() } return mailRecord({ actions: [] }) }, [`POST ${RECORD}/actions/reset`]: { body: { data: { message: 'Restored from file.', fill: {} }, meta: {} } }, }), ) expect(wrapper.find('[data-record-action="reset"]').exists()).toBe(true) expect(wrapper.find('[data-action="delete"]').exists()).toBe(false) await wrapper.find('[data-record-action="reset"]').trigger('click') await flushPromises() const dialog = document.body.querySelector('[data-confirm-dialog]') expect(dialog?.textContent).toContain('Discard customizations?') dialog!.querySelector('[data-action="confirm"]')!.click() await flushPromises() expect(toasts.value.some((toast) => toast.text.includes('Restored from file.'))).toBe(true) }) it('honours meta.deletable true and absent (default true for non-mail fixtures)', async () => { const deletable = await mountApp( '/summercms/cabana/mailtemplates/7', routes({ [`GET ${RECORD}`]: mailRecord({ deletable: true, actions: [] }) }), ) expect(deletable.wrapper.find('[data-action="delete"]').exists()).toBe(true) const absent = await mountApp( '/summercms/cabana/mailtemplates/7', routes({ [`GET ${RECORD}`]: (() => { const envelope = clone(recordFixture) envelope.data = { id: 7, subject: 'Hello', body: 'Body' } envelope.meta = { ...(envelope.meta ?? {}), actions: [] } delete (envelope.meta as { deletable?: boolean }).deletable return { body: envelope } })(), }), ) expect(absent.wrapper.find('[data-action="delete"]').exists()).toBe(true) }) it('shows the locale switcher from meta.localeVariants when there are two or more', async () => { const two = await mountApp( '/summercms/cabana/mailtemplates/7', routes({ [`GET ${RECORD}`]: mailRecord({ localeVariants: [ { id: 7, locale: '', label: 'Default', states: [], current: true }, { id: 9, locale: 'de', label: 'de', states: ['Customized'], current: false }, ], }), }), ) const select = two.wrapper.find('header select[data-locale-variant]') expect(select.exists()).toBe(true) expect(select.findAll('option').map((option) => option.text())).toEqual(['Default', 'de — Customized']) const none = await mountApp('/summercms/cabana/mailtemplates/7', routes()) expect(none.wrapper.find('[data-locale-variant]').exists()).toBe(false) }) it('toasts 403 and 409 paths from record actions', async () => { const forbidden = await mountApp( '/summercms/cabana/mailtemplates/7', routes({ [`POST ${RECORD}/actions/reset`]: { status: 403, body: { error: { code: 'forbidden', message: '', details: {} } }, }, }), ) await forbidden.wrapper.find('[data-record-action="reset"]').trigger('click') await flushPromises() document.body.querySelector('[data-action="confirm"]')!.click() await flushPromises() expect(toasts.value.some((toast) => toast.text.includes('permission'))).toBe(true) resetState() setBundle({ ...langFixture.data, ...strings }, 'en') const stale = await mountApp( '/summercms/cabana/mailtemplates/7', routes({ [`POST ${RECORD}/actions/reset`]: { status: 409, body: { error: { code: 'conflict', message: 'gone', details: {} } }, }, }), ) await stale.wrapper.find('[data-record-action="reset"]').trigger('click') await flushPromises() document.body.querySelector('[data-action="confirm"]')!.click() await flushPromises() expect(toasts.value.some((toast) => toast.text.includes('no longer applies'))).toBe(true) }) })