Files
baserow/web-frontend/test/unit/database/store/field.spec.js
alamin-br 9b4fbdff7e fix: button field follow-ups from the #6086 review (#6135)
* fix: keep a button action card open when its save finishes

A created action is keyed by its new id once the list is refetched, which closed the card while the context was still on screen.

* fix: give the button action error a line height

* fix: outline the selected button field cell

* fix: name the button actions that ran before one failed

Completed actions are not rolled back, and the old message left the clicker to assume nothing had happened.

* fix: disable a button field for users who may not click it

The dispatch operation is now scoped to the field, which is the only id a cell has to ask with. Roles attach no lower than the table, so one check on the field answers the same as one per action.

* fix: refetch permissions when a field is created or restored

A role on the table or database lists the fields it covers, so a field
added after the page loaded was refused by every field scoped check
until a reload.

* test: cover the disabled button in the row modal and on a card

* fix: keep a button action card in place while its save finishes

The refetched list keyed a created or retyped action by its new id, so
its card remounted and the editor scrolled to the top mid-save.
2026-09-23 16:00:14 +02:00

257 lines
6.8 KiB
JavaScript

import { vi } from 'vitest'
import flushPromises from 'flush-promises'
import { TestApp } from '@baserow/test/helpers/testApp'
describe('field store', () => {
let testApp
let store
beforeEach(() => {
testApp = new TestApp()
store = testApp.store
})
afterEach(async () => {
await testApp.afterEach()
})
const workspaceWithTable = async () => {
await store.dispatch('workspace/forceCreate', { id: 1, name: 'Workspace' })
store.commit('workspace/SET_PERMISSIONS', {
workspaceId: 1,
permissions: [],
})
await store.dispatch('application/forceCreate', {
id: 5,
type: 'database',
workspace: { id: 1 },
tables: [{ id: 10, name: 'Table', database_id: 5 }],
})
return { id: 10, database_id: 5 }
}
const permissionRequests = () =>
testApp.mock.history.get.filter((request) =>
request.url.endsWith('/workspaces/1/permissions/')
)
test('a created field refetches the workspace permissions', async () => {
// A role on the table or database lists the fields it covers, and the
// list was fetched before this field existed.
const table = await workspaceWithTable()
testApp.mock
.onGet('/workspaces/1/permissions/')
.reply(200, [{ name: 'core', permissions: ['refetched'] }])
await store.dispatch('field/forceCreate', {
table,
values: { id: 1, table_id: 10, name: 'Go', type: 'text' },
})
await flushPromises()
expect(store.getters['field/get'](1).name).toBe('Go')
expect(store.getters['workspace/get'](1)._.permissions).toEqual([
{ name: 'core', permissions: ['refetched'] },
])
})
test('a restored field refetches the workspace permissions', async () => {
const table = await workspaceWithTable()
testApp.mock
.onGet('/workspaces/1/permissions/')
.reply(200, [{ name: 'core', permissions: ['refetched'] }])
await store.dispatch('field/fieldRestored', {
table,
selectedView: null,
values: { id: 1, table_id: 10, name: 'Go', type: 'text' },
})
await flushPromises()
expect(store.getters['workspace/get'](1)._.permissions).toEqual([
{ name: 'core', permissions: ['refetched'] },
])
})
test('fields landing in a burst share one refetch and one follow-up', async () => {
// An import creates many fields at once. One request runs, one more is
// queued for whatever landed meanwhile, and the newest response is the
// last applied.
const table = await workspaceWithTable()
testApp.mock.onGet('/workspaces/1/permissions/').reply(200, [])
await Promise.all(
[1, 2, 3].map((id) =>
store.dispatch('field/forceCreate', {
table,
values: { id, table_id: 10, name: `Field ${id}`, type: 'text' },
})
)
)
await flushPromises()
expect(permissionRequests()).toHaveLength(2)
})
test('a created field still shows when the permissions cannot be refetched', async () => {
testApp.dontFailOnErrorResponses()
const table = await workspaceWithTable()
testApp.mock.onGet('/workspaces/1/permissions/').reply(500)
await store.dispatch('field/forceCreate', {
table,
values: { id: 1, table_id: 10, name: 'Go', type: 'text' },
})
await flushPromises()
expect(store.getters['field/get'](1).name).toBe('Go')
expect(store.state.toast.permissionsUpdated).toBe(true)
})
test('refreshLoadedFieldErrors updates only errors of cached fields', async () => {
await store.dispatch('field/forceSetFields', {
fields: [
{
id: 1,
table_id: 10,
name: 'AI field',
type: 'text',
error: null,
},
{
id: 2,
table_id: 10,
name: 'Invalid field',
type: 'text',
error: 'Existing error',
},
],
})
store.commit('field/SET_LOADED', { tableId: 10, viewId: null })
const originalField = store.getters['field/get'](1)
testApp.mock.onGet('/database/fields/table/10/').reply(200, [
{
id: 1,
table_id: 10,
name: 'Changed server name',
type: 'text',
error: 'Model unavailable',
},
{
id: 2,
table_id: 10,
name: 'Changed invalid field name',
type: 'text',
error: null,
},
])
await store.dispatch('field/refreshLoadedFieldErrors')
expect(store.getters['field/get'](1)).toBe(originalField)
expect(store.getters['field/get'](1)).toMatchObject({
name: 'AI field',
error: 'Model unavailable',
})
expect(store.getters['field/get'](2)).toMatchObject({
name: 'Invalid field',
error: null,
})
})
test('realtime error recovery requests primary-backed field data', async () => {
await store.dispatch('field/forceSetFields', {
fields: [
{
id: 1,
table_id: 10,
name: 'AI field',
type: 'text',
error: null,
},
],
})
store.commit('field/SET_LOADED', { tableId: 10, viewId: null })
let requestHeaders
testApp.mock.onGet('/database/fields/table/10/').reply((config) => {
requestHeaders = config.headers
return [
200,
[
{
id: 1,
table_id: 10,
name: 'AI field',
type: 'text',
error: null,
},
],
]
})
await store.dispatch('field/refreshLoadedFieldErrors', {
realtimeRecovery: true,
})
expect(requestHeaders['X-Baserow-Realtime-Recovery']).toBe('true')
})
test('an older error refresh cannot overwrite a newer response', async () => {
await store.dispatch('field/forceSetFields', {
fields: [
{
id: 1,
table_id: 10,
name: 'AI field',
type: 'text',
error: null,
},
],
})
store.commit('field/SET_LOADED', { tableId: 10, viewId: null })
const resolvers = []
testApp.mock.onGet('/database/fields/table/10/').reply(
() =>
new Promise((resolve) => {
resolvers.push(resolve)
})
)
const olderRefresh = store.dispatch('field/refreshLoadedFieldErrors')
await vi.waitFor(() => expect(resolvers).toHaveLength(1))
const newerRefresh = store.dispatch('field/refreshLoadedFieldErrors')
await vi.waitFor(() => expect(resolvers).toHaveLength(2))
resolvers[1]([
200,
[
{
id: 1,
table_id: 10,
name: 'AI field',
type: 'text',
error: 'New error',
},
],
])
await newerRefresh
resolvers[0]([
200,
[
{
id: 1,
table_id: 10,
name: 'AI field',
type: 'text',
error: 'Old error',
},
],
])
await olderRefresh
expect(store.getters['field/get'](1).error).toBe('New error')
})
})