Files
baserow/web-frontend/test/unit/database/components/field/fieldContextWorkflowActions.spec.js
alamin-br 25335530f8 feat: undo for button field action configuration (#6086)
* feat: undo for button field action configuration

Creating, editing, reordering and deleting a button's actions bypassed undo, so Ctrl+Z restored the field but not its actions.

* fix: keep button action undo working across edits and type changes

A type change dropped trashed actions and gave restored ones new ids, so redo failed.
A reopened editor kept stale card values and could revert an undo on save.
A concurrent delete of one action returned a 500.

* test: e2e coverage for button field action undo

Covers one undo reverting a whole editor save, redo, and redo after a field is changed into a button.

* fix: skip the order call when a button save keeps the order

Each call adds an undo step, and an undo group only reverts 20 of them.

* docs: document create_instance_from_serialized parameters

* fix: keep sensitive fields when undoing a button action type change

A type change deleted the old service, and undo rebuilt it from logged values that leave sensitive fields out.
The replaced service is now kept until the undo step is cleaned up.

* fix: remove a button action's trash entry when the action is deleted

A field changing away from a button deleted its trashed actions but left their entries listed in the trash.

* fix: harden button action undo around secrets and field type changes

Field backups logged action secrets, undo reattached integrations unchecked, and a field type round trip broke redo.
Sensitive-only edits added empty undo steps, and the delete race error could never be reached.

* fix: rebuild button action cards when the editor is cancelled

Each card copies its values once, so a cancelled edit stayed visible and could be saved later.

* fix: skip undo of a button action already restored from the trash

* fix: restore a deleted button action to its old position on undo

* fix: check the workflow when undo brings back a start workflow action

* perf: check a kept button action service in one query

* fix: name a trashed button action by its position

* chore: invoke button action types through the action registry

* fix: send a button save too big for one undo group ungrouped

* docs: spell out what button action undo leaves unreverted
2026-09-16 18:48:00 +02:00

522 lines
18 KiB
JavaScript

import { vi } from 'vitest'
import { TestApp } from '@baserow/test/helpers/testApp'
import UpdateFieldContext from '@baserow/modules/database/components/field/UpdateFieldContext'
import CreateFieldContext from '@baserow/modules/database/components/field/CreateFieldContext'
/**
* The field create/update response carries a `has_workflow_actions` computed
* before `afterFieldSaved` runs, and the store commits it verbatim, so
* every cell renders the wrong branch until a reload. These tests pin that the
* contexts correct the flag after the actions are saved, in both directions.
*/
describe('field contexts keep has_workflow_actions in sync', () => {
let testApp = null
let client = null
// What the stubbed field form reports the save response got wrong. `null`
// stands for a field type with nothing to correct.
let reportedValuesAfterSave = null
// Set by a test to make the stubbed form's action save fail.
let actionSaveError = null
// The options each `afterFieldSaved` call was given.
let afterFieldSavedOptions = []
const table = { id: 1 }
const view = { id: 1, type: 'grid' }
const database = { id: 1, workspace: { id: 1 } }
const allFieldsInTable = [{ id: 1, type: 'text', name: 'Name' }]
// Only what the context does with the form's answer matters here. The
// editor itself is covered by fieldButtonSubForm.spec.js.
const FieldFormStub = {
name: 'FieldForm',
props: [
'table',
'view',
'primary',
'forcedType',
'allFieldsInTable',
'defaultValues',
'database',
],
methods: {
async afterFieldSaved(fieldId, options) {
afterFieldSavedOptions.push(options)
if (actionSaveError !== null) {
throw actionSaveError
}
},
fieldValuesAfterSave() {
return reportedValuesAfterSave
},
reset() {},
handleErrorByForm() {
return false
},
isDescriptionFieldNotEmpty() {
return false
},
},
template: '<div />',
}
const buttonField = (overrides = {}) => ({
id: 7,
table_id: 1,
name: 'Go',
type: 'button',
label: 'Go',
has_workflow_actions: false,
primary: false,
read_only: false,
related_fields: [],
...overrides,
})
const mountContext = async (component, propsData) => {
const wrapper = await testApp.mount(component, {
propsData,
global: { stubs: { FieldForm: FieldFormStub } },
})
wrapper.vm.$refs.context.forceRender()
await wrapper.vm.$nextTick()
return wrapper
}
beforeEach(async () => {
testApp = new TestApp()
reportedValuesAfterSave = null
actionSaveError = null
afterFieldSavedOptions = []
client = testApp.getApp().$client
vi.spyOn(client, 'get').mockResolvedValue({ data: [] })
vi.spyOn(client, 'post').mockResolvedValue({ data: {} })
vi.spyOn(client, 'patch').mockResolvedValue({ data: {} })
vi.spyOn(client, 'delete').mockResolvedValue({ data: {} })
})
afterEach(async () => {
await testApp.afterEach()
vi.restoreAllMocks()
})
test('an update saves the field and its actions under one undo group', async () => {
// Undo works on the group, so a save that changed the label and an action
// is taken back by one undo rather than one per request.
const field = buttonField()
await testApp.store.dispatch('field/forceSetFields', { fields: [field] })
client.patch.mockResolvedValue({ data: buttonField() })
const wrapper = await mountContext(UpdateFieldContext, {
table,
field,
view,
allFieldsInTable,
database,
})
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
const patched = client.patch.mock.calls[0][2]
expect(patched.headers.ClientUndoRedoActionGroupId).toBeTruthy()
expect(afterFieldSavedOptions).toEqual([
{ undoRedoActionGroupId: patched.headers.ClientUndoRedoActionGroupId },
])
})
test('each update save gets an undo group of its own', async () => {
const field = buttonField()
await testApp.store.dispatch('field/forceSetFields', { fields: [field] })
client.patch.mockResolvedValue({ data: buttonField() })
const wrapper = await mountContext(UpdateFieldContext, {
table,
field,
view,
allFieldsInTable,
database,
})
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Again' })
const [first, second] = afterFieldSavedOptions
expect(first.undoRedoActionGroupId).not.toBe(second.undoRedoActionGroupId)
})
test('a create saves the field and its actions under one undo group', async () => {
client.post.mockResolvedValue({ data: buttonField() })
const wrapper = await mountContext(CreateFieldContext, {
table,
view,
allFieldsInTable,
database,
})
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
const created = client.post.mock.calls[0][2]
expect(afterFieldSavedOptions).toEqual([
{ undoRedoActionGroupId: created.headers.ClientUndoRedoActionGroupId },
])
})
test('a failed action save leaves the editor open on the edits', async () => {
// Closing would drop whatever did not save, leaving only a toast. The
// field itself did save, so its callback still has to run.
const field = buttonField()
await testApp.store.dispatch('field/forceSetFields', { fields: [field] })
const wrapper = await mountContext(UpdateFieldContext, {
table,
field,
view,
allFieldsInTable,
database,
})
client.patch.mockResolvedValue({ data: { ...field } })
// Shaped like an API error, which is what `notifyIf` turns into a toast
// rather than re-throwing.
actionSaveError = { handler: { notifyIf: vi.fn() } }
const hide = vi.spyOn(wrapper.vm, 'hide')
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
await wrapper.emitted('update')[0][0].callback()
expect(hide).not.toHaveBeenCalled()
expect(wrapper.emitted('updated')).toBeUndefined()
expect(wrapper.vm.loading).toBe(false)
})
test('a save that works closes the editor', async () => {
const field = buttonField()
await testApp.store.dispatch('field/forceSetFields', { fields: [field] })
const wrapper = await mountContext(UpdateFieldContext, {
table,
field,
view,
allFieldsInTable,
database,
})
client.patch.mockResolvedValue({ data: { ...field } })
const hide = vi.spyOn(wrapper.vm, 'hide')
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
await wrapper.emitted('update')[0][0].callback()
expect(hide).toHaveBeenCalled()
expect(wrapper.emitted('updated')).toHaveLength(1)
})
test('a first action flips the flag the update response left false', async () => {
const field = buttonField()
await testApp.store.dispatch('field/forceSetFields', { fields: [field] })
const wrapper = await mountContext(UpdateFieldContext, {
table,
field,
view,
allFieldsInTable,
database,
})
// The response was built before the action existed.
client.patch.mockResolvedValue({
data: { ...field, has_workflow_actions: false },
})
reportedValuesAfterSave = { has_workflow_actions: true }
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
await wrapper.emitted('update')[0][0].callback()
expect(testApp.store.getters['field/get'](7).has_workflow_actions).toBe(
true
)
})
test('losing the last action clears the flag the update response left true', async () => {
const field = buttonField({ has_workflow_actions: true })
await testApp.store.dispatch('field/forceSetFields', { fields: [field] })
const wrapper = await mountContext(UpdateFieldContext, {
table,
field,
view,
allFieldsInTable,
database,
})
// The response still describes the action that the save then removed.
client.patch.mockResolvedValue({
data: { ...field, has_workflow_actions: true },
})
reportedValuesAfterSave = { has_workflow_actions: false }
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
await wrapper.emitted('update')[0][0].callback()
expect(testApp.store.getters['field/get'](7).has_workflow_actions).toBe(
false
)
})
test('a field type without workflow actions is left alone', async () => {
const field = {
id: 8,
table_id: 1,
name: 'Name',
type: 'text',
primary: false,
read_only: false,
related_fields: [],
}
await testApp.store.dispatch('field/forceSetFields', { fields: [field] })
const wrapper = await mountContext(UpdateFieldContext, {
table,
field,
view,
allFieldsInTable,
database,
})
client.patch.mockResolvedValue({ data: { ...field } })
reportedValuesAfterSave = null
await wrapper.vm.submit({ name: 'Name', type: 'text' })
await wrapper.emitted('update')[0][0].callback()
expect(
testApp.store.getters['field/get'](8).has_workflow_actions
).toBeUndefined()
})
test('a created button field is stored with the flag its actions imply', async () => {
const wrapper = await mountContext(CreateFieldContext, {
table,
view,
allFieldsInTable,
database,
})
client.post.mockResolvedValue({
data: buttonField({ has_workflow_actions: false }),
})
reportedValuesAfterSave = { has_workflow_actions: true }
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
await wrapper.emitted('field-created')[0][0].callback()
expect(testApp.store.getters['field/get'](7).has_workflow_actions).toBe(
true
)
})
/**
* Creating the field and saving its actions are two calls, so the second can
* fail on a field that exists. Closing on that would discard the whole
* configuration the user just typed.
*/
describe('a creation whose actions fail', () => {
const createWithFailingActions = async () => {
const wrapper = await mountContext(CreateFieldContext, {
table,
view,
allFieldsInTable,
database,
})
client.post.mockResolvedValue({ data: buttonField() })
client.patch.mockResolvedValue({ data: buttonField() })
// Shaped like an API error, which `notifyIf` turns into a toast rather
// than re-throwing.
actionSaveError = { handler: { notifyIf: vi.fn() } }
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
await wrapper.emitted('field-created')[0][0].callback()
return wrapper
}
test('keeps the editor open on the edits', async () => {
const wrapper = await createWithFailingActions()
expect(wrapper.vm.createdField.id).toBe(7)
expect(wrapper.vm.loading).toBe(false)
// Without this the form checks its name against the field it just made.
expect(wrapper.vm.defaultValues.id).toBe(7)
expect(wrapper.emitted('field-created-callback-done')).toBeUndefined()
// The field itself did save, so the view still has to hear about it.
expect(testApp.store.getters['field/get'](7)).toBeTruthy()
})
test('saves a retry against that field rather than a second one', async () => {
const wrapper = await createWithFailingActions()
const hide = vi.spyOn(wrapper.vm, 'hide')
actionSaveError = null
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
// The parent refreshes its rows first, then runs the callback.
await wrapper.emitted('field-created')[1][0].callback()
expect(client.post).toHaveBeenCalledTimes(1)
expect(client.patch).toHaveBeenCalledWith(
'/database/fields/7/',
expect.objectContaining({ name: 'Go', type: 'button' }),
expect.anything()
)
expect(hide).toHaveBeenCalled()
expect(wrapper.vm.createdField).toBe(null)
expect(wrapper.emitted('field-created-callback-done')).toHaveLength(1)
})
test('reports the retried field as it stands afterwards', async () => {
const wrapper = await createWithFailingActions()
actionSaveError = null
client.patch.mockResolvedValue({ data: buttonField({ name: 'Renamed' }) })
await wrapper.vm.submit({ name: 'Renamed', type: 'button', label: 'Go' })
await wrapper.emitted('field-created')[1][0].callback()
const done = wrapper.emitted('field-created-callback-done')
expect(done).toHaveLength(1)
// The copy taken when the field was made still calls it "Go".
expect(done[0][0].newField.name).toBe('Renamed')
})
test('the retry patch is part of the create group', async () => {
// Undo works on the group, so a patch outside it leaves the create to be
// undone on its own: the field is trashed and the patch has nothing left
// to undo.
const wrapper = await createWithFailingActions()
const created = client.post.mock.calls[0][2]
actionSaveError = null
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
const patched = client.patch.mock.calls[0][2]
expect(created.headers.ClientUndoRedoActionGroupId).toBeTruthy()
expect(patched?.headers?.ClientUndoRedoActionGroupId).toBe(
created.headers.ClientUndoRedoActionGroupId
)
// The retried actions too, on the attempt that failed and on this one.
expect(afterFieldSavedOptions).toEqual([
{ undoRedoActionGroupId: created.headers.ClientUndoRedoActionGroupId },
{ undoRedoActionGroupId: created.headers.ClientUndoRedoActionGroupId },
])
})
test('the retry lets the parent refresh its rows first', async () => {
// The create path hands the parent a callback so the rows are refreshed
// before the field is committed. A retry that changes the type needs the
// same, or the rows are read against the wrong column.
const wrapper = await createWithFailingActions()
actionSaveError = null
await wrapper.vm.submit({ name: 'Go', type: 'button', label: 'Go' })
const created = wrapper.emitted('field-created')
expect(created).toHaveLength(2)
// Nothing is finished until the parent runs the callback.
expect(wrapper.emitted('field-created-callback-done')).toBeUndefined()
await created[1][0].callback()
expect(wrapper.emitted('field-created-callback-done')).toHaveLength(1)
})
test('dismissing a retry in flight reports the field once', async () => {
const wrapper = await createWithFailingActions()
actionSaveError = null
let releasePatch = null
client.patch.mockReturnValue(
new Promise((resolve) => {
releasePatch = () => resolve({ data: buttonField() })
})
)
const retrying = wrapper.vm.submit({
name: 'Go',
type: 'button',
label: 'Go',
})
wrapper.vm.hide()
releasePatch()
await retrying
const created = wrapper.emitted('field-created')
await created[created.length - 1][0].callback()
expect(wrapper.emitted('field-created-callback-done')).toHaveLength(1)
})
test('reports the field once the retry is abandoned', async () => {
const wrapper = await createWithFailingActions()
wrapper.vm.hide()
const done = wrapper.emitted('field-created-callback-done')
expect(done).toHaveLength(1)
expect(done[0][0].newField.id).toBe(7)
expect(wrapper.vm.createdField).toBe(null)
expect(wrapper.vm.defaultValues.id).toBeUndefined()
})
test('dismissing a retry that then fails still reports the field', async () => {
// `@hidden` fires while the retry runs and does not fire again. The
// retry failing reports nothing either, so without the close being held
// the field is never handed over and the context stays on it: the next
// create would patch this field instead of making a new one.
const wrapper = await createWithFailingActions()
let releasePatch = null
client.patch.mockReturnValue(
new Promise((resolve) => {
releasePatch = () => resolve({ data: buttonField() })
})
)
const retrying = wrapper.vm.submit({
name: 'Go',
type: 'button',
label: 'Go',
})
wrapper.vm.hide()
releasePatch()
await retrying
const created = wrapper.emitted('field-created')
await created[created.length - 1][0].callback()
expect(wrapper.emitted('field-created-callback-done')).toHaveLength(1)
expect(wrapper.vm.createdField).toBe(null)
expect(wrapper.vm.defaultValues.id).toBeUndefined()
})
test('dismissing a create whose actions fail still reports the field', async () => {
// The same window on the first attempt: the field is made, the actions
// are not, and the close lands before the callback settles it.
const wrapper = await mountContext(CreateFieldContext, {
table,
view,
allFieldsInTable,
database,
})
let releasePost = null
client.post.mockReturnValue(
new Promise((resolve) => {
releasePost = () => resolve({ data: buttonField() })
})
)
actionSaveError = { handler: { notifyIf: vi.fn() } }
const creating = wrapper.vm.submit({
name: 'Go',
type: 'button',
label: 'Go',
})
wrapper.vm.hide()
releasePost()
await creating
await wrapper.emitted('field-created')[0][0].callback()
expect(wrapper.emitted('field-created-callback-done')).toHaveLength(1)
expect(wrapper.vm.createdField).toBe(null)
expect(wrapper.vm.defaultValues.id).toBeUndefined()
})
})
})