feat(admin): follow presets on mltext fields per locale
- formState: changedLocales and presetUpdates hold the preset-follow rules for text and mltext pairs - FormView applies presetUpdates on create, tracks per-locale hand edits of ML fields and follows the active ML locale - rebuilt modules/boardwalk/dist
This commit is contained in:
@@ -553,4 +553,81 @@ describe('password and preset on the form (UI-SPEC S7, D-19, D-27 G7)', () => {
|
||||
await field(update.wrapper, 'name').setValue('Ada King')
|
||||
expect(field(update.wrapper, 'slug').element.value).toBe('ada-lovelace')
|
||||
})
|
||||
|
||||
/** The roster schema with en/pl content locales and name/slug retyped. */
|
||||
function mlSchema(nameType: string, slugType: string) {
|
||||
const envelope = clone(rosterFormSchemaFixture)
|
||||
envelope.data.meta.enabledLocales = ['en', 'pl']
|
||||
for (const item of envelope.data.fields) {
|
||||
if (item.name === 'name') {
|
||||
item.type = nameType
|
||||
} else if (item.name === 'slug') {
|
||||
item.type = slugType
|
||||
}
|
||||
}
|
||||
return { body: envelope }
|
||||
}
|
||||
const editor = (wrapper: Mounted, name: string) => wrapper.find<HTMLInputElement>(`#field-${name}-editor`)
|
||||
const localeSelect = (wrapper: Mounted, name: string) => wrapper.find<HTMLSelectElement>(`[data-field="${name}"] [data-ml-locale]`)
|
||||
const invalidCreate = { status: 422, body: { error: { code: 'validation_failed', message: 'Validation failed', details: {} } } }
|
||||
|
||||
it('follows an mltext source per locale on create, and stops only a locale edited by hand', async () => {
|
||||
const { wrapper, calls } = await mountApp(
|
||||
'/acme/roster/people/create',
|
||||
people({ [`GET ${PEOPLE}/schema/form`]: mlSchema('mltext', 'mltext'), [`POST ${PEOPLE}`]: invalidCreate }),
|
||||
)
|
||||
await editor(wrapper, 'name').setValue('Zażółć gęślą')
|
||||
expect(editor(wrapper, 'slug').element.value).toBe('za-g-l')
|
||||
|
||||
await localeSelect(wrapper, 'name').setValue('pl')
|
||||
await editor(wrapper, 'name').setValue('Witaj Świecie')
|
||||
expect(localeSelect(wrapper, 'slug').element.value).toBe('pl')
|
||||
expect(editor(wrapper, 'slug').element.value).toBe('witaj-wiecie')
|
||||
|
||||
await localeSelect(wrapper, 'name').setValue('en')
|
||||
expect(editor(wrapper, 'slug').element.value).toBe('za-g-l')
|
||||
|
||||
// A hand edit of the pl slug stops pl only.
|
||||
await localeSelect(wrapper, 'slug').setValue('pl')
|
||||
await editor(wrapper, 'slug').setValue('reczny')
|
||||
await editor(wrapper, 'name').setValue('Inny Tytuł')
|
||||
expect(editor(wrapper, 'slug').element.value).toBe('reczny')
|
||||
await localeSelect(wrapper, 'name').setValue('en')
|
||||
await editor(wrapper, 'name').setValue('Hello World')
|
||||
expect(editor(wrapper, 'slug').element.value).toBe('hello-world')
|
||||
|
||||
await save(wrapper)
|
||||
const body = (await requestsTo(calls, 'POST', PEOPLE)[0]!.clone().json()) as Record<string, unknown>
|
||||
expect(body.name).toEqual({ en: 'Hello World', pl: 'Inny Tytuł' })
|
||||
expect(body.slug).toEqual({ en: 'hello-world', pl: 'reczny' })
|
||||
})
|
||||
|
||||
it('does not follow an mltext source on update', async () => {
|
||||
const record = clone(rosterRecordFixture)
|
||||
record.data.name = { en: 'Ada Lovelace', pl: '' }
|
||||
record.data.slug = { en: 'ada-lovelace', pl: '' }
|
||||
const { wrapper } = await mountApp('/acme/roster/people/1', people({ [`GET ${PEOPLE}/schema/form`]: mlSchema('mltext', 'mltext'), [`GET ${PERSON}`]: { body: record } }))
|
||||
await editor(wrapper, 'name').setValue('Ada King')
|
||||
expect(editor(wrapper, 'slug').element.value).toBe('ada-lovelace')
|
||||
})
|
||||
|
||||
it('drives a text slug from the active locale of an mltext name, until edited', async () => {
|
||||
const { wrapper } = await mountApp('/acme/roster/people/create', people({ [`GET ${PEOPLE}/schema/form`]: mlSchema('mltext', 'text') }))
|
||||
await editor(wrapper, 'name').setValue('Hello World')
|
||||
expect(field(wrapper, 'slug').element.value).toBe('hello-world')
|
||||
await localeSelect(wrapper, 'name').setValue('pl')
|
||||
await editor(wrapper, 'name').setValue('Witaj Świecie')
|
||||
expect(field(wrapper, 'slug').element.value).toBe('witaj-wiecie')
|
||||
await field(wrapper, 'slug').setValue('by-hand')
|
||||
await editor(wrapper, 'name').setValue('Inny')
|
||||
expect(field(wrapper, 'slug').element.value).toBe('by-hand')
|
||||
})
|
||||
|
||||
it('fills only the active locale of an mltext slug from a text name', async () => {
|
||||
const { wrapper } = await mountApp('/acme/roster/people/create', people({ [`GET ${PEOPLE}/schema/form`]: mlSchema('text', 'mltext') }))
|
||||
await field(wrapper, 'name').setValue('Hello World')
|
||||
expect(editor(wrapper, 'slug').element.value).toBe('hello-world')
|
||||
await localeSelect(wrapper, 'slug').setValue('pl')
|
||||
expect(editor(wrapper, 'slug').element.value).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user