- LocaleVariantSwitcher: current flag, option values and locales, the label through the control id, the query kept on navigation, no navigation for the open version, selection restored when a guard throws, props updates - FormView: no switcher with one variant; with unsaved edits switching asks the unsaved-changes confirm, stays on cancel and opens the sibling on confirm - check-phase35.sh requires LocaleVariantSwitcher.test.ts (ADMIN_FILES)
132 lines
5.4 KiB
TypeScript
132 lines
5.4 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
|
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
|
|
import { defineComponent, h } from 'vue'
|
|
import { createMemoryHistory, createRouter } from 'vue-router'
|
|
import LocaleVariantSwitcher from '../../src/components/form/LocaleVariantSwitcher.vue'
|
|
import { setBundle } from '../../src/app/i18n'
|
|
import { resetState } from '../helpers'
|
|
|
|
const variants = [
|
|
{ id: 7, locale: '', label: 'Default', states: [], current: true },
|
|
{ id: 9, locale: 'de', label: 'de', states: ['Customized', 'File changed'], current: false },
|
|
{ id: 8, locale: 'en', label: 'en', states: [], current: false },
|
|
]
|
|
|
|
const Stub = defineComponent({ render: () => h('div') })
|
|
|
|
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(options.path ?? '/summercms/cabana/mailtemplates/7')
|
|
const wrapper = mount(LocaleVariantSwitcher, {
|
|
props: { variants: options.list ?? variants, controlId: options.controlId },
|
|
global: { plugins: [router] },
|
|
attachTo: document.body,
|
|
})
|
|
return { wrapper, router }
|
|
}
|
|
|
|
beforeEach(() => {
|
|
resetState()
|
|
setBundle({ 'backend::lang.form.locale_variant': { other: 'Language' } }, 'en')
|
|
})
|
|
|
|
afterEach(() => {
|
|
document.body.innerHTML = ''
|
|
})
|
|
|
|
enableAutoUnmount(afterEach)
|
|
|
|
describe('LocaleVariantSwitcher', () => {
|
|
it('is a labelled select of the variants with their states, the current one selected', async () => {
|
|
const { wrapper } = await mountSwitcher(() => true)
|
|
const select = wrapper.find<HTMLSelectElement>('select[data-locale-variant]')
|
|
expect(select.exists()).toBe(true)
|
|
const label = wrapper.find('label')
|
|
expect(label.text()).toBe('Language')
|
|
expect(label.attributes('for')).toBe(select.attributes('id'))
|
|
expect(wrapper.findAll('option').map((option) => option.text())).toEqual([
|
|
'Default',
|
|
'de — Customized, File changed',
|
|
'en',
|
|
])
|
|
expect(select.element.value).toBe('7')
|
|
})
|
|
|
|
it('opens the chosen version on the same route', async () => {
|
|
const { wrapper, router } = await mountSwitcher(() => true)
|
|
await wrapper.find('select').setValue('9')
|
|
await flushPromises()
|
|
expect(router.currentRoute.value.params.id).toBe('9')
|
|
expect(router.currentRoute.value.path).toBe('/summercms/cabana/mailtemplates/9')
|
|
})
|
|
|
|
it('puts the selection back when the navigation is refused', async () => {
|
|
const { wrapper, router } = await mountSwitcher(() => false)
|
|
const select = wrapper.find<HTMLSelectElement>('select')
|
|
await select.setValue('8')
|
|
await flushPromises()
|
|
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<HTMLSelectElement>('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<HTMLSelectElement>('#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<HTMLSelectElement>('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<HTMLSelectElement>('select').element.value).toBe('8')
|
|
})
|
|
})
|