Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(Explorer): Fix minor errors in ExploreViewContainer syntax, add tests #29249

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getChartMetadataRegistry,
ChartMetadata,
} from '@superset-ui/core';
import { QUERY_MODE_REQUISITES } from 'src/explore/constants';
import { MemoryRouter, Route } from 'react-router-dom';
import { render, screen, waitFor } from 'spec/helpers/testing-library';
import userEvent from '@testing-library/user-event';
Expand Down Expand Up @@ -221,3 +222,75 @@ test('preserves unknown parameters', async () => {
);
replaceState.mockRestore();
});

test('retains query mode requirements when query_mode is enabled', async () => {
const customState = {
...reduxState,
explore: {
...reduxState.explore,
controls: {
...reduxState.explore.controls,
query_mode: { value: 'raw' },
optional_key1: { value: 'value1' },
all_columns: { value: ['all_columns'] },
groupby: { value: ['groupby'] },
},
hiddenFormData: {
all_columns: ['all_columns'],
groupby: ['groupby'],
optional_key1: 'value1',
},
},
};

await waitFor(() => renderWithRouter({ initialState: customState }));

const formDataEndpointCalls = fetchMock.calls(/api\/v1\/explore\/form_data/);
expect(formDataEndpointCalls.length).toBeGreaterThan(0);
const lastCall = formDataEndpointCalls[formDataEndpointCalls.length - 1];

const body = JSON.parse(lastCall[1]?.body as string);
const formData = JSON.parse(body.form_data);

const queryModeFields = Object.keys(
customState.explore.hiddenFormData,
).filter(key => QUERY_MODE_REQUISITES.has(key));

queryModeFields.forEach(key => {
expect(formData[key]).toBeDefined();
});
expect(formData.optional_key1).toBeUndefined();
});

test('does omit hiddenFormData when query_mode is not enabled', async () => {
const customState = {
...reduxState,
explore: {
...reduxState.explore,
controls: {
...reduxState.explore.controls,
optional_key1: { value: 'value1' },
all_columns: { value: ['all_columns'] },
groupby: { value: ['groupby'] },
},
hiddenFormData: {
all_columns: ['all_columns'],
groupby: ['groupby'],
optional_key1: 'value1',
},
},
};

await waitFor(() => renderWithRouter({ initialState: customState }));

const formDataEndpointCalls = fetchMock.calls(/api\/v1\/explore\/form_data/);
expect(formDataEndpointCalls.length).toBeGreaterThan(0);
const lastCall = formDataEndpointCalls[formDataEndpointCalls.length - 1];

const body = JSON.parse(lastCall[1]?.body as string);
const formData = JSON.parse(body.form_data);

Object.keys(customState.explore.hiddenFormData).forEach(key => {
expect(formData[key]).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -711,11 +711,10 @@ function ExploreViewContainer(props) {

ExploreViewContainer.propTypes = propTypes;

const retainQueryModeRequirements = hiddenFormData => {
const retainQueryModeRequirements = hiddenFormData =>
Object.keys(hiddenFormData ?? {}).filter(
key => !QUERY_MODE_REQUISITES.has(key),
);
};

function mapStateToProps(state) {
const {
Expand All @@ -732,7 +731,7 @@ function mapStateToProps(state) {
const hasQueryMode = !!controls.query_mode?.value;
const fieldsToOmit = hasQueryMode
? retainQueryModeRequirements(hiddenFormData)
: hiddenFormData;
: Object.keys(hiddenFormData ?? {});
const form_data = omit(getFormDataFromControls(controls), fieldsToOmit);
const slice_id = form_data.slice_id ?? slice?.slice_id ?? 0; // 0 - unsaved chart
form_data.extra_form_data = mergeExtraFormData(
Expand Down
Loading