mirror of
https://github.com/apache/superset.git
synced 2026-05-08 01:15:46 +00:00
799 lines
23 KiB
TypeScript
799 lines
23 KiB
TypeScript
/**
|
|
* 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 type React from 'react';
|
|
import { Route } from 'react-router-dom';
|
|
import fetchMock from 'fetch-mock';
|
|
import { DatasourceType, JsonObject, SupersetClient } from '@superset-ui/core';
|
|
import {
|
|
render,
|
|
screen,
|
|
act,
|
|
userEvent,
|
|
waitFor,
|
|
} from 'spec/helpers/testing-library';
|
|
import { fallbackExploreInitialData } from 'src/explore/fixtures';
|
|
import type { ColumnObject } from 'src/features/datasets/types';
|
|
import DatasourceControl from '.';
|
|
|
|
const SupersetClientGet = jest.spyOn(SupersetClient, 'get');
|
|
|
|
let originalLocation: Location;
|
|
|
|
beforeEach(() => {
|
|
originalLocation = window.location;
|
|
});
|
|
|
|
afterEach(() => {
|
|
window.location = originalLocation;
|
|
fetchMock.clearHistory().removeRoutes();
|
|
jest.clearAllMocks(); // Clears mock history but keeps spy in place
|
|
});
|
|
|
|
interface TestDatasource {
|
|
id?: number;
|
|
name: string;
|
|
datasource_name?: string;
|
|
database: {
|
|
id: number;
|
|
database_name: string;
|
|
name?: string;
|
|
backend?: string;
|
|
};
|
|
columns?: Partial<ColumnObject>[];
|
|
type?: DatasourceType;
|
|
main_dttm_col?: string | null;
|
|
owners?: Array<{
|
|
first_name: string;
|
|
last_name: string;
|
|
id: number;
|
|
username?: string;
|
|
}>;
|
|
sql?: string;
|
|
metrics?: Array<{ id: number; metric_name: string }>;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
const mockDatasource: TestDatasource = {
|
|
id: 25,
|
|
database: {
|
|
id: 1,
|
|
database_name: 'examples',
|
|
name: 'examples',
|
|
},
|
|
name: 'channels',
|
|
datasource_name: 'channels',
|
|
type: DatasourceType.Table,
|
|
columns: [],
|
|
owners: [{ first_name: 'john', last_name: 'doe', id: 1, username: 'jd' }],
|
|
sql: 'SELECT * FROM mock_datasource_sql',
|
|
};
|
|
|
|
// Use type assertion for test props since the component is wrapped with withTheme
|
|
// The withTheme HOC makes the props type complex, so we cast through unknown to bypass type check
|
|
type DatasourceControlComponentProps = React.ComponentProps<
|
|
typeof DatasourceControl
|
|
>;
|
|
const createProps = (
|
|
overrides: JsonObject = {},
|
|
): DatasourceControlComponentProps =>
|
|
({
|
|
hovered: false,
|
|
type: 'DatasourceControl',
|
|
label: 'Datasource',
|
|
default: null,
|
|
description: null,
|
|
value: '25__table',
|
|
form_data: {},
|
|
datasource: mockDatasource,
|
|
validationErrors: [],
|
|
name: 'datasource',
|
|
actions: {
|
|
changeDatasource: jest.fn(),
|
|
setControlValue: jest.fn(),
|
|
},
|
|
isEditable: true,
|
|
user: {
|
|
createdOn: '2021-04-27T18:12:38.952304',
|
|
email: 'admin',
|
|
firstName: 'admin',
|
|
isActive: true,
|
|
lastName: 'admin',
|
|
permissions: {},
|
|
roles: { Admin: Array(173) },
|
|
userId: 1,
|
|
username: 'admin',
|
|
},
|
|
onChange: jest.fn(),
|
|
onDatasourceSave: jest.fn(),
|
|
...overrides,
|
|
}) as unknown as DatasourceControlComponentProps;
|
|
|
|
const getDbWithQuery = 'glob:*/api/v1/database/?q=*';
|
|
const getDatasetWithAll = 'glob:*/api/v1/dataset/*';
|
|
const putDatasetWithAll = 'glob:*/api/v1/dataset/*';
|
|
|
|
async function openAndSaveChanges(
|
|
datasource: TestDatasource | Record<string, unknown>,
|
|
) {
|
|
fetchMock.removeRoute(getDbWithQuery);
|
|
fetchMock.get(getDbWithQuery, { result: [] }, { name: getDbWithQuery });
|
|
|
|
fetchMock.removeRoute('put' + putDatasetWithAll);
|
|
fetchMock.put(putDatasetWithAll, {}, { name: 'put' + putDatasetWithAll });
|
|
|
|
fetchMock.removeRoute('get' + getDatasetWithAll);
|
|
fetchMock.get(
|
|
getDatasetWithAll,
|
|
{ result: datasource },
|
|
{
|
|
name: 'get' + getDatasetWithAll,
|
|
},
|
|
);
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
await userEvent.click(await screen.findByTestId('edit-dataset'));
|
|
await userEvent.click(await screen.findByTestId('datasource-modal-save'));
|
|
await userEvent.click(await screen.findByText('OK'));
|
|
}
|
|
|
|
test('Should render', async () => {
|
|
const props = createProps();
|
|
render(<DatasourceControl {...props} />, { useRouter: true });
|
|
expect(await screen.findByTestId('datasource-control')).toBeVisible();
|
|
});
|
|
|
|
test('Should have elements', async () => {
|
|
const props = createProps();
|
|
render(<DatasourceControl {...props} />, { useRouter: true });
|
|
expect(await screen.findByText('channels')).toBeVisible();
|
|
expect(screen.getByTestId('datasource-menu-trigger')).toBeVisible();
|
|
});
|
|
|
|
test('Should open a menu', async () => {
|
|
const props = createProps();
|
|
render(<DatasourceControl {...props} />, { useRouter: true });
|
|
|
|
expect(screen.queryByText('Edit dataset')).not.toBeInTheDocument();
|
|
expect(screen.queryByText('Swap dataset')).not.toBeInTheDocument();
|
|
expect(screen.queryByText('View in SQL Lab')).not.toBeInTheDocument();
|
|
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
|
|
expect(await screen.findByText('Edit dataset')).toBeInTheDocument();
|
|
expect(screen.getByText('Swap dataset')).toBeInTheDocument();
|
|
expect(screen.getByText('View in SQL Lab')).toBeInTheDocument();
|
|
});
|
|
|
|
test('Should not show SQL Lab for non sql_lab role', async () => {
|
|
const props = createProps({
|
|
user: {
|
|
createdOn: '2021-04-27T18:12:38.952304',
|
|
email: 'gamma',
|
|
firstName: 'gamma',
|
|
isActive: true,
|
|
lastName: 'gamma',
|
|
permissions: {},
|
|
roles: { Gamma: [] },
|
|
userId: 2,
|
|
username: 'gamma',
|
|
},
|
|
});
|
|
render(<DatasourceControl {...props} />, { useRouter: true });
|
|
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
|
|
expect(await screen.findByText('Edit dataset')).toBeInTheDocument();
|
|
expect(screen.getByText('Swap dataset')).toBeInTheDocument();
|
|
expect(screen.queryByText('View in SQL Lab')).not.toBeInTheDocument();
|
|
});
|
|
|
|
test('Should show SQL Lab for sql_lab role', async () => {
|
|
const props = createProps({
|
|
user: {
|
|
createdOn: '2021-04-27T18:12:38.952304',
|
|
email: 'sql',
|
|
firstName: 'sql',
|
|
isActive: true,
|
|
lastName: 'sql',
|
|
permissions: {},
|
|
roles: { Gamma: [], sql_lab: [['menu_access', 'SQL Lab']] },
|
|
userId: 2,
|
|
username: 'sql',
|
|
},
|
|
});
|
|
render(<DatasourceControl {...props} />, { useRouter: true });
|
|
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
|
|
expect(await screen.findByText('Edit dataset')).toBeInTheDocument();
|
|
expect(screen.getByText('Swap dataset')).toBeInTheDocument();
|
|
expect(screen.getByText('View in SQL Lab')).toBeInTheDocument();
|
|
});
|
|
|
|
test('Click on Swap dataset option', async () => {
|
|
const props = createProps();
|
|
SupersetClientGet.mockImplementationOnce(
|
|
async ({ endpoint }: { endpoint: string }) => {
|
|
if (endpoint.includes('_info')) {
|
|
return {
|
|
json: { permissions: ['can_read', 'can_write'] },
|
|
} as any;
|
|
}
|
|
return { json: { result: [] } } as any;
|
|
},
|
|
);
|
|
|
|
render(<DatasourceControl {...props} />, {
|
|
useRedux: true,
|
|
useRouter: true,
|
|
});
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
|
|
await act(async () => {
|
|
await userEvent.click(screen.getByText('Swap 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 () => {
|
|
const props = createProps();
|
|
SupersetClientGet.mockImplementationOnce(
|
|
async () => ({ json: { result: [] } }) as any,
|
|
);
|
|
fetchMock.removeRoute(getDbWithQuery);
|
|
fetchMock.get(getDbWithQuery, { result: [] }, { name: getDbWithQuery });
|
|
render(<DatasourceControl {...props} />, {
|
|
useRedux: true,
|
|
useRouter: 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('Edit dataset should be disabled when user is not admin', async () => {
|
|
const props = createProps();
|
|
props.user.roles = {};
|
|
props.datasource.owners = [];
|
|
SupersetClientGet.mockImplementationOnce(
|
|
async () => ({ json: { result: [] } }) as any,
|
|
);
|
|
|
|
render(<DatasourceControl {...props} />, {
|
|
useRedux: true,
|
|
useRouter: true,
|
|
});
|
|
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
|
|
expect(await screen.findByTestId('edit-dataset')).toHaveAttribute(
|
|
'aria-disabled',
|
|
'true',
|
|
);
|
|
});
|
|
|
|
test('Click on View in SQL Lab', async () => {
|
|
const props = createProps();
|
|
|
|
const { queryByTestId, getByTestId } = render(
|
|
<>
|
|
<Route
|
|
path="/sqllab"
|
|
render={({ location }) => (
|
|
<div data-test="mock-sqllab-route">
|
|
{JSON.stringify(location.state)}
|
|
</div>
|
|
)}
|
|
/>
|
|
<DatasourceControl {...props} />
|
|
</>,
|
|
{
|
|
useRedux: true,
|
|
useRouter: true,
|
|
},
|
|
);
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
|
|
expect(queryByTestId('mock-sqllab-route')).not.toBeInTheDocument();
|
|
|
|
await act(async () => {
|
|
await userEvent.click(screen.getByText('View in SQL Lab'));
|
|
});
|
|
|
|
expect(getByTestId('mock-sqllab-route')).toBeInTheDocument();
|
|
expect(JSON.parse(`${getByTestId('mock-sqllab-route').textContent}`)).toEqual(
|
|
{
|
|
requestedQuery: {
|
|
datasourceKey: `${mockDatasource.id}__${mockDatasource.type}`,
|
|
sql: mockDatasource.sql,
|
|
},
|
|
},
|
|
);
|
|
});
|
|
|
|
test('Should open a different menu when datasource=query', async () => {
|
|
const props = createProps();
|
|
const queryProps = {
|
|
...props,
|
|
datasource: {
|
|
...props.datasource,
|
|
type: DatasourceType.Query,
|
|
},
|
|
};
|
|
render(<DatasourceControl {...queryProps} />, { useRouter: true });
|
|
|
|
expect(screen.queryByText('Query preview')).not.toBeInTheDocument();
|
|
expect(screen.queryByText('View in SQL Lab')).not.toBeInTheDocument();
|
|
expect(screen.queryByText('Save as dataset')).not.toBeInTheDocument();
|
|
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
|
|
expect(await screen.findByText('Query preview')).toBeInTheDocument();
|
|
expect(screen.getByText('View in SQL Lab')).toBeInTheDocument();
|
|
expect(screen.getByText('Save as dataset')).toBeInTheDocument();
|
|
});
|
|
|
|
test('Click on Save as dataset', async () => {
|
|
const props = createProps();
|
|
const queryProps = {
|
|
...props,
|
|
datasource: {
|
|
...props.datasource,
|
|
type: DatasourceType.Query,
|
|
},
|
|
};
|
|
|
|
render(<DatasourceControl {...queryProps} />, {
|
|
useRedux: true,
|
|
useRouter: true,
|
|
});
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
expect(
|
|
screen.queryByRole('button', { name: /save/i }),
|
|
).not.toBeInTheDocument();
|
|
expect(
|
|
screen.queryByRole('button', { name: /close/i }),
|
|
).not.toBeInTheDocument();
|
|
expect(
|
|
screen.queryByText(/select or type dataset name/i),
|
|
).not.toBeInTheDocument();
|
|
await userEvent.click(screen.getByText('Save as dataset'));
|
|
|
|
// Renders a save dataset modal
|
|
const saveRadioBtn = await screen.findByRole('radio', {
|
|
name: /save as new/i,
|
|
});
|
|
const overwriteRadioBtn = screen.getByRole('radio', {
|
|
name: /overwrite existing/i,
|
|
});
|
|
const dropdownField = screen.getByText(/select or type dataset name/i);
|
|
expect(saveRadioBtn).toBeInTheDocument();
|
|
expect(overwriteRadioBtn).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: /save/i })).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: /close/i })).toBeInTheDocument();
|
|
expect(dropdownField).toBeInTheDocument();
|
|
});
|
|
|
|
test('should set the default temporal column', async () => {
|
|
const props = createProps();
|
|
const overrideProps = {
|
|
...props,
|
|
form_data: {
|
|
granularity_sqla: 'test-col',
|
|
},
|
|
datasource: {
|
|
...props.datasource,
|
|
main_dttm_col: 'test-default',
|
|
columns: [
|
|
{
|
|
column_name: 'test-col',
|
|
is_dttm: false,
|
|
},
|
|
{
|
|
column_name: 'test-default',
|
|
is_dttm: true,
|
|
},
|
|
],
|
|
},
|
|
};
|
|
render(<DatasourceControl {...props} {...overrideProps} />, {
|
|
useRedux: true,
|
|
useRouter: true,
|
|
});
|
|
|
|
await openAndSaveChanges(overrideProps.datasource);
|
|
await waitFor(() => {
|
|
expect(props.actions.setControlValue).toHaveBeenCalledWith(
|
|
'granularity_sqla',
|
|
'test-default',
|
|
);
|
|
});
|
|
});
|
|
|
|
test('should set the first available temporal column', async () => {
|
|
const props = createProps();
|
|
const overrideProps = {
|
|
...props,
|
|
form_data: {
|
|
granularity_sqla: 'test-col',
|
|
},
|
|
datasource: {
|
|
...props.datasource,
|
|
main_dttm_col: null,
|
|
columns: [
|
|
{
|
|
column_name: 'test-col',
|
|
is_dttm: false,
|
|
},
|
|
{
|
|
column_name: 'test-first',
|
|
is_dttm: true,
|
|
},
|
|
],
|
|
},
|
|
};
|
|
render(<DatasourceControl {...props} {...overrideProps} />, {
|
|
useRedux: true,
|
|
useRouter: true,
|
|
});
|
|
|
|
await openAndSaveChanges(overrideProps.datasource);
|
|
await waitFor(() => {
|
|
expect(props.actions.setControlValue).toHaveBeenCalledWith(
|
|
'granularity_sqla',
|
|
'test-first',
|
|
);
|
|
});
|
|
});
|
|
|
|
test('should not set the temporal column', async () => {
|
|
const props = createProps();
|
|
const overrideProps = {
|
|
...props,
|
|
form_data: {
|
|
granularity_sqla: undefined,
|
|
},
|
|
datasource: {
|
|
...props.datasource,
|
|
main_dttm_col: undefined,
|
|
columns: [
|
|
{
|
|
column_name: 'test-col',
|
|
is_dttm: false,
|
|
},
|
|
{
|
|
column_name: 'test-col-2',
|
|
is_dttm: false,
|
|
},
|
|
],
|
|
},
|
|
};
|
|
render(<DatasourceControl {...props} {...overrideProps} />, {
|
|
useRedux: true,
|
|
useRouter: true,
|
|
});
|
|
|
|
await openAndSaveChanges(overrideProps.datasource);
|
|
await waitFor(() => {
|
|
expect(props.actions.setControlValue).toHaveBeenCalledWith(
|
|
'granularity_sqla',
|
|
null,
|
|
);
|
|
});
|
|
});
|
|
|
|
test('should show missing params state', () => {
|
|
const props = createProps({ datasource: fallbackExploreInitialData.dataset });
|
|
render(<DatasourceControl {...props} />, { useRedux: true, useRouter: true });
|
|
expect(screen.getByText(/missing dataset/i)).toBeVisible();
|
|
expect(screen.getByText(/missing url parameters/i)).toBeVisible();
|
|
expect(
|
|
screen.getByText(
|
|
/the url is missing the dataset_id or slice_id parameters/i,
|
|
),
|
|
).toBeVisible();
|
|
});
|
|
|
|
test('should show missing dataset state', () => {
|
|
// @ts-expect-error - overriding window.location for test
|
|
delete window.location;
|
|
// @ts-expect-error - overriding window.location for test
|
|
window.location = { search: '?slice_id=152' };
|
|
const props = createProps({ datasource: fallbackExploreInitialData.dataset });
|
|
render(<DatasourceControl {...props} />, { useRedux: true, useRouter: true });
|
|
expect(screen.getAllByText(/missing dataset/i)).toHaveLength(2);
|
|
expect(
|
|
screen.getByText(
|
|
/the dataset linked to this chart may have been deleted\./i,
|
|
),
|
|
).toBeVisible();
|
|
});
|
|
|
|
test('should show forbidden dataset state', () => {
|
|
// @ts-expect-error - overriding window.location for test
|
|
delete window.location;
|
|
// @ts-expect-error - overriding window.location for test
|
|
window.location = { search: '?slice_id=152' };
|
|
const error = {
|
|
error_type: 'TABLE_SECURITY_ACCESS_ERROR',
|
|
statusText: 'FORBIDDEN',
|
|
message: 'You do not have access to the following tables: blocked_table',
|
|
extra: {
|
|
datasource: 152,
|
|
datasource_name: 'forbidden dataset',
|
|
},
|
|
};
|
|
const props = createProps({
|
|
datasource: {
|
|
...fallbackExploreInitialData.dataset,
|
|
extra: {
|
|
error,
|
|
},
|
|
},
|
|
});
|
|
render(<DatasourceControl {...props} />, { useRedux: true, useRouter: true });
|
|
expect(screen.getByText(error.message)).toBeInTheDocument();
|
|
expect(screen.getByText(error.statusText)).toBeVisible();
|
|
});
|
|
|
|
test('should allow creating new metrics in dataset editor', async () => {
|
|
const newMetricName = `test_metric_${Date.now()}`;
|
|
const mockDatasourceWithMetrics = {
|
|
...mockDatasource,
|
|
metrics: [],
|
|
};
|
|
|
|
const props = createProps({
|
|
datasource: mockDatasourceWithMetrics,
|
|
});
|
|
|
|
// Mock API calls for dataset editor
|
|
fetchMock.get(getDbWithQuery, { response: { result: [] } });
|
|
|
|
fetchMock.get(getDatasetWithAll, { result: mockDatasourceWithMetrics });
|
|
|
|
fetchMock.put(putDatasetWithAll, {
|
|
result: {
|
|
...mockDatasourceWithMetrics,
|
|
metrics: [{ id: 1, metric_name: newMetricName }],
|
|
},
|
|
});
|
|
|
|
SupersetClientGet.mockImplementationOnce(
|
|
async () => ({ json: { result: [] } }) as any,
|
|
);
|
|
|
|
render(<DatasourceControl {...props} />, {
|
|
useRedux: true,
|
|
useRouter: true,
|
|
});
|
|
|
|
// Open datasource menu and click edit dataset
|
|
userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
userEvent.click(await screen.findByTestId('edit-dataset'));
|
|
|
|
// Wait for modal to appear and navigate to Metrics tab
|
|
await waitFor(() => {
|
|
expect(screen.getByText('Metrics')).toBeInTheDocument();
|
|
});
|
|
|
|
userEvent.click(screen.getByText('Metrics'));
|
|
|
|
// Click add new metric button
|
|
const addButton = await screen.findByTestId('crud-add-table-item');
|
|
userEvent.click(addButton);
|
|
|
|
// Find and fill in the metric name
|
|
const nameInput = await screen.findByTestId('textarea-editable-title-input');
|
|
userEvent.clear(nameInput);
|
|
userEvent.type(nameInput, newMetricName);
|
|
|
|
// Save the modal
|
|
userEvent.click(screen.getByTestId('datasource-modal-save'));
|
|
|
|
// Confirm the save
|
|
const okButton = await screen.findByText('OK');
|
|
userEvent.click(okButton);
|
|
|
|
// Verify the onDatasourceSave callback was called
|
|
await waitFor(() => {
|
|
expect(props.onDatasourceSave).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
test('should allow deleting metrics in dataset editor', async () => {
|
|
const existingMetricName = 'existing_metric';
|
|
const mockDatasourceWithMetrics = {
|
|
...mockDatasource,
|
|
metrics: [{ id: 1, metric_name: existingMetricName }],
|
|
};
|
|
|
|
const props = createProps({
|
|
datasource: mockDatasourceWithMetrics,
|
|
});
|
|
|
|
// Mock API calls
|
|
fetchMock.get('glob:*/api/v1/database/?q=*', { result: [] });
|
|
|
|
fetchMock.get('glob:*/api/v1/dataset/*', {
|
|
result: mockDatasourceWithMetrics,
|
|
});
|
|
|
|
fetchMock.put('glob:*/api/v1/dataset/*', {
|
|
result: { ...mockDatasourceWithMetrics, metrics: [] },
|
|
});
|
|
|
|
SupersetClientGet.mockImplementationOnce(
|
|
async () => ({ json: { result: [] } }) as any,
|
|
);
|
|
|
|
render(<DatasourceControl {...props} />, {
|
|
useRedux: true,
|
|
useRouter: true,
|
|
});
|
|
|
|
// Open edit dataset modal
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
await userEvent.click(await screen.findByTestId('edit-dataset'));
|
|
|
|
// Navigate to Metrics tab
|
|
await waitFor(() => {
|
|
expect(screen.getByText('Metrics')).toBeInTheDocument();
|
|
});
|
|
await userEvent.click(screen.getByText('Metrics'));
|
|
|
|
// Find existing metric and delete it
|
|
const metricRow = (await screen.findByText(existingMetricName)).closest('tr');
|
|
expect(metricRow).toBeInTheDocument();
|
|
|
|
const deleteButton = metricRow?.querySelector(
|
|
'[data-test="crud-delete-icon"]',
|
|
);
|
|
expect(deleteButton).toBeInTheDocument();
|
|
await userEvent.click(deleteButton!);
|
|
|
|
// Save the changes
|
|
await userEvent.click(screen.getByTestId('datasource-modal-save'));
|
|
|
|
// Confirm the save
|
|
const okButton = await screen.findByText('OK');
|
|
await userEvent.click(okButton);
|
|
|
|
// Verify the onDatasourceSave callback was called
|
|
await waitFor(() => {
|
|
expect(props.onDatasourceSave).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
test('should handle metric save confirmation modal', async () => {
|
|
const props = createProps();
|
|
|
|
// Mock API calls for dataset editor
|
|
fetchMock.get('glob:*/api/v1/database/?q=*', { result: [] });
|
|
|
|
fetchMock.get('glob:*/api/v1/dataset/*', { result: mockDatasource });
|
|
|
|
fetchMock.put('glob:*/api/v1/dataset/*', { result: mockDatasource });
|
|
|
|
SupersetClientGet.mockImplementationOnce(
|
|
async () => ({ json: { result: [] } }) as any,
|
|
);
|
|
|
|
render(<DatasourceControl {...props} />, {
|
|
useRedux: true,
|
|
useRouter: true,
|
|
});
|
|
|
|
// Open edit dataset modal
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
await userEvent.click(await screen.findByTestId('edit-dataset'));
|
|
|
|
// Save without making changes
|
|
const saveButton = await screen.findByTestId('datasource-modal-save');
|
|
await userEvent.click(saveButton);
|
|
|
|
// Verify confirmation modal appears
|
|
await waitFor(() => {
|
|
expect(screen.getByText('OK')).toBeInTheDocument();
|
|
});
|
|
|
|
// Click OK to confirm
|
|
await userEvent.click(screen.getByText('OK'));
|
|
|
|
// Verify the save was processed
|
|
await waitFor(() => {
|
|
expect(props.onDatasourceSave).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
test('should verify real DatasourceControl callback fires on save', async () => {
|
|
// This test verifies that the REAL DatasourceControl component calls onDatasourceSave
|
|
// This is simpler than the full metric creation flow but tests the key integration
|
|
|
|
const mockOnDatasourceSave = jest.fn();
|
|
const props = createProps({
|
|
datasource: mockDatasource,
|
|
onDatasourceSave: mockOnDatasourceSave,
|
|
});
|
|
|
|
// Mock API calls with the same datasource (no changes needed for this test)
|
|
fetchMock.get('glob:*/api/v1/database/?q=*', { result: [] });
|
|
|
|
fetchMock.get('glob:*/api/v1/dataset/*', { result: mockDatasource });
|
|
|
|
fetchMock.put('glob:*/api/v1/dataset/*', { result: mockDatasource });
|
|
|
|
SupersetClientGet.mockImplementationOnce(
|
|
async () => ({ json: { result: [] } }) as any,
|
|
);
|
|
|
|
// Render the REAL DatasourceControl component
|
|
render(<DatasourceControl {...props} />, {
|
|
useRedux: true,
|
|
useRouter: true,
|
|
});
|
|
|
|
// Verify the real component rendered
|
|
expect(screen.getByTestId('datasource-control')).toBeInTheDocument();
|
|
|
|
// Open dataset editor
|
|
await userEvent.click(screen.getByTestId('datasource-menu-trigger'));
|
|
await userEvent.click(await screen.findByTestId('edit-dataset'));
|
|
|
|
// Wait for modal to open
|
|
await waitFor(() => {
|
|
expect(screen.getByText('Columns')).toBeInTheDocument();
|
|
});
|
|
|
|
// Save without making changes (this should still trigger the callback)
|
|
await userEvent.click(screen.getByTestId('datasource-modal-save'));
|
|
const okButton = await screen.findByText('OK');
|
|
await userEvent.click(okButton);
|
|
|
|
// Verify the REAL component called the callback
|
|
// This tests that the integration point works (regardless of what data is passed)
|
|
await waitFor(() => {
|
|
expect(mockOnDatasourceSave).toHaveBeenCalled();
|
|
});
|
|
|
|
// Verify it was called with a datasource object
|
|
expect(mockOnDatasourceSave).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
id: expect.any(Number),
|
|
name: expect.any(String),
|
|
}),
|
|
);
|
|
});
|
|
|
|
// Note: Cross-component integration test removed due to complex Redux/user context setup
|
|
// The existing callback tests provide sufficient coverage for metric creation workflows
|
|
// Future enhancement could add MetricsControl integration when test infrastructure supports it
|