2019-01-15 18:53:27 -05:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2018-08-06 18:30:13 -04:00
|
|
|
import React from 'react';
|
|
|
|
import { shallow } from 'enzyme';
|
|
|
|
import configureStore from 'redux-mock-store';
|
2018-10-19 14:41:11 -04:00
|
|
|
import fetchMock from 'fetch-mock';
|
|
|
|
import thunk from 'redux-thunk';
|
2021-06-07 22:12:56 -04:00
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
import { render, screen } from 'spec/helpers/testing-library';
|
|
|
|
|
2021-04-20 11:48:10 -04:00
|
|
|
import { Radio } from 'src/components/Radio';
|
2018-08-06 18:30:13 -04:00
|
|
|
|
2021-06-22 18:07:09 -04:00
|
|
|
import Icons from 'src/components/Icons';
|
2021-04-27 03:15:21 -04:00
|
|
|
import Tabs from 'src/components/Tabs';
|
2020-05-07 17:18:10 -04:00
|
|
|
import DatasourceEditor from 'src/datasource/DatasourceEditor';
|
|
|
|
import Field from 'src/CRUD/Field';
|
2020-11-30 17:23:56 -05:00
|
|
|
import mockDatasource from 'spec/fixtures/mockDatasource';
|
2020-12-01 20:10:33 -05:00
|
|
|
import * as featureFlags from 'src/featureFlags';
|
|
|
|
import TableSelector from 'src/components/TableSelector';
|
2018-08-06 18:30:13 -04:00
|
|
|
|
|
|
|
const props = {
|
|
|
|
datasource: mockDatasource['7__table'],
|
|
|
|
addSuccessToast: () => {},
|
|
|
|
addDangerToast: () => {},
|
2018-10-19 14:41:11 -04:00
|
|
|
onChange: () => {},
|
2018-08-06 18:30:13 -04:00
|
|
|
};
|
2021-08-02 04:55:31 -04:00
|
|
|
const DATASOURCE_ENDPOINT = 'glob:*/datasource/external_metadata_by_name/*';
|
2018-10-19 14:41:11 -04:00
|
|
|
|
2018-08-06 18:30:13 -04:00
|
|
|
describe('DatasourceEditor', () => {
|
2018-10-19 14:41:11 -04:00
|
|
|
const mockStore = configureStore([thunk]);
|
2018-08-06 18:30:13 -04:00
|
|
|
const store = mockStore({});
|
2018-10-19 14:41:11 -04:00
|
|
|
fetchMock.get(DATASOURCE_ENDPOINT, []);
|
2018-08-06 18:30:13 -04:00
|
|
|
|
|
|
|
let wrapper;
|
|
|
|
let el;
|
|
|
|
let inst;
|
2020-12-01 20:10:33 -05:00
|
|
|
let isFeatureEnabledMock;
|
2018-08-06 18:30:13 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-12-09 14:58:42 -05:00
|
|
|
el = <DatasourceEditor {...props} store={store} />;
|
|
|
|
wrapper = shallow(el).dive();
|
2018-08-06 18:30:13 -04:00
|
|
|
inst = wrapper.instance();
|
|
|
|
});
|
|
|
|
|
2021-06-07 22:12:56 -04:00
|
|
|
afterEach(() => {
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
|
2018-08-06 18:30:13 -04:00
|
|
|
it('is valid', () => {
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(React.isValidElement(el)).toBe(true);
|
2018-08-06 18:30:13 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders Tabs', () => {
|
2021-04-05 18:54:28 -04:00
|
|
|
expect(wrapper.find('#table-tabs')).toExist();
|
2018-08-06 18:30:13 -04:00
|
|
|
});
|
|
|
|
|
2021-01-10 20:26:35 -05:00
|
|
|
it('makes an async request', () =>
|
|
|
|
new Promise(done => {
|
2020-06-29 00:37:04 -04:00
|
|
|
wrapper.setState({ activeTabKey: 2 });
|
|
|
|
const syncButton = wrapper.find('.sync-from-source');
|
|
|
|
expect(syncButton).toHaveLength(1);
|
|
|
|
syncButton.simulate('click');
|
2018-10-19 14:41:11 -04:00
|
|
|
|
2020-06-29 00:37:04 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
expect(fetchMock.calls(DATASOURCE_ENDPOINT)).toHaveLength(1);
|
|
|
|
fetchMock.reset();
|
|
|
|
done();
|
|
|
|
}, 0);
|
2021-01-10 20:26:35 -05:00
|
|
|
}));
|
2018-08-06 18:30:13 -04:00
|
|
|
|
2020-08-18 14:47:13 -04:00
|
|
|
it('to add, remove and modify columns accordingly', () => {
|
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
name: 'ds',
|
|
|
|
type: 'DATETIME',
|
|
|
|
nullable: true,
|
|
|
|
default: '',
|
|
|
|
primary_key: false,
|
2021-06-02 20:20:47 -04:00
|
|
|
is_dttm: true,
|
2020-08-18 14:47:13 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'gender',
|
|
|
|
type: 'VARCHAR(32)',
|
|
|
|
nullable: true,
|
|
|
|
default: '',
|
|
|
|
primary_key: false,
|
2021-06-02 20:20:47 -04:00
|
|
|
is_dttm: false,
|
2020-08-18 14:47:13 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'new_column',
|
|
|
|
type: 'VARCHAR(10)',
|
|
|
|
nullable: true,
|
|
|
|
default: '',
|
|
|
|
primary_key: false,
|
2021-06-02 20:20:47 -04:00
|
|
|
is_dttm: false,
|
2020-08-18 14:47:13 -04:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2018-08-06 18:30:13 -04:00
|
|
|
const numCols = props.datasource.columns.length;
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(inst.state.databaseColumns).toHaveLength(numCols);
|
2020-08-18 14:47:13 -04:00
|
|
|
inst.updateColumns(columns);
|
|
|
|
expect(inst.state.databaseColumns).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
{
|
|
|
|
type: 'DATETIME',
|
|
|
|
description: null,
|
|
|
|
filterable: false,
|
|
|
|
verbose_name: null,
|
|
|
|
is_dttm: true,
|
|
|
|
expression: '',
|
|
|
|
groupby: false,
|
|
|
|
column_name: 'ds',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'VARCHAR(32)',
|
|
|
|
description: null,
|
|
|
|
filterable: true,
|
|
|
|
verbose_name: null,
|
|
|
|
is_dttm: false,
|
|
|
|
expression: '',
|
|
|
|
groupby: true,
|
|
|
|
column_name: 'gender',
|
|
|
|
},
|
|
|
|
expect.objectContaining({
|
|
|
|
column_name: 'new_column',
|
|
|
|
type: 'VARCHAR(10)',
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
expect(inst.state.databaseColumns).not.toEqual(
|
|
|
|
expect.arrayContaining([expect.objectContaining({ name: 'name' })]),
|
|
|
|
);
|
2018-08-06 18:30:13 -04:00
|
|
|
});
|
2018-10-29 13:33:03 -04:00
|
|
|
|
|
|
|
it('renders isSqla fields', () => {
|
2019-10-29 18:26:11 -04:00
|
|
|
wrapper.setState({ activeTabKey: 4 });
|
2018-10-29 13:33:03 -04:00
|
|
|
expect(wrapper.state('isSqla')).toBe(true);
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(
|
2020-05-07 17:18:10 -04:00
|
|
|
wrapper.find(Field).find({ fieldKey: 'fetch_values_predicate' }).exists(),
|
2019-11-27 17:02:48 -05:00
|
|
|
).toBe(true);
|
2018-10-29 13:33:03 -04:00
|
|
|
});
|
2020-12-01 20:10:33 -05:00
|
|
|
|
|
|
|
describe('enable edit Source tab', () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
isFeatureEnabledMock = jest
|
|
|
|
.spyOn(featureFlags, 'isFeatureEnabled')
|
2020-12-02 21:13:42 -05:00
|
|
|
.mockImplementation(() => false);
|
2020-12-01 20:10:33 -05:00
|
|
|
wrapper = shallow(el, { context: { store } }).dive();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
isFeatureEnabledMock.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Source Tab: edit mode', () => {
|
|
|
|
wrapper.setState({ activeTabKey: 0, isEditMode: true });
|
|
|
|
const sourceTab = wrapper.find(Tabs.TabPane).first();
|
|
|
|
expect(sourceTab.find(Radio).first().prop('disabled')).toBe(false);
|
|
|
|
|
2021-06-22 18:07:09 -04:00
|
|
|
const icon = wrapper.find(Icons.LockUnlocked);
|
|
|
|
expect(icon).toExist();
|
2020-12-01 20:10:33 -05:00
|
|
|
|
|
|
|
const tableSelector = sourceTab.find(Field).shallow().find(TableSelector);
|
|
|
|
expect(tableSelector.length).toBe(1);
|
|
|
|
expect(tableSelector.prop('readOnly')).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Source Tab: readOnly mode', () => {
|
|
|
|
const sourceTab = wrapper.find(Tabs.TabPane).first();
|
|
|
|
expect(sourceTab.find(Radio).length).toBe(2);
|
|
|
|
expect(sourceTab.find(Radio).first().prop('disabled')).toBe(true);
|
|
|
|
|
2021-06-22 18:07:09 -04:00
|
|
|
const icon = wrapper.find(Icons.LockLocked);
|
|
|
|
expect(icon).toExist();
|
2020-12-01 20:10:33 -05:00
|
|
|
icon.parent().simulate('click');
|
|
|
|
expect(wrapper.state('isEditMode')).toBe(true);
|
|
|
|
|
|
|
|
const tableSelector = sourceTab.find(Field).shallow().find(TableSelector);
|
|
|
|
expect(tableSelector.length).toBe(1);
|
|
|
|
expect(tableSelector.prop('readOnly')).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('disable edit Source tab', () => {
|
|
|
|
// when edit is disabled, show readOnly controls and no padlock
|
|
|
|
isFeatureEnabledMock = jest
|
|
|
|
.spyOn(featureFlags, 'isFeatureEnabled')
|
|
|
|
.mockImplementation(() => true);
|
|
|
|
wrapper = shallow(el, { context: { store } }).dive();
|
|
|
|
wrapper.setState({ activeTabKey: 0 });
|
|
|
|
|
|
|
|
const sourceTab = wrapper.find(Tabs.TabPane).first();
|
|
|
|
expect(sourceTab.find(Radio).length).toBe(2);
|
|
|
|
expect(sourceTab.find(Radio).first().prop('disabled')).toBe(true);
|
|
|
|
|
2021-07-13 17:24:59 -04:00
|
|
|
const icon = sourceTab.find(Icons.LockLocked);
|
2020-12-01 20:10:33 -05:00
|
|
|
expect(icon).toHaveLength(0);
|
|
|
|
|
|
|
|
isFeatureEnabledMock.mockRestore();
|
|
|
|
});
|
2018-08-06 18:30:13 -04:00
|
|
|
});
|
2021-06-07 22:12:56 -04:00
|
|
|
|
|
|
|
describe('DatasourceEditor RTL', () => {
|
|
|
|
it('properly renders the metric information', async () => {
|
|
|
|
render(<DatasourceEditor {...props} />, { useRedux: true });
|
|
|
|
const metricButton = screen.getByTestId('collection-tab-Metrics');
|
|
|
|
userEvent.click(metricButton);
|
|
|
|
const expandToggle = await screen.findAllByLabelText(/toggle expand/i);
|
|
|
|
userEvent.click(expandToggle[0]);
|
|
|
|
const certificationDetails = await screen.findByPlaceholderText(
|
|
|
|
/certification details/i,
|
|
|
|
);
|
|
|
|
expect(certificationDetails.value).toEqual('foo');
|
|
|
|
const warningMarkdown = await await screen.findByPlaceholderText(
|
|
|
|
/certified by/i,
|
|
|
|
);
|
|
|
|
expect(warningMarkdown.value).toEqual('someone');
|
|
|
|
});
|
2021-06-09 17:54:25 -04:00
|
|
|
it('properly updates the metric information', async () => {
|
|
|
|
render(<DatasourceEditor {...props} />, {
|
|
|
|
useRedux: true,
|
|
|
|
});
|
|
|
|
const metricButton = screen.getByTestId('collection-tab-Metrics');
|
|
|
|
userEvent.click(metricButton);
|
|
|
|
const expandToggle = await screen.findAllByLabelText(/toggle expand/i);
|
|
|
|
userEvent.click(expandToggle[1]);
|
|
|
|
const certifiedBy = await screen.findByPlaceholderText(/certified by/i);
|
|
|
|
userEvent.type(certifiedBy, 'I am typing a new name');
|
|
|
|
const certificationDetails = await screen.findByPlaceholderText(
|
|
|
|
/certification details/i,
|
|
|
|
);
|
|
|
|
expect(certifiedBy.value).toEqual('I am typing a new name');
|
|
|
|
userEvent.type(certificationDetails, 'I am typing something new');
|
|
|
|
expect(certificationDetails.value).toEqual('I am typing something new');
|
|
|
|
});
|
2021-06-07 22:12:56 -04:00
|
|
|
});
|