This is an automated email from the ASF dual-hosted git repository. michaelsmolina pushed a commit to branch 4.0 in repository https://gitbox.apache.org/repos/asf/superset.git
commit 88967ba634af37eda6f55dd6cfb9b9f37eae06b5 Author: Ross Mabbett <[email protected]> AuthorDate: Fri Jun 14 12:40:05 2024 -0400 test(Explorer): Fix minor errors in ExploreViewContainer syntax, add tests (#29249) (cherry picked from commit 2418efe85c2fbdd33365b9776fe416a87bf7b4b4) --- .../ExploreViewContainer.test.tsx | 73 ++++++++++++++++++++++ .../components/ExploreViewContainer/index.jsx | 5 +- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/superset-frontend/src/explore/components/ExploreViewContainer/ExploreViewContainer.test.tsx b/superset-frontend/src/explore/components/ExploreViewContainer/ExploreViewContainer.test.tsx index c58f8e04f5..6b6577110d 100644 --- a/superset-frontend/src/explore/components/ExploreViewContainer/ExploreViewContainer.test.tsx +++ b/superset-frontend/src/explore/components/ExploreViewContainer/ExploreViewContainer.test.tsx @@ -23,6 +23,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'; @@ -222,3 +223,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(); + }); +}); diff --git a/superset-frontend/src/explore/components/ExploreViewContainer/index.jsx b/superset-frontend/src/explore/components/ExploreViewContainer/index.jsx index a2ac4223a4..626325c53a 100644 --- a/superset-frontend/src/explore/components/ExploreViewContainer/index.jsx +++ b/superset-frontend/src/explore/components/ExploreViewContainer/index.jsx @@ -705,11 +705,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 { @@ -726,7 +725,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(
