mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +00:00
* feat: let builder data sources be tested and work without an integration * fix: return a clear error when testing a data source with a missing sort or filter field
214 lines
6.5 KiB
JavaScript
214 lines
6.5 KiB
JavaScript
import { expect } from 'vitest'
|
|
import { MockServer } from '@baserow/test/fixtures/mockServer'
|
|
import MockAdapter from 'axios-mock-adapter'
|
|
import dataSourceStore from '@baserow/modules/builder/store/dataSource'
|
|
|
|
describe('dataSource store', () => {
|
|
let testApp = null
|
|
let store = null
|
|
let mockServer = null
|
|
let mock = null
|
|
|
|
beforeEach(() => {
|
|
testApp = useNuxtApp()
|
|
const { $store, $client, $registry } = useNuxtApp()
|
|
store = $store
|
|
mock = new MockAdapter($client, { onNoMatch: 'throwException' })
|
|
mockServer = new MockServer(mock, $store)
|
|
})
|
|
|
|
afterEach(() => {
|
|
mock.restore()
|
|
})
|
|
|
|
test('getPageDataSources', () => {
|
|
const page = {
|
|
id: 42,
|
|
dataSources: [
|
|
{ type: null },
|
|
{ type: 'local_baserow_list_rows' },
|
|
{ type: 'local_baserow_get_row' },
|
|
],
|
|
}
|
|
|
|
const collectionDataSources =
|
|
store.getters['dataSource/getPageDataSources'](page)
|
|
|
|
expect(collectionDataSources.length).toBe(3)
|
|
})
|
|
|
|
test('getPagesDataSources skips pages whose data sources are not loaded', () => {
|
|
// A builder's shared page can exist in the store (e.g. listed in the sidebar
|
|
// while on a non-builder scope) without its `dataSources` being loaded.
|
|
const loadedPage = { id: 1, dataSources: [{ id: 10 }, { id: 11 }] }
|
|
const unloadedPage = { id: 2 }
|
|
|
|
const result = store.getters['dataSource/getPagesDataSources']([
|
|
loadedPage,
|
|
unloadedPage,
|
|
])
|
|
|
|
expect(result).toEqual([{ id: 10 }, { id: 11 }])
|
|
})
|
|
|
|
test.each(['UPDATE_ITEM', 'FULL_UPDATE_ITEM'])(
|
|
'%s ignores a source that is no longer in the page',
|
|
(mutation) => {
|
|
const page = {
|
|
dataSources: [
|
|
{ id: 1, name: 'First' },
|
|
{ id: 2, name: 'Second' },
|
|
{ id: 3, name: 'Third' },
|
|
],
|
|
}
|
|
const originalSources = structuredClone(page.dataSources)
|
|
store.commit(`dataSource/${mutation}`, {
|
|
page,
|
|
dataSource: { id: 99 },
|
|
values: { id: 99, name: 'Late response' },
|
|
})
|
|
expect(page.dataSources).toEqual(originalSources)
|
|
}
|
|
)
|
|
|
|
test('fetch', async () => {
|
|
const page = {
|
|
id: 42,
|
|
dataSources: [],
|
|
_: {},
|
|
}
|
|
|
|
// Mock the fetch call
|
|
mockServer.mock
|
|
.onGet(`builder/page/42/data-sources/`)
|
|
.replyOnce(200, [
|
|
{ type: null },
|
|
{ type: 'local_baserow_list_rows' },
|
|
{ type: 'local_baserow_get_row' },
|
|
])
|
|
|
|
expect(store.getters['dataSource/getFetched'](page)).toBe(false)
|
|
|
|
await store.dispatch('dataSource/fetch', {
|
|
page,
|
|
})
|
|
|
|
const collectionDataSources =
|
|
store.getters['dataSource/getPageDataSources'](page)
|
|
|
|
expect(collectionDataSources.length).toBe(3)
|
|
expect(store.getters['dataSource/getFetched'](page)).toBe(true)
|
|
})
|
|
|
|
test('simulateDispatch stores the tested data source', async () => {
|
|
const page = {
|
|
id: 42,
|
|
dataSources: [{ id: 7, name: 'Products', sample_data: null }],
|
|
_: {},
|
|
}
|
|
const testedDataSource = {
|
|
id: 7,
|
|
name: 'Products',
|
|
sample_data: { data: { name: 'Lamp' }, status: 200 },
|
|
schema: { type: 'object', properties: { name: { type: 'string' } } },
|
|
}
|
|
mock
|
|
.onPost('builder/data-source/7/simulate-dispatch/', {
|
|
metadata: { page_parameter: { id: 1 } },
|
|
})
|
|
.replyOnce(200, testedDataSource)
|
|
|
|
await store.dispatch('dataSource/simulateDispatch', {
|
|
page,
|
|
dataSourceId: 7,
|
|
dispatchContext: { page_parameter: { id: 1 } },
|
|
})
|
|
|
|
expect(store.getters['dataSource/getPageDataSourceById'](page, 7)).toEqual(
|
|
testedDataSource
|
|
)
|
|
})
|
|
|
|
test('fetchPublished uses the selected builder in preview mode', async () => {
|
|
const builder = { id: 123, _: { selected: false } }
|
|
const page = { id: 42, dataSources: [], _: {} }
|
|
store.commit('application/SET_ITEMS', [builder])
|
|
store.commit('application/SET_SELECTED', builder)
|
|
await store.dispatch('publicBuilder/setPageMode', 'preview')
|
|
expect(store.getters['publicBuilder/getPreviewBuilderId']).toBe(123)
|
|
mock.onGet('builder/preview/123/pages/42/data-sources/').replyOnce(200, [])
|
|
|
|
await store.dispatch('dataSource/fetchPublished', { page })
|
|
|
|
expect(mock.history.get).toHaveLength(1)
|
|
})
|
|
})
|
|
|
|
describe('dataSource redispatchForIntegration', () => {
|
|
const builder = { id: 1, type: 'builder' }
|
|
const selectedPage = { id: 10 }
|
|
const sharedPage = { id: 99 }
|
|
|
|
const run = (dataSources, integrationId) => {
|
|
const dispatch = vi.fn()
|
|
const element = { id: 100 }
|
|
const rootGetters = {
|
|
'page/getSelected': selectedPage,
|
|
'page/getSharedPage': () => sharedPage,
|
|
'element/getElementsOrdered': (page) =>
|
|
page.id === selectedPage.id ? [element] : [],
|
|
}
|
|
const getters = { getPagesDataSources: () => dataSources }
|
|
dataSourceStore.actions.redispatchForIntegration(
|
|
{ dispatch, getters, rootGetters },
|
|
{ builder, integrationId }
|
|
)
|
|
return { dispatch, element }
|
|
}
|
|
|
|
test('re-dispatches only the data sources bound to the integration', () => {
|
|
const { dispatch, element } = run(
|
|
[
|
|
{ id: 1, integration_id: 5 },
|
|
{ id: 2, integration_id: 6 },
|
|
{ id: 3, integration_id: 5 },
|
|
],
|
|
5
|
|
)
|
|
|
|
const calls = dispatch.mock.calls.filter(
|
|
([name]) => name === 'element/emitElementEvent'
|
|
)
|
|
expect(calls.map(([, payload]) => payload.dataSourceId)).toEqual([1, 3])
|
|
calls.forEach(([, payload, opts]) => {
|
|
expect(payload.event).toBe('DATA_SOURCE_AFTER_UPDATE')
|
|
expect(payload.elements).toEqual([element])
|
|
expect(payload.builder).toBe(builder)
|
|
expect(opts).toEqual({ root: true })
|
|
})
|
|
})
|
|
|
|
test('does nothing when no data source uses the integration', () => {
|
|
const { dispatch } = run([{ id: 1, integration_id: 6 }], 5)
|
|
expect(dispatch).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('does nothing for non-builder applications', () => {
|
|
// Integration realtime events fire for every application type (e.g.
|
|
// automations). Those have no pages, so the action must bail out instead
|
|
// of resolving the shared page (which would crash).
|
|
const dispatch = vi.fn()
|
|
const rootGetters = {
|
|
'page/getSelected': selectedPage,
|
|
'page/getSharedPage': () => {
|
|
throw new Error('must not resolve pages for non-builder applications')
|
|
},
|
|
}
|
|
dataSourceStore.actions.redispatchForIntegration(
|
|
{ dispatch, getters: {}, rootGetters },
|
|
{ builder: { id: 2, type: 'automation' }, integrationId: 5 }
|
|
)
|
|
expect(dispatch).not.toHaveBeenCalled()
|
|
})
|
|
})
|