import { flushPromises } from '@vue/test-utils' import { TestApp } from '@baserow/test/helpers/testApp' import CreateTableModal from '@baserow/modules/database/components/table/CreateTableModal' import CreateDataSync from '@baserow/modules/database/components/table/CreateDataSync' describe('CreateTableModal', () => { let testApp = null beforeEach(() => { testApp = new TestApp() }) afterEach(async () => { await testApp.afterEach() }) test('switches from a data sync back to a new table', async () => { const wrapper = await testApp.mount(CreateTableModal, { propsData: { database: { id: 1, workspace: { id: 1 }, tables: [] } }, }) await wrapper.vm.show() await flushPromises() wrapper.vm.setChosenType('ical_calendar') await flushPromises() // The data sync form is still the rendered one when the type changes. expect(() => wrapper.vm.setChosenType('')).not.toThrow() await flushPromises() expect(wrapper.vm.isImporter).toBe(true) }) const mountDataSync = (testApp, chosenType) => testApp.mount(CreateDataSync, { propsData: { database: { id: 1, workspace: { id: 1 }, tables: [] }, chosenType, }, // The type specific forms aren't needed here. global: { stubs: { TableForm: true } }, }) test('switching data sync type turns the sync options back to defaults', async () => { const wrapper = await mountDataSync(testApp, 'postgresql') wrapper.vm.twoWaySync = true wrapper.vm.autoAddNewProperties = true wrapper.vm.deleteUnmatchedRows = false // iCal doesn't support two-way sync, so a leftover `true` is rejected by // the backend. await wrapper.setProps({ chosenType: 'ical_calendar' }) const { twoWaySync, autoAddNewProperties, deleteUnmatchedRows } = wrapper.vm expect({ twoWaySync, autoAddNewProperties, deleteUnmatchedRows }).toEqual({ twoWaySync: false, autoAddNewProperties: false, deleteUnmatchedRows: true, }) }) test('shows the error and the form when a restored sync fails', async () => { const wrapper = await mountDataSync(testApp, 'ical_calendar') wrapper.vm.job = { id: 1, state: 'started', progress_percentage: 10 } wrapper.vm.restoredFromStore = true await flushPromises() wrapper.vm.job = { id: 1, state: 'failed', human_readable_error: 'The calendar URL is unreachable', } await flushPromises() expect(wrapper.vm.restoredFromStore).toBe(false) expect(wrapper.text()).toContain('The calendar URL is unreachable') }) test('ignores properties that arrive after the type changed', async () => { let reply testApp.mock .onPost('/database/data-sync/database/1/properties/') .reply(() => new Promise((resolve) => (reply = resolve))) const wrapper = await mountDataSync(testApp, 'postgresql') const submitted = wrapper.vm.submitted({ name: 'Table' }) await flushPromises() await wrapper.setProps({ chosenType: 'ical_calendar' }) reply([200, [{ key: 'id', name: 'id', initially_selected: true }]]) await submitted const { loadedProperties, loadingProperties, properties } = wrapper.vm expect({ loadedProperties, loadingProperties, properties }).toEqual({ loadedProperties: false, loadingProperties: false, properties: null, }) }) })