Skip to content
Original file line number Diff line number Diff line change
@@ -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;
Comment thread
yardz marked this conversation as resolved.
Outdated

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(<DatasourceControl {...defaultProps} />);
expect(screen.getByTestId('DatasourceControl')).toBeInTheDocument();
});

test('Should have elements', () => {
render(<DatasourceControl {...defaultProps} />);
expect(screen.getByText('channels')).toBeVisible();
expect(screen.getByTestId('datasource-menu-trigger')).toBeVisible(); // Error
Comment thread
yardz marked this conversation as resolved.
Outdated
});

test('Should open a menu', () => {
render(<DatasourceControl {...defaultProps} />);

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();
Comment thread
yardz marked this conversation as resolved.
Outdated
expect(screen.queryByText('Change dataset')).toBeInTheDocument();
Comment thread
yardz marked this conversation as resolved.
Outdated
expect(screen.queryByText('View in SQL Lab')).toBeInTheDocument();
Comment thread
yardz marked this conversation as resolved.
Outdated
});

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(<DatasourceControl {...defaultProps} />, {
useRedux: true,
});
userEvent.click(screen.getByTestId('datasource-menu-trigger'));

await act(async () => {
userEvent.click(screen.getByText('Change dataset'));
});
Comment thread
yardz marked this conversation as resolved.
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(<DatasourceControl {...defaultProps} />, {
useRedux: true,
});
userEvent.click(screen.getByTestId('datasource-menu-trigger'));

await act(async () => {
userEvent.click(screen.getByText('Edit dataset'));
});
Comment thread
yardz marked this conversation as resolved.

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(<DatasourceControl {...defaultProps} />, {
useRedux: true,
});
userEvent.click(screen.getByTestId('datasource-menu-trigger'));

expect(postFormSpy).toBeCalledTimes(0);

await act(async () => {
userEvent.click(screen.getByText('View in SQL Lab'));
});
Comment thread
yardz marked this conversation as resolved.

expect(postFormSpy).toBeCalledTimes(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class DatasourceControl extends React.PureComponent {
const { health_check_message: healthCheckMessage } = datasource;

return (
<Styles className="DatasourceControl">
<Styles data-test="DatasourceControl" className="DatasourceControl">
<div className="data-container">
<Icon name="dataset-physical" className="dataset-svg" />
{/* Add a tooltip only for long dataset names */}
Expand Down