Skip to content

Commit

Permalink
fix(explore): Metric control breaks when saved metric deleted from da…
Browse files Browse the repository at this point in the history
…taset (#17503)
  • Loading branch information
kgabryje authored Nov 24, 2021
1 parent 83e49fc commit 7353a2b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,62 @@ test('remove selected custom metric when metric gets removed from dataset', () =
expect(screen.getByText('SUM(Column B)')).toBeVisible();
});

test('remove selected custom metric when metric gets removed from dataset for single-select metric control', () => {
let metricValue = 'metric_b';

const onChange = (val: any) => {
metricValue = val;
};

const { rerender } = render(
<DndMetricSelect
{...defaultProps}
value={metricValue}
onChange={onChange}
multi={false}
/>,
{
useDnd: true,
},
);

expect(screen.getByText('Metric B')).toBeVisible();
expect(
screen.queryByText('Drop column or metric here'),
).not.toBeInTheDocument();

const newPropsWithRemovedMetric = {
...defaultProps,
savedMetrics: [
{
metric_name: 'metric_a',
expression: 'expression_a',
},
],
};

// rerender twice - first to update columns, second to update value
rerender(
<DndMetricSelect
{...newPropsWithRemovedMetric}
value={metricValue}
onChange={onChange}
multi={false}
/>,
);
rerender(
<DndMetricSelect
{...newPropsWithRemovedMetric}
value={metricValue}
onChange={onChange}
multi={false}
/>,
);

expect(screen.queryByText('Metric B')).not.toBeInTheDocument();
expect(screen.getByText('Drop column or metric here')).toBeVisible();
});

test('remove selected adhoc metric when column gets removed from dataset', async () => {
let metricValues = ['metric_a', 'metric_b', adhocMetricA, adhocMetricB];
const onChange = (val: any[]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const getMetricsMatchingCurrentDataset = (
savedMetrics: (savedMetricType | Metric)[],
prevColumns: ColumnMeta[],
prevSavedMetrics: (savedMetricType | Metric)[],
) => {
): ValueType[] => {
const areSavedMetricsEqual =
!prevSavedMetrics || isEqual(prevSavedMetrics, savedMetrics);
const areColsEqual = !prevColumns || isEqual(prevColumns, columns);
Expand All @@ -96,16 +96,17 @@ const getMetricsMatchingCurrentDataset = (
return values;
}
return values.reduce((acc: ValueType[], metric) => {
if (
(typeof metric === 'string' || (metric as Metric).metric_name) &&
(areSavedMetricsEqual ||
if (typeof metric === 'string' || (metric as Metric).metric_name) {
if (
areSavedMetricsEqual ||
savedMetrics?.some(
savedMetric =>
savedMetric.metric_name === metric ||
savedMetric.metric_name === (metric as Metric).metric_name,
))
) {
acc.push(metric);
)
) {
acc.push(metric);
}
return acc;
}

Expand Down

0 comments on commit 7353a2b

Please sign in to comment.