From 50a534551917290993cbf17758ead36a4c4d0435 Mon Sep 17 00:00:00 2001 From: Bruno Motta Date: Fri, 12 Mar 2021 17:14:18 -0300 Subject: [PATCH 1/8] Tests for DatasourceControl --- .../DatasourceControl.test.tsx | 145 ++++++++++++++++++ .../index.jsx} | 4 +- 2 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx rename superset-frontend/src/explore/components/controls/{DatasourceControl.jsx => DatasourceControl/index.jsx} (98%) diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx new file mode 100644 index 0000000000000..256dcac3d4ae2 --- /dev/null +++ b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx @@ -0,0 +1,145 @@ +/** + * 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. + */ + +import React from 'react'; +import { render, screen, act } from 'spec/helpers/testing-library'; +import userEvent from '@testing-library/user-event'; +import { SupersetClient } from '@superset-ui/core'; +import * as Utils from 'src/explore/exploreUtils'; +import DatasourceControl from '.'; + +const SupersetClientGet = jest.spyOn(SupersetClient, 'get'); + +let defaultProps: any; + +beforeEach(() => { + defaultProps = { + hovered: false, + type: 'DatasourceControl', + label: 'Datasource', + default: null, + description: null, + value: '25__table', + datasource: { + id: 25, + database: { + name: 'examples', + }, + name: 'channels', + type: 'table', + columns: [], + }, + validationErrors: [], + name: 'datasource', + actions: {}, + isEditable: true, + onChange: jest.fn(), + onDatasourceSave: jest.fn(), + }; +}); + +test('Should render', () => { + render(); + expect(screen.getByTestId('DatasourceControl')).toBeInTheDocument(); +}); + +test('Should have elements', () => { + render(); + expect(screen.getByText('channels')).toBeVisible(); + expect(screen.getByTestId('datasource-menu-trigger')).toBeVisible(); // Error +}); + +test('Should open a menu', () => { + render(); + + expect(screen.queryByText('Edit dataset')).not.toBeInTheDocument(); + expect(screen.queryByText('Change dataset')).not.toBeInTheDocument(); + expect(screen.queryByText('View in SQL Lab')).not.toBeInTheDocument(); + + userEvent.click(screen.getByTestId('datasource-menu-trigger')); + + expect(screen.queryByText('Edit dataset')).toBeInTheDocument(); + expect(screen.queryByText('Change dataset')).toBeInTheDocument(); + expect(screen.queryByText('View in SQL Lab')).toBeInTheDocument(); +}); + +test('Click on Change dataset option', async () => { + SupersetClientGet.mockImplementation( + async ({ endpoint }: { endpoint: string }) => { + if (endpoint.includes('_info')) { + return { + json: { permissions: ['can_read', 'can_write'] }, + } as any; + } + return { json: { result: [] } } as any; + }, + ); + + render(, { + useRedux: true, + }); + userEvent.click(screen.getByTestId('datasource-menu-trigger')); + + await act(async () => { + userEvent.click(screen.getByText('Change dataset')); + }); + expect( + screen.getByText( + 'Changing the dataset may break the chart if the chart relies on columns or metadata that does not exist in the target dataset', + ), + ).toBeInTheDocument(); +}); + +test('Click on Edit dataset', async () => { + SupersetClientGet.mockImplementation( + async () => ({ json: { result: [] } } as any), + ); + render(, { + useRedux: true, + }); + userEvent.click(screen.getByTestId('datasource-menu-trigger')); + + await act(async () => { + userEvent.click(screen.getByText('Edit dataset')); + }); + + expect( + screen.getByText( + 'Changing these settings will affect all charts using this dataset, including charts owned by other people.', + ), + ).toBeInTheDocument(); +}); + +test('Click on View in SQL Lab', async () => { + const postFormSpy = jest.spyOn(Utils, 'postForm'); + postFormSpy.mockImplementation(jest.fn()); + + render(, { + useRedux: true, + }); + userEvent.click(screen.getByTestId('datasource-menu-trigger')); + + expect(postFormSpy).toBeCalledTimes(0); + + await act(async () => { + userEvent.click(screen.getByText('View in SQL Lab')); + }); + + expect(postFormSpy).toBeCalledTimes(1); +}); diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl.jsx b/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx similarity index 98% rename from superset-frontend/src/explore/components/controls/DatasourceControl.jsx rename to superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx index 89d293f4925ca..d4497a291f2d2 100644 --- a/superset-frontend/src/explore/components/controls/DatasourceControl.jsx +++ b/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx @@ -39,7 +39,7 @@ const propTypes = { }; const defaultProps = { - onChange: () => {}, + onChange: () => { }, onDatasourceSave: null, value: null, isEditable: true, @@ -179,7 +179,7 @@ class DatasourceControl extends React.PureComponent { const { health_check_message: healthCheckMessage } = datasource; return ( - +
{/* Add a tooltip only for long dataset names */} From aa1732db22d2043632010e7ce1c8174b8f10ea2a Mon Sep 17 00:00:00 2001 From: Bruno Motta Date: Fri, 12 Mar 2021 18:35:26 -0300 Subject: [PATCH 2/8] fix: lint error --- .../src/explore/components/controls/DatasourceControl/index.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx b/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx index d4497a291f2d2..7b88b72d7bce8 100644 --- a/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx +++ b/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx @@ -39,7 +39,7 @@ const propTypes = { }; const defaultProps = { - onChange: () => { }, + onChange: () => {}, onDatasourceSave: null, value: null, isEditable: true, From a69b3e6adf6d06a5d9a7b87a88cee014d0d3174c Mon Sep 17 00:00:00 2001 From: Bruno Motta Date: Thu, 18 Mar 2021 12:19:17 -0300 Subject: [PATCH 3/8] Update superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com> --- .../controls/DatasourceControl/DatasourceControl.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx index 256dcac3d4ae2..d1f2ea1df2aa7 100644 --- a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx +++ b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx @@ -76,7 +76,7 @@ test('Should open a menu', () => { expect(screen.queryByText('Edit dataset')).toBeInTheDocument(); expect(screen.queryByText('Change dataset')).toBeInTheDocument(); - expect(screen.queryByText('View in SQL Lab')).toBeInTheDocument(); + expect(screen.getByText('View in SQL Lab')).toBeInTheDocument(); }); test('Click on Change dataset option', async () => { From d9a4e6d39df694a0cd7e7d012a9fa74f8b7b133d Mon Sep 17 00:00:00 2001 From: Bruno Motta Date: Thu, 18 Mar 2021 12:20:03 -0300 Subject: [PATCH 4/8] Update superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com> --- .../controls/DatasourceControl/DatasourceControl.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx index d1f2ea1df2aa7..600d70113da56 100644 --- a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx +++ b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx @@ -74,7 +74,7 @@ test('Should open a menu', () => { userEvent.click(screen.getByTestId('datasource-menu-trigger')); - expect(screen.queryByText('Edit dataset')).toBeInTheDocument(); + expect(screen.getByText('Edit dataset')).toBeInTheDocument(); expect(screen.queryByText('Change dataset')).toBeInTheDocument(); expect(screen.getByText('View in SQL Lab')).toBeInTheDocument(); }); From 4cba16fcc4940bcd7e9a891a6a8ebbe2b8cb41a0 Mon Sep 17 00:00:00 2001 From: Bruno Motta Date: Thu, 18 Mar 2021 12:20:13 -0300 Subject: [PATCH 5/8] Update superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com> --- .../controls/DatasourceControl/DatasourceControl.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx index 600d70113da56..de332e1091522 100644 --- a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx +++ b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx @@ -75,7 +75,7 @@ test('Should open a menu', () => { userEvent.click(screen.getByTestId('datasource-menu-trigger')); expect(screen.getByText('Edit dataset')).toBeInTheDocument(); - expect(screen.queryByText('Change dataset')).toBeInTheDocument(); + expect(screen.getByText('Change dataset')).toBeInTheDocument(); expect(screen.getByText('View in SQL Lab')).toBeInTheDocument(); }); From afee3edf5d4d8f3020ac1e8b77bb31a06bc60102 Mon Sep 17 00:00:00 2001 From: Bruno Motta Date: Mon, 22 Mar 2021 12:44:36 -0300 Subject: [PATCH 6/8] Remove error comment --- .../controls/DatasourceControl/DatasourceControl.test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx index de332e1091522..a7400812469b1 100644 --- a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx +++ b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx @@ -56,13 +56,13 @@ beforeEach(() => { test('Should render', () => { render(); - expect(screen.getByTestId('DatasourceControl')).toBeInTheDocument(); + expect(screen.getByTestId('DatasourceControl')).toBeVisible(); }); test('Should have elements', () => { render(); expect(screen.getByText('channels')).toBeVisible(); - expect(screen.getByTestId('datasource-menu-trigger')).toBeVisible(); // Error + expect(screen.getByTestId('datasource-menu-trigger')).toBeVisible(); }); test('Should open a menu', () => { From 95dda98107a7ca9c51d8de621f447f509ab52bd7 Mon Sep 17 00:00:00 2001 From: Bruno Motta Date: Fri, 26 Mar 2021 12:26:18 -0300 Subject: [PATCH 7/8] replace component data-test --- .../controls/DatasourceControl/DatasourceControl.test.tsx | 2 +- .../src/explore/components/controls/DatasourceControl/index.jsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx index a7400812469b1..38245a75e008f 100644 --- a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx +++ b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx @@ -56,7 +56,7 @@ beforeEach(() => { test('Should render', () => { render(); - expect(screen.getByTestId('DatasourceControl')).toBeVisible(); + expect(screen.getByTestId('datasource-control')).toBeVisible(); }); test('Should have elements', () => { diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx b/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx index 45c7d39aadeb1..b181e596d7eb5 100644 --- a/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx +++ b/superset-frontend/src/explore/components/controls/DatasourceControl/index.jsx @@ -187,7 +187,7 @@ class DatasourceControl extends React.PureComponent { } return ( - +
{/* Add a tooltip only for long dataset names */} From 31433ed836405e95e1099986a152debd04bb7218 Mon Sep 17 00:00:00 2001 From: Bruno Motta Date: Tue, 30 Mar 2021 11:35:06 -0300 Subject: [PATCH 8/8] factory to props --- .../DatasourceControl.test.tsx | 64 ++++++++++--------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx index 38245a75e008f..744b929a626fa 100644 --- a/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx +++ b/superset-frontend/src/explore/components/controls/DatasourceControl/DatasourceControl.test.tsx @@ -26,47 +26,46 @@ import DatasourceControl from '.'; const SupersetClientGet = jest.spyOn(SupersetClient, 'get'); -let defaultProps: any; - -beforeEach(() => { - defaultProps = { - hovered: false, - type: 'DatasourceControl', - label: 'Datasource', - default: null, - description: null, - value: '25__table', - datasource: { - id: 25, - database: { - name: 'examples', - }, - name: 'channels', - type: 'table', - columns: [], +const createProps = () => ({ + hovered: false, + type: 'DatasourceControl', + label: 'Datasource', + default: null, + description: null, + value: '25__table', + datasource: { + id: 25, + database: { + name: 'examples', }, - validationErrors: [], - name: 'datasource', - actions: {}, - isEditable: true, - onChange: jest.fn(), - onDatasourceSave: jest.fn(), - }; + name: 'channels', + type: 'table', + columns: [], + }, + validationErrors: [], + name: 'datasource', + actions: {}, + isEditable: true, + onChange: jest.fn(), + onDatasourceSave: jest.fn(), }); test('Should render', () => { - render(); + const props = createProps(); + render(); expect(screen.getByTestId('datasource-control')).toBeVisible(); }); test('Should have elements', () => { - render(); + const props = createProps(); + render(); expect(screen.getByText('channels')).toBeVisible(); expect(screen.getByTestId('datasource-menu-trigger')).toBeVisible(); }); test('Should open a menu', () => { - render(); + const props = createProps(); + render(); expect(screen.queryByText('Edit dataset')).not.toBeInTheDocument(); expect(screen.queryByText('Change dataset')).not.toBeInTheDocument(); @@ -80,6 +79,7 @@ test('Should open a menu', () => { }); test('Click on Change dataset option', async () => { + const props = createProps(); SupersetClientGet.mockImplementation( async ({ endpoint }: { endpoint: string }) => { if (endpoint.includes('_info')) { @@ -91,7 +91,7 @@ test('Click on Change dataset option', async () => { }, ); - render(, { + render(, { useRedux: true, }); userEvent.click(screen.getByTestId('datasource-menu-trigger')); @@ -107,10 +107,11 @@ test('Click on Change dataset option', async () => { }); test('Click on Edit dataset', async () => { + const props = createProps(); SupersetClientGet.mockImplementation( async () => ({ json: { result: [] } } as any), ); - render(, { + render(, { useRedux: true, }); userEvent.click(screen.getByTestId('datasource-menu-trigger')); @@ -127,10 +128,11 @@ test('Click on Edit dataset', async () => { }); test('Click on View in SQL Lab', async () => { + const props = createProps(); const postFormSpy = jest.spyOn(Utils, 'postForm'); postFormSpy.mockImplementation(jest.fn()); - render(, { + render(, { useRedux: true, }); userEvent.click(screen.getByTestId('datasource-menu-trigger'));