Skip to content

Commit 96d7dc4

Browse files
committed
simplify new-chart hydration and tidy the tests
1 parent 084a269 commit 96d7dc4

5 files changed

Lines changed: 47 additions & 39 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { getDatasetDefaultTimeFilter } from '../../src';
20+
21+
test.each([
22+
[
23+
'{"default_time_filter": "Last week", "warning_markdown": "x"}',
24+
'Last week',
25+
],
26+
[{ default_time_filter: 'No filter' }, 'No filter'],
27+
['{"default_time_filter": ""}', undefined],
28+
['{"default_time_filter": 7}', undefined],
29+
['{bad', undefined],
30+
['[]', undefined],
31+
[undefined, undefined],
32+
])('getDatasetDefaultTimeFilter(%p) returns %p', (extra, expected) => {
33+
expect(getDatasetDefaultTimeFilter({ extra })).toBe(expected);
34+
});

‎superset-frontend/src/components/Datasource/components/DatasourceEditor/tests/DatasourceEditorDefaultTimeFilter.test.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ test('resets the default time filter to the instance default and keeps other met
7171
});
7272
});
7373
expect(
74-
within(settings).getByText('Use instance default'),
75-
).toBeInTheDocument();
74+
within(settings).getByRole('button', { name: 'Default time filter' }),
75+
).toHaveTextContent('Use instance default');
76+
expect(
77+
within(settings).queryByRole('button', { name: 'Use instance default' }),
78+
).not.toBeInTheDocument();
7679
});

‎superset-frontend/src/explore/actions/hydrateExplore.test.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,7 @@ const datasetExtra = JSON.stringify({
515515
});
516516

517517
// Table and Pie use ad hoc time filters; the calendar uses a Time range
518-
// control. They are registered only while `run` runs so other tests keep their
519-
// state.
518+
// control.
520519
function withControlPanels<T>(run: () => T): T {
521520
const registry = getChartControlPanelRegistry();
522521
const panels = {
@@ -547,8 +546,7 @@ type NewChartOptions = {
547546
dataset?: Partial<typeof exploreInitialData.dataset>;
548547
};
549548

550-
// Hydrates a new chart on a dataset whose default datetime column is not its
551-
// first temporal column. Call it inside `withControlPanels`.
549+
// The dataset's default datetime column is not its first temporal column.
552550
function hydrateNewChart({
553551
vizType = VizType.Table,
554552
extra = datasetExtra,
@@ -590,7 +588,6 @@ function hydrateNewChart({
590588
return dispatch.mock.calls[0][0].data;
591589
}
592590

593-
// Returns the form data of the first query of a new chart.
594591
function hydrateNewChartQuery(options?: NewChartOptions): QueryFormData {
595592
const { charts } = withControlPanels(() => hydrateNewChart(options));
596593
return Object.values(charts)[0].latestQueryFormData as QueryFormData;
@@ -616,10 +613,7 @@ test.each([
616613
[VizType.Table, datasetExtra, datasetRange],
617614
['cal_heatmap', datasetExtra, datasetRange],
618615
[VizType.Table, `{"default_time_filter": "${NO_TIME_RANGE}"}`, NO_TIME_RANGE],
619-
['cal_heatmap', `{"default_time_filter": "${NO_TIME_RANGE}"}`, NO_TIME_RANGE],
620616
[VizType.Table, '{}', instanceRange],
621-
['cal_heatmap', '{}', instanceRange],
622-
[VizType.Table, '{bad', instanceRange],
623617
])(
624618
'a new %s chart on a dataset with extra %s starts with %s on the default datetime column',
625619
(vizType, extra, range) => {
@@ -698,8 +692,6 @@ test.each([
698692
},
699693
);
700694

701-
// Switches a new table chart, which starts with the instance range on
702-
// `created_at`, to another dataset. Call it inside `withControlPanels`.
703695
function switchNewChartDataset(dataset: Partial<Dataset>) {
704696
const { explore } = hydrateNewChart({ extra: '{}' });
705697
return exploreReducer(explore as ExploreState, {
@@ -714,7 +706,6 @@ function switchNewChartDataset(dataset: Partial<Dataset>) {
714706
});
715707
}
716708

717-
// Changes the chart type and returns the form data of the new chart.
718709
function changeChartType(explore: ExploreState, vizType: string) {
719710
const { controls } = exploreReducer(explore, {
720711
type: 'SET_FIELD_VALUE',

‎superset-frontend/src/explore/actions/hydrateExplore.ts‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
*/
1919
import {
2020
ControlStateMapping,
21-
Dataset,
2221
getDatasetDefaultTimeFilter,
23-
getTemporalColumns,
2422
} from '@superset-ui/chart-controls';
2523

2624
import {
@@ -42,10 +40,8 @@ import {
4240
getColumnLabel,
4341
getSequentialSchemeRegistry,
4442
isFeatureEnabled,
45-
isSimpleAdhocFilter,
4643
NO_TIME_RANGE,
4744
QueryFormColumn,
48-
QueryFormData,
4945
VizType,
5046
} from '@superset-ui/core';
5147
import {
@@ -89,24 +85,6 @@ function isNeutralNewChartEntry([key, value]: [string, unknown]): boolean {
8985
);
9086
}
9187

92-
/**
93-
* Returns the dataset's default time filter for a new chart, unless the chart
94-
* already has a time filter or the dataset has no temporal column.
95-
*/
96-
function getNewChartTimeRange(
97-
dataset: Dataset,
98-
formData: QueryFormData,
99-
): string | undefined {
100-
const hasTimeFilter = ensureIsArray(formData.adhoc_filters).some(
101-
filter =>
102-
isSimpleAdhocFilter(filter) && filter.operator === 'TEMPORAL_RANGE',
103-
);
104-
if (hasTimeFilter || !getTemporalColumns(dataset).defaultTemporalColumn) {
105-
return undefined;
106-
}
107-
return getDatasetDefaultTimeFilter(dataset);
108-
}
109-
11088
export const hydrateExplore =
11189
({
11290
form_data,
@@ -157,7 +135,7 @@ export const hydrateExplore =
157135
}
158136
if (!initialFormData.time_range) {
159137
initialFormData.time_range =
160-
(isNewChart && getNewChartTimeRange(dataset, initialFormData)) ||
138+
(isNewChart && getDatasetDefaultTimeFilter(dataset)) ||
161139
common?.conf?.DEFAULT_TIME_FILTER ||
162140
NO_TIME_RANGE;
163141
}

‎superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSimpleTabContent/AdhocFilterEditPopoverSimpleTabContent.test.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,11 @@ test('editing a dashboard-inherited time range filter clears isExtra so the new
478478

479479
test('the date picker of a temporal filter without a time range starts with the dataset default time filter', async () => {
480480
const timeRangeEndpoint = 'glob:*/api/v1/time_range/*';
481-
fetchMock.get(timeRangeEndpoint, {
482-
result: [{ since: '', until: '', timeRange: 'Last day' }],
483-
});
481+
fetchMock.get(
482+
timeRangeEndpoint,
483+
{ result: [{ since: '', until: '', timeRange: 'Last day' }] },
484+
{ name: timeRangeEndpoint },
485+
);
484486
try {
485487
setup({
486488
adhocFilter: new AdhocFilter({

0 commit comments

Comments
 (0)