diff --git a/admin/tests/form/FormViewMail.test.ts b/admin/tests/form/FormViewMail.test.ts index 0a6d3f9f..816c7056 100644 --- a/admin/tests/form/FormViewMail.test.ts +++ b/admin/tests/form/FormViewMail.test.ts @@ -125,6 +125,57 @@ describe('FormView mail footer actions and meta.deletable', () => { const none = await mountApp('/summercms/cabana/mailtemplates/7', routes()) expect(none.wrapper.find('[data-locale-variant]').exists()).toBe(false) + + const one = await mountApp( + '/summercms/cabana/mailtemplates/7', + routes({ + [`GET ${RECORD}`]: mailRecord({ + localeVariants: [{ id: 7, locale: '', label: 'Default', states: [], current: true }], + }), + }), + ) + expect(one.wrapper.find('[data-locale-variant]').exists()).toBe(false) + }) + + it('asks the unsaved-changes confirm before switching locale and stays on cancel', async () => { + const localeVariants = [ + { id: 7, locale: '', label: 'Default', states: [], current: true }, + { id: 9, locale: 'de', label: 'de', states: [], current: false }, + ] + const sibling = clone(recordFixture) + sibling.data = { id: 9, subject: 'Hallo', body: 'Body' } + sibling.meta = { + ...sibling.meta, + actions: [], + deletable: false, + localeVariants: localeVariants.map((variant) => ({ ...variant, current: variant.id === 9 })), + } + const { wrapper, router, calls } = await mountApp( + '/summercms/cabana/mailtemplates/7', + routes({ + [`GET ${RECORD}`]: mailRecord({ actions: [], localeVariants }), + [`GET ${BASE}/9`]: { body: sibling }, + }), + ) + await wrapper.find('#field-subject').setValue('Changed') + const select = wrapper.find('header select[data-locale-variant]') + await select.setValue('9') + await flushPromises() + const dialog = document.body.querySelector('[data-confirm-dialog]') + expect(dialog?.textContent).toContain(langFixture.data['backend::lang.form.unsaved_confirm']!.other) + dialog!.querySelector('[data-action="cancel"]')!.click() + await flushPromises() + expect(router.currentRoute.value.fullPath).toBe('/summercms/cabana/mailtemplates/7') + expect(select.element.value).toBe('7') + expect((wrapper.find('#field-subject').element as HTMLInputElement).value).toBe('Changed') + expect(calls.some((request) => request.method === 'GET' && request.url.endsWith('/mailtemplates/9'))).toBe(false) + + await select.setValue('9') + await flushPromises() + document.body.querySelector('[data-confirm-dialog] [data-action="confirm"]')!.click() + await flushPromises() + expect(router.currentRoute.value.fullPath).toBe('/summercms/cabana/mailtemplates/9') + expect(calls.some((request) => request.method === 'GET' && request.url.endsWith('/mailtemplates/9'))).toBe(true) }) it('toasts 403 and 409 paths from record actions', async () => { diff --git a/admin/tests/form/LocaleVariantSwitcher.test.ts b/admin/tests/form/LocaleVariantSwitcher.test.ts index 5937630c..1ac57b82 100644 --- a/admin/tests/form/LocaleVariantSwitcher.test.ts +++ b/admin/tests/form/LocaleVariantSwitcher.test.ts @@ -14,15 +14,18 @@ const variants = [ const Stub = defineComponent({ render: () => h('div') }) -async function mountSwitcher(allow: () => boolean) { +async function mountSwitcher( + allow: () => boolean, + options: { list?: typeof variants; path?: string; controlId?: string } = {}, +) { const router = createRouter({ history: createMemoryHistory(), routes: [{ path: '/:vendor/:plugin/:controller/:id', name: 'update', component: Stub }], }) router.beforeEach((_to, from) => (from.matched.length === 0 ? true : allow())) - await router.push('/summercms/cabana/mailtemplates/7') + await router.push(options.path ?? '/summercms/cabana/mailtemplates/7') const wrapper = mount(LocaleVariantSwitcher, { - props: { variants }, + props: { variants: options.list ?? variants, controlId: options.controlId }, global: { plugins: [router] }, attachTo: document.body, }) @@ -72,4 +75,57 @@ describe('LocaleVariantSwitcher', () => { expect(router.currentRoute.value.params.id).toBe('7') expect(select.element.value).toBe('7') }) + + it('selects the variant flagged current, not the first one', async () => { + const list = variants.map((variant) => ({ ...variant, current: variant.id === 9 })) + const { wrapper } = await mountSwitcher(() => true, { list, path: '/summercms/cabana/mailtemplates/9' }) + expect(wrapper.find('select').element.value).toBe('9') + expect(wrapper.findAll('option').map((option) => option.attributes('data-locale'))).toEqual(['', 'de', 'en']) + expect(wrapper.findAll('option').map((option) => option.attributes('value'))).toEqual(['7', '9', '8']) + }) + + it('labels the select through the given control id', async () => { + const { wrapper } = await mountSwitcher(() => true, { controlId: 'form-locale-variant' }) + const select = wrapper.find('select') + expect(select.attributes('id')).toBe('form-locale-variant') + expect(wrapper.find('label').attributes('for')).toBe('form-locale-variant') + expect(document.body.querySelector('#form-locale-variant')?.labels?.[0]?.textContent).toBe('Language') + }) + + it('keeps the query string when it opens a sibling version', async () => { + const { wrapper, router } = await mountSwitcher(() => true, { path: '/summercms/cabana/mailtemplates/7?tab=preview' }) + await wrapper.find('select').setValue('8') + await flushPromises() + expect(router.currentRoute.value.fullPath).toBe('/summercms/cabana/mailtemplates/8?tab=preview') + }) + + it('does not navigate when the open version is chosen again', async () => { + let asked = 0 + const { wrapper, router } = await mountSwitcher(() => { + asked++ + return true + }) + await wrapper.find('select').setValue('7') + await flushPromises() + expect(asked).toBe(0) + expect(router.currentRoute.value.params.id).toBe('7') + }) + + it('restores the selection when the navigation throws', async () => { + const { wrapper, router } = await mountSwitcher(() => { + throw new Error('guard failed') + }) + router.onError(() => {}) + const select = wrapper.find('select') + await select.setValue('9') + await flushPromises() + expect(router.currentRoute.value.params.id).toBe('7') + expect(select.element.value).toBe('7') + }) + + it('follows a new current variant from the props', async () => { + const { wrapper } = await mountSwitcher(() => true) + await wrapper.setProps({ variants: variants.map((variant) => ({ ...variant, current: variant.id === 8 })) }) + expect(wrapper.find('select').element.value).toBe('8') + }) }) diff --git a/scripts/check-phase35.sh b/scripts/check-phase35.sh index 6c46e49a..c8843d4c 100755 --- a/scripts/check-phase35.sh +++ b/scripts/check-phase35.sh @@ -134,6 +134,7 @@ ADMIN_FILES=( tests/form/ColorPickerField.test.ts tests/form/MailImageField.test.ts tests/form/FormViewMail.test.ts + tests/form/LocaleVariantSwitcher.test.ts tests/list/RowStateBadges.test.ts tests/views/SettingsFormView.test.ts )