fix: corrects block duplicate action and add tests (#6589)

This commit is contained in:
Jessica Chowdhury
2024-06-12 14:44:17 -04:00
committed by GitHub
parent be0462db56
commit 763a34f19b
2 changed files with 17 additions and 2 deletions

View File

@@ -247,7 +247,7 @@ export function fieldReducer(state: FormState, action: FieldAction): FormState {
case 'DUPLICATE_ROW': {
const { path, rowIndex } = action
const { remainingFields, rows } = separateRows(path, state)
const rowsMetadata = state[path]?.rows || []
const rowsMetadata = [...(state[path].rows || [])]
const duplicateRowMetadata = deepCopyObject(rowsMetadata[rowIndex])
if (duplicateRowMetadata.id) duplicateRowMetadata.id = new ObjectId().toHexString()
@@ -264,13 +264,13 @@ export function fieldReducer(state: FormState, action: FieldAction): FormState {
const newState = {
...remainingFields,
...flattenRows(path, rows),
[path]: {
...state[path],
disableFormData: true,
rows: rowsMetadata,
value: rows.length,
},
...flattenRows(path, rows),
}
return newState

View File

@@ -115,6 +115,21 @@ describe('Block fields', () => {
await expect(addedRow.locator('.blocks-field__block-pill-content')).toContainText('Content') // went from `Number` to `Content`
})
test('should duplicate block', async () => {
await page.goto(url.create)
const firstRow = page.locator('#field-blocks #blocks-row-0')
const rowActions = firstRow.locator('.collapsible__actions')
await expect(rowActions).toBeVisible()
await rowActions.locator('.array-actions__button').click()
const duplicateButton = rowActions.locator('.array-actions__action.array-actions__duplicate')
await expect(duplicateButton).toBeVisible()
await duplicateButton.click()
const blocks = page.locator('#field-blocks > .blocks-field__rows > div')
expect(await blocks.count()).toEqual(4)
})
test('should use i18n block labels', async () => {
await page.goto(url.create)
await expect(page.locator('#field-i18nBlocks .blocks-field__header')).toContainText('Block en')