import { defineComponent, nextTick } from 'vue' import { mountSuspended } from '@nuxt/test-utils/runtime' import { flushPromises } from '@vue/test-utils' import { DataProviderType } from '@baserow/modules/core/dataProviderTypes' import DataSourceForm from '@baserow/modules/builder/components/dataSource/DataSourceForm' import { CoreHTTPDataSourceServiceType } from '@baserow/modules/integrations/core/serviceTypes' const FormGroupStub = defineComponent({ name: 'FormGroup', template: '
', }) const FormInputStub = defineComponent({ name: 'FormInput', props: { modelValue: { type: String, required: false, default: '', }, }, emits: ['update:modelValue', 'blur'], template: '', }) const GroupedDropdownStub = defineComponent({ name: 'GroupedDropdown', props: { modelValue: { type: String, required: false, default: null, }, items: { type: Array, required: true, }, placeholder: { type: String, required: false, default: '', }, searchPlaceholder: { type: String, required: false, default: '', }, emptyText: { type: String, required: false, default: '', }, panelHeight: { type: String, required: false, default: null, }, }, emits: ['update:modelValue'], template: '
', }) const IntegrationDropdownStub = defineComponent({ name: 'IntegrationDropdown', props: { modelValue: { type: Number, required: false, default: null, }, application: { type: Object, required: true, }, integrations: { type: Array, required: true, }, integrationType: { type: Object, required: false, default: null, }, disabled: { type: Boolean, required: false, default: false, }, placeholder: { type: String, required: false, default: '', }, }, emits: ['update:modelValue'], template: '
', }) const localBaserowIntegrationType = { name: 'Local Baserow', image: '/local-baserow.svg', iconClass: 'iconoir-database', iconColor: 'darker-blue', getType: () => 'local_baserow', } const otherIntegrationType = { name: 'Other', image: '/other.svg', getType: () => 'other', } const serviceType = { name: 'Get row', icon: 'iconoir-pin', iconColor: 'darker-blue', description: 'Reads a row from a Baserow table.', integrationType: localBaserowIntegrationType, isDataSource: true, formComponent: null, getType: () => 'local_baserow_get_row', isDeactivatedReason: () => null, } const httpGroup = { id: 'http', label: 'HTTP', icon: 'iconoir-globe', iconColor: 'muted-cyan', } const ServiceFormStub = defineComponent({ name: 'ServiceForm', template: '
', }) const httpServiceType = { name: 'HTTP request', icon: 'iconoir-globe', iconColor: 'muted-cyan', description: 'Fetches data from a URL.', integrationType: null, group: httpGroup, isDataSource: true, canBeTested: true, formComponent: ServiceFormStub, getType: () => 'http_get', isDeactivatedReason: () => null, } const hiddenServiceType = { name: 'Read a CSV file', icon: 'iconoir-page', iconColor: 'muted-yellow', description: 'Reads rows from a CSV file.', integrationType: null, group: { id: 'files', label: 'Files' }, isDataSource: true, hiddenInDataSourcePicker: true, formComponent: null, getType: () => 'csv_file_reader', isDeactivatedReason: () => null, } const BUILDER_ID = 1 // The shared application owning the workspace integrations. const SHARED_APPLICATION_ID = 99 async function mountComponent({ serviceTypes = [serviceType], create = true, dataSource = {}, defaultValues = {}, storeDispatch = () => Promise.resolve(), integrations = [ { id: 1, type: 'local_baserow', application_id: BUILDER_ID }, { id: 2, type: 'other', application_id: BUILDER_ID }, ], } = {}) { const registry = { getOrderedList(namespace) { return namespace === 'service' ? serviceTypes : [localBaserowIntegrationType, otherIntegrationType] }, get(namespace, type) { if (namespace === 'service') { return serviceTypes.find((s) => s.getType() === type) || null } return null }, } return await mountSuspended(DataSourceForm, { props: { builder: { id: BUILDER_ID, workspace: { id: 1 } }, page: { id: 1 }, integrations, create, dataSource, defaultValues, }, global: { provide: { applicationContext: {}, }, stubs: { FormGroup: FormGroupStub, FormInput: FormInputStub, IntegrationDropdown: IntegrationDropdownStub, GroupedDropdown: GroupedDropdownStub, DataSourceTestForm: { name: 'DataSourceTestForm', inject: ['dataSourceTest'], template: '
', }, }, mocks: { $registry: registry, $store: { getters: { 'dataSource/getPageDataSources': () => [], }, dispatch: storeDispatch, }, $t: (key) => key, }, }, }) } describe('DataSourceForm', () => { test('groups service types under their integration type', async () => { const wrapper = await mountComponent() const actionDropdown = wrapper.findComponent(GroupedDropdownStub) expect(actionDropdown.props('panelHeight')).toBe('240px') expect(actionDropdown.props('items')).toEqual([ { id: 'integration-local_baserow', label: 'Local Baserow', image: '/local-baserow.svg', icon: 'iconoir-database', iconColor: 'darker-blue', children: [ { id: 'service-local_baserow_get_row', label: 'Get row', value: 'local_baserow_get_row', icon: 'iconoir-pin', iconColor: 'darker-blue', description: 'Reads a row from a Baserow table.', disabled: false, disabledReason: null, }, ], order: 0, }, ]) }) test('filters and auto-selects connections for the selected action', async () => { const wrapper = await mountComponent() wrapper .findComponent(GroupedDropdownStub) .vm.$emit('update:modelValue', serviceType.getType()) await nextTick() const connectionDropdown = wrapper.findComponent(IntegrationDropdownStub) expect(connectionDropdown.props('integrations')).toEqual([ { id: 1, type: 'local_baserow', application_id: BUILDER_ID }, ]) expect(connectionDropdown.props('modelValue')).toBe(1) }) async function selectServiceType(wrapper) { wrapper .findComponent(GroupedDropdownStub) .vm.$emit('update:modelValue', serviceType.getType()) await nextTick() return wrapper.findComponent(IntegrationDropdownStub).props('modelValue') } const own = (id) => ({ id, type: 'local_baserow', application_id: BUILDER_ID, }) const ofWorkspace = (id) => ({ id, type: 'local_baserow', application_id: SHARED_APPLICATION_ID, }) test.each([ ['own integration', [ofWorkspace(3), own(1)], 1], ['only workspace integration', [ofWorkspace(3)], 3], [ 'ambiguous workspace integrations', [ofWorkspace(3), ofWorkspace(4)], null, ], ])( 'handles a late integration response: %s', async (_, integrations, expected) => { const wrapper = await mountComponent({ integrations: [] }) expect(await selectServiceType(wrapper)).toBe(null) await wrapper.setProps({ integrations }) expect( wrapper.findComponent(IntegrationDropdownStub).props('modelValue') ).toBe(expected) } ) test('keeps a manual choice when the integration list refreshes', async () => { const wrapper = await mountComponent({ integrations: [ofWorkspace(3), ofWorkspace(4)], }) await selectServiceType(wrapper) wrapper .findComponent(IntegrationDropdownStub) .vm.$emit('update:modelValue', 4) await nextTick() await wrapper.setProps({ integrations: [own(1), ofWorkspace(3), ofWorkspace(4)], }) expect( wrapper.findComponent(IntegrationDropdownStub).props('modelValue') ).toBe(4) }) test('does not silently repair an existing source when integrations finish loading', async () => { const wrapper = await mountComponent({ create: false, integrations: [], defaultValues: { type: serviceType.getType(), integration_id: null }, }) await wrapper.setProps({ integrations: [own(1)] }) expect( wrapper.findComponent(IntegrationDropdownStub).props('modelValue') ).toBe(null) }) test('auto-selects the builder connection before a workspace one', async () => { const wrapper = await mountComponent({ integrations: [ofWorkspace(3), own(1)], }) expect(await selectServiceType(wrapper)).toBe(1) }) test('auto-selects the only workspace connection when the builder has none', async () => { const wrapper = await mountComponent({ integrations: [ofWorkspace(3), { ...own(2), type: 'other' }], }) expect(await selectServiceType(wrapper)).toBe(3) }) test.each([ ['of the builder', [own(1), own(2), ofWorkspace(3)]], ['of the workspace', [ofWorkspace(3), ofWorkspace(4)]], ])( 'auto-selects nothing when several connections %s fit', async (_, integrations) => { const wrapper = await mountComponent({ integrations }) expect(await selectServiceType(wrapper)).toBe(null) } ) test('keeps a selected workspace connection that fits the new action', async () => { const wrapper = await mountComponent({ integrations: [own(1), ofWorkspace(3)], defaultValues: { integration_id: 3 }, }) expect(await selectServiceType(wrapper)).toBe(3) }) test('groups service types without an integration under their own group', async () => { const wrapper = await mountComponent({ serviceTypes: [serviceType, httpServiceType, hiddenServiceType], }) const items = wrapper.findComponent(GroupedDropdownStub).props('items') // The menu sorts groups by order before label, so Local Baserow stays // above HTTP. expect(items.map(({ id, order }) => [id, order])).toEqual([ ['integration-local_baserow', 0], ['http', 1], ]) expect(items[1]).toEqual({ id: 'http', label: 'HTTP', image: undefined, icon: 'iconoir-globe', iconColor: 'muted-cyan', children: [ { id: 'service-http_get', label: 'HTTP request', value: 'http_get', icon: 'iconoir-globe', iconColor: 'muted-cyan', description: 'Fetches data from a URL.', disabled: false, disabledReason: null, }, ], order: 1, }) }) test('hides the connection dropdown for a service without an integration', async () => { const wrapper = await mountComponent({ serviceTypes: [serviceType, httpServiceType], }) expect(wrapper.findComponent(IntegrationDropdownStub).exists()).toBe(true) wrapper .findComponent(GroupedDropdownStub) .vm.$emit('update:modelValue', httpServiceType.getType()) await nextTick() expect(wrapper.findComponent(IntegrationDropdownStub).exists()).toBe(false) }) test('shows the service and test forms of a data source without an integration', async () => { const dataSource = { id: 3, name: 'Products', type: 'http_get', integration_id: null, } const wrapper = await mountComponent({ serviceTypes: [serviceType, httpServiceType], create: false, dataSource, defaultValues: dataSource, }) await nextTick() expect(wrapper.find('.service-form-stub').exists()).toBe(true) expect(wrapper.find('.data-source-test-form-stub').exists()).toBe(true) }) describe('with the HTTP request type', () => { const app = { $i18n: { t: (key) => key }, $registry: { get: () => ({}) }, } const httpTypes = () => [new CoreHTTPDataSourceServiceType({ app })] test('the picker shows the HTTP request entry', async () => { const wrapper = await mountComponent({ serviceTypes: httpTypes() }) const items = wrapper.findComponent(GroupedDropdownStub).props('items') expect(items).toHaveLength(1) expect(items[0].id).toBe('http') expect(items[0].children.map(({ value }) => value)).toEqual(['http_get']) }) test('a list data source is shown as the HTTP request entry', async () => { const dataSource = { id: 3, name: 'Products', type: 'http_get', returns_list: true, integration_id: null, } const wrapper = await mountComponent({ serviceTypes: httpTypes(), create: false, dataSource, defaultValues: dataSource, }) await nextTick() expect( wrapper.findComponent(GroupedDropdownStub).props('modelValue') ).toBe('http_get') expect(wrapper.find('.data-source-test-form-stub').exists()).toBe(true) }) }) test('saves unsaved changes before testing', async () => { const EditableServiceFormStub = defineComponent({ name: 'EditableServiceForm', data: () => ({ values: { url: { formula: '', mode: 'simple' } } }), methods: { getFormValues() { return { ...this.values } }, isFormValid() { return true }, }, template: '
', }) const dataSource = { id: 3, name: 'Products', type: 'http_get', integration_id: null, url: { formula: '', mode: 'simple' }, } // The form saves through its own store, the test dispatches through the // app's. const dispatch = vi.fn().mockResolvedValue({}) const simulateDispatch = vi .spyOn(useNuxtApp().$store, 'dispatch') .mockResolvedValue({}) vi.spyOn( DataProviderType, 'getAllDataSourceDispatchContext' ).mockReturnValue({}) const wrapper = await mountComponent({ serviceTypes: [ { ...httpServiceType, formComponent: EditableServiceFormStub }, ], create: false, dataSource, defaultValues: dataSource, storeDispatch: dispatch, }) await nextTick() // Nothing changed, so nothing is saved before the test. await wrapper.find('.data-source-test-form-stub').trigger('click') await flushPromises() expect(dispatch).not.toHaveBeenCalled() expect(simulateDispatch).toHaveBeenCalledTimes(1) expect(wrapper.emitted('saved')).toBeUndefined() wrapper.findComponent(EditableServiceFormStub).vm.values.url = { formula: 'https://httpbin.org/get', mode: 'simple', } await wrapper.find('.data-source-test-form-stub').trigger('click') await flushPromises() expect(dispatch).toHaveBeenCalledWith('dataSource/update', { page: { id: 1 }, dataSourceId: 3, values: { url: { formula: 'https://httpbin.org/get', mode: 'simple' } }, }) // So that the elements using the data source show what it now returns. expect(wrapper.emitted('saved')).toHaveLength(1) expect(simulateDispatch).toHaveBeenLastCalledWith( 'dataSource/simulateDispatch', { page: { id: 1 }, dataSourceId: 3, dispatchContext: {} } ) vi.restoreAllMocks() }) })