mirror of https://github.com/apache/superset.git
chore(AlteredSliceTag): Migrate to functional (#27891)
Co-authored-by: Elizabeth Thompson <eschutho@gmail.com>
This commit is contained in:
parent
44690fb299
commit
efda57e8a5
|
@ -17,252 +17,132 @@
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { styledMount as mount } from 'spec/helpers/theming';
|
import '@testing-library/jest-dom/extend-expect';
|
||||||
import { getChartControlPanelRegistry } from '@superset-ui/core';
|
import { render, screen } from 'spec/helpers/testing-library';
|
||||||
|
import userEvent from '@testing-library/user-event';
|
||||||
|
import AlteredSliceTag, {
|
||||||
|
alterForComparison,
|
||||||
|
formatValueHandler,
|
||||||
|
isEqualish,
|
||||||
|
} from 'src/components/AlteredSliceTag';
|
||||||
|
import { defaultProps } from './AlteredSliceTagMocks';
|
||||||
|
|
||||||
import AlteredSliceTag from 'src/components/AlteredSliceTag';
|
const controlsMap = {
|
||||||
import ModalTrigger from 'src/components/ModalTrigger';
|
b: { type: 'BoundsControl', label: 'Bounds' },
|
||||||
import { Tooltip } from 'src/components/Tooltip';
|
column_collection: { type: 'CollectionControl', label: 'Collection' },
|
||||||
import TableCollection from 'src/components/TableCollection';
|
metrics: { type: 'MetricsControl', label: 'Metrics' },
|
||||||
import TableView from 'src/components/TableView';
|
adhoc_filters: { type: 'AdhocFilterControl', label: 'Adhoc' },
|
||||||
|
other_control: { type: 'OtherControl', label: 'Other' },
|
||||||
import {
|
|
||||||
defaultProps,
|
|
||||||
expectedDiffs,
|
|
||||||
expectedRows,
|
|
||||||
fakePluginControls,
|
|
||||||
} from './AlteredSliceTagMocks';
|
|
||||||
|
|
||||||
const getTableWrapperFromModalBody = modalBody =>
|
|
||||||
modalBody.find(TableView).find(TableCollection);
|
|
||||||
|
|
||||||
describe('AlteredSliceTag', () => {
|
|
||||||
let wrapper;
|
|
||||||
let props;
|
|
||||||
let controlsMap;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
getChartControlPanelRegistry().registerValue(
|
|
||||||
'altered_slice_tag_spec',
|
|
||||||
fakePluginControls,
|
|
||||||
);
|
|
||||||
props = { ...defaultProps };
|
|
||||||
wrapper = mount(<AlteredSliceTag {...props} />);
|
|
||||||
({ controlsMap } = wrapper.instance().state);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('correctly determines form data differences', () => {
|
|
||||||
const diffs = wrapper.instance().getDiffs(props);
|
|
||||||
expect(diffs).toEqual(expectedDiffs);
|
|
||||||
expect(wrapper.instance().state.rows).toEqual(expectedRows);
|
|
||||||
expect(wrapper.instance().state.hasDiffs).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not run when there are no differences', () => {
|
|
||||||
props = {
|
|
||||||
origFormData: props.origFormData,
|
|
||||||
currentFormData: props.origFormData,
|
|
||||||
};
|
};
|
||||||
wrapper = mount(<AlteredSliceTag {...props} />);
|
|
||||||
expect(wrapper.instance().state.rows).toEqual([]);
|
|
||||||
expect(wrapper.instance().state.hasDiffs).toBe(false);
|
|
||||||
expect(wrapper.instance().render()).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not run when temporary controls have changes', () => {
|
test('renders the "Altered" label', () => {
|
||||||
props = {
|
render(
|
||||||
origFormData: { ...props.origFormData, url_params: { foo: 'foo' } },
|
<AlteredSliceTag
|
||||||
currentFormData: { ...props.origFormData, url_params: { bar: 'bar' } },
|
origFormData={defaultProps.origFormData}
|
||||||
};
|
currentFormData={defaultProps.currentFormData}
|
||||||
wrapper = mount(<AlteredSliceTag {...props} />);
|
/>,
|
||||||
expect(wrapper.instance().state.rows).toEqual([]);
|
|
||||||
expect(wrapper.instance().state.hasDiffs).toBe(false);
|
|
||||||
expect(wrapper.instance().render()).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sets new rows when receiving new props', () => {
|
|
||||||
const testRows = ['testValue'];
|
|
||||||
const getRowsFromDiffsStub = jest
|
|
||||||
.spyOn(AlteredSliceTag.prototype, 'getRowsFromDiffs')
|
|
||||||
.mockReturnValueOnce(testRows);
|
|
||||||
const newProps = {
|
|
||||||
currentFormData: { ...props.currentFormData },
|
|
||||||
origFormData: { ...props.origFormData },
|
|
||||||
};
|
|
||||||
wrapper = mount(<AlteredSliceTag {...props} />);
|
|
||||||
const wrapperInstance = wrapper.instance();
|
|
||||||
wrapperInstance.UNSAFE_componentWillReceiveProps(newProps);
|
|
||||||
expect(getRowsFromDiffsStub).toHaveBeenCalled();
|
|
||||||
expect(wrapperInstance.state.rows).toEqual(testRows);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not set new state when props are the same', () => {
|
|
||||||
const currentRows = wrapper.instance().state.rows;
|
|
||||||
wrapper.instance().UNSAFE_componentWillReceiveProps(props);
|
|
||||||
// Check equal references
|
|
||||||
expect(wrapper.instance().state.rows).toBe(currentRows);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('renders a ModalTrigger', () => {
|
|
||||||
expect(wrapper.find(ModalTrigger)).toExist();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('renderTriggerNode', () => {
|
|
||||||
it('renders a Tooltip', () => {
|
|
||||||
const triggerNode = mount(
|
|
||||||
<div>{wrapper.instance().renderTriggerNode()}</div>,
|
|
||||||
);
|
);
|
||||||
expect(triggerNode.find(Tooltip)).toHaveLength(1);
|
|
||||||
});
|
const alteredLabel = screen.getByText('Altered');
|
||||||
|
expect(alteredLabel).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('renderModalBody', () => {
|
test('opens the modal on click', () => {
|
||||||
it('renders a Table', () => {
|
render(
|
||||||
const modalBody = mount(
|
<AlteredSliceTag
|
||||||
<div>{wrapper.instance().renderModalBody()}</div>,
|
origFormData={defaultProps.origFormData}
|
||||||
|
currentFormData={defaultProps.currentFormData}
|
||||||
|
/>,
|
||||||
);
|
);
|
||||||
expect(modalBody.find(TableView)).toHaveLength(1);
|
|
||||||
|
const alteredLabel = screen.getByText('Altered');
|
||||||
|
userEvent.click(alteredLabel);
|
||||||
|
|
||||||
|
const modalTitle = screen.getByText('Chart changes');
|
||||||
|
expect(modalTitle).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('renders a thead', () => {
|
test('displays the differences in the modal', () => {
|
||||||
const modalBody = mount(
|
render(
|
||||||
<div>{wrapper.instance().renderModalBody()}</div>,
|
<AlteredSliceTag
|
||||||
|
origFormData={defaultProps.origFormData}
|
||||||
|
currentFormData={defaultProps.currentFormData}
|
||||||
|
/>,
|
||||||
);
|
);
|
||||||
expect(
|
|
||||||
getTableWrapperFromModalBody(modalBody).find('thead'),
|
const alteredLabel = screen.getByText('Altered');
|
||||||
).toHaveLength(1);
|
userEvent.click(alteredLabel);
|
||||||
|
|
||||||
|
const beforeValue = screen.getByText('1, 2, 3, 4');
|
||||||
|
const afterValue = screen.getByText('a, b, c, d');
|
||||||
|
expect(beforeValue).toBeInTheDocument();
|
||||||
|
expect(afterValue).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('renders th', () => {
|
test('does not render anything if there are no differences', () => {
|
||||||
const modalBody = mount(
|
render(
|
||||||
<div>{wrapper.instance().renderModalBody()}</div>,
|
<AlteredSliceTag
|
||||||
|
origFormData={defaultProps.origFormData}
|
||||||
|
currentFormData={defaultProps.origFormData}
|
||||||
|
/>,
|
||||||
);
|
);
|
||||||
const th = getTableWrapperFromModalBody(modalBody).find('th');
|
|
||||||
expect(th).toHaveLength(3);
|
const alteredLabel = screen.queryByText('Altered');
|
||||||
['Control', 'Before', 'After'].forEach(async (v, i) => {
|
expect(alteredLabel).not.toBeInTheDocument();
|
||||||
await expect(th.at(i).find('span').get(0).props.children[0]).toBe(v);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('renders the correct number of Tr', () => {
|
test('alterForComparison returns null for undefined value', () => {
|
||||||
const modalBody = mount(
|
expect(alterForComparison(undefined)).toBeNull();
|
||||||
<div>{wrapper.instance().renderModalBody()}</div>,
|
|
||||||
);
|
|
||||||
const tr = getTableWrapperFromModalBody(modalBody).find('tr');
|
|
||||||
expect(tr).toHaveLength(8);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('renders the correct number of td', () => {
|
test('alterForComparison returns null for null value', () => {
|
||||||
const modalBody = mount(
|
expect(alterForComparison(null)).toBeNull();
|
||||||
<div>{wrapper.instance().renderModalBody()}</div>,
|
|
||||||
);
|
|
||||||
const td = getTableWrapperFromModalBody(modalBody).find('td');
|
|
||||||
expect(td).toHaveLength(21);
|
|
||||||
['control', 'before', 'after'].forEach((v, i) => {
|
|
||||||
expect(td.find('defaultRenderer').get(0).props.columns[i].id).toBe(v);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('renderRows', () => {
|
test('alterForComparison returns null for empty string value', () => {
|
||||||
it('returns an array of rows with one tr and three td', () => {
|
expect(alterForComparison('')).toBeNull();
|
||||||
const modalBody = mount(
|
|
||||||
<div>{wrapper.instance().renderModalBody()}</div>,
|
|
||||||
);
|
|
||||||
const rows = getTableWrapperFromModalBody(modalBody).find('tr');
|
|
||||||
expect(rows).toHaveLength(8);
|
|
||||||
const slice = mount(
|
|
||||||
<table>
|
|
||||||
<tbody>{rows.get(1)}</tbody>
|
|
||||||
</table>,
|
|
||||||
);
|
|
||||||
expect(slice.find('tr')).toHaveLength(1);
|
|
||||||
expect(slice.find('td')).toHaveLength(3);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('formatValue', () => {
|
test('alterForComparison returns null for empty array value', () => {
|
||||||
it('returns "N/A" for undefined values', () => {
|
expect(alterForComparison([])).toBeNull();
|
||||||
expect(wrapper.instance().formatValue(undefined, 'b', controlsMap)).toBe(
|
|
||||||
'N/A',
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns "null" for null values', () => {
|
test('alterForComparison returns null for empty object value', () => {
|
||||||
expect(wrapper.instance().formatValue(null, 'b', controlsMap)).toBe(
|
expect(alterForComparison({})).toBeNull();
|
||||||
'null',
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns "Max" and "Min" for BoundsControl', () => {
|
test('alterForComparison returns value for non-empty array', () => {
|
||||||
// need to pass the viz type to the wrapper
|
const value = [1, 2, 3];
|
||||||
expect(
|
expect(alterForComparison(value)).toEqual(value);
|
||||||
wrapper.instance().formatValue([5, 6], 'y_axis_bounds', controlsMap),
|
|
||||||
).toBe('Min: 5, Max: 6');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns stringified objects for CollectionControl', () => {
|
test('alterForComparison returns value for non-empty object', () => {
|
||||||
const value = [
|
const value = { key: 'value' };
|
||||||
{ 1: 2, alpha: 'bravo' },
|
expect(alterForComparison(value)).toEqual(value);
|
||||||
{ sent: 'imental', w0ke: 5 },
|
|
||||||
];
|
|
||||||
const expected = '{"1":2,"alpha":"bravo"}, {"sent":"imental","w0ke":5}';
|
|
||||||
expect(
|
|
||||||
wrapper.instance().formatValue(value, 'column_collection', controlsMap),
|
|
||||||
).toBe(expected);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns boolean values as string', () => {
|
test('formatValueHandler handles undefined value', () => {
|
||||||
expect(wrapper.instance().formatValue(true, 'b', controlsMap)).toBe(
|
const value = undefined;
|
||||||
'true',
|
const key = 'b';
|
||||||
);
|
const formattedValue = formatValueHandler(value, key, controlsMap);
|
||||||
expect(wrapper.instance().formatValue(false, 'b', controlsMap)).toBe(
|
expect(formattedValue).toBe('N/A');
|
||||||
'false',
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns Array joined by commas', () => {
|
test('formatValueHandler handles null value', () => {
|
||||||
const value = [5, 6, 7, 8, 'hello', 'goodbye'];
|
const value = null;
|
||||||
const expected = '5, 6, 7, 8, hello, goodbye';
|
const key = 'b';
|
||||||
expect(
|
const formattedValue = formatValueHandler(value, key, controlsMap);
|
||||||
wrapper.instance().formatValue(value, undefined, controlsMap),
|
expect(formattedValue).toBe('null');
|
||||||
).toBe(expected);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns Metrics if the field type is metrics', () => {
|
test('formatValueHandler returns "[]" for empty filters', () => {
|
||||||
const value = [
|
const value = [];
|
||||||
{
|
const key = 'adhoc_filters';
|
||||||
label: 'SUM(Sales)',
|
const formattedValue = formatValueHandler(value, key, controlsMap);
|
||||||
},
|
expect(formattedValue).toBe('[]');
|
||||||
];
|
|
||||||
const expected = 'SUM(Sales)';
|
|
||||||
expect(
|
|
||||||
wrapper.instance().formatValue(value, 'metrics', controlsMap),
|
|
||||||
).toBe(expected);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('stringifies objects', () => {
|
test('formatValueHandler formats filters with array values', () => {
|
||||||
const value = { 1: 2, alpha: 'bravo' };
|
|
||||||
const expected = '{"1":2,"alpha":"bravo"}';
|
|
||||||
expect(
|
|
||||||
wrapper.instance().formatValue(value, undefined, controlsMap),
|
|
||||||
).toBe(expected);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does nothing to strings and numbers', () => {
|
|
||||||
expect(wrapper.instance().formatValue(5, undefined, controlsMap)).toBe(5);
|
|
||||||
expect(
|
|
||||||
wrapper.instance().formatValue('hello', undefined, controlsMap),
|
|
||||||
).toBe('hello');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns "[]" for empty filters', () => {
|
|
||||||
expect(
|
|
||||||
wrapper.instance().formatValue([], 'adhoc_filters', controlsMap),
|
|
||||||
).toBe('[]');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('correctly formats filters with array values', () => {
|
|
||||||
const filters = [
|
const filters = [
|
||||||
{
|
{
|
||||||
clause: 'WHERE',
|
clause: 'WHERE',
|
||||||
|
@ -279,13 +159,13 @@ describe('AlteredSliceTag', () => {
|
||||||
subject: 'b',
|
subject: 'b',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
const key = 'adhoc_filters';
|
||||||
|
const formattedValue = formatValueHandler(filters, key, controlsMap);
|
||||||
const expected = 'a IN [1, g, 7, ho], b NOT IN [hu, ho, ha]';
|
const expected = 'a IN [1, g, 7, ho], b NOT IN [hu, ho, ha]';
|
||||||
expect(
|
expect(formattedValue).toBe(expected);
|
||||||
wrapper.instance().formatValue(filters, 'adhoc_filters', controlsMap),
|
|
||||||
).toBe(expected);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('correctly formats filters with string values', () => {
|
test('formatValueHandler formats filters with string values', () => {
|
||||||
const filters = [
|
const filters = [
|
||||||
{
|
{
|
||||||
clause: 'WHERE',
|
clause: 'WHERE',
|
||||||
|
@ -302,40 +182,100 @@ describe('AlteredSliceTag', () => {
|
||||||
subject: 'b',
|
subject: 'b',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
const key = 'adhoc_filters';
|
||||||
const expected = 'a == gucci, b LIKE moshi moshi';
|
const expected = 'a == gucci, b LIKE moshi moshi';
|
||||||
expect(
|
const formattedValue = formatValueHandler(filters, key, controlsMap);
|
||||||
wrapper.instance().formatValue(filters, 'adhoc_filters', controlsMap),
|
expect(formattedValue).toBe(expected);
|
||||||
).toBe(expected);
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
describe('isEqualish', () => {
|
|
||||||
it('considers null, undefined, {} and [] as equal', () => {
|
|
||||||
const inst = wrapper.instance();
|
|
||||||
expect(inst.isEqualish(null, undefined)).toBe(true);
|
|
||||||
expect(inst.isEqualish(null, [])).toBe(true);
|
|
||||||
expect(inst.isEqualish(null, {})).toBe(true);
|
|
||||||
expect(inst.isEqualish(undefined, {})).toBe(true);
|
|
||||||
});
|
|
||||||
it('considers empty strings are the same as null', () => {
|
|
||||||
const inst = wrapper.instance();
|
|
||||||
expect(inst.isEqualish(undefined, '')).toBe(true);
|
|
||||||
expect(inst.isEqualish(null, '')).toBe(true);
|
|
||||||
});
|
|
||||||
it('considers deeply equal objects as equal', () => {
|
|
||||||
const inst = wrapper.instance();
|
|
||||||
expect(inst.isEqualish('', '')).toBe(true);
|
|
||||||
expect(inst.isEqualish({ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 })).toBe(
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
// Out of order
|
|
||||||
expect(inst.isEqualish({ a: 1, b: 2, c: 3 }, { b: 2, a: 1, c: 3 })).toBe(
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Actually not equal
|
test('formatValueHandler formats "Min" and "Max" for BoundsControl', () => {
|
||||||
expect(inst.isEqualish({ a: 1, b: 2, z: 9 }, { a: 1, b: 2, c: 3 })).toBe(
|
const value = [1, 2];
|
||||||
false,
|
const key = 'b';
|
||||||
|
const result = formatValueHandler(value, key, controlsMap);
|
||||||
|
expect(result).toEqual('Min: 1, Max: 2');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('formatValueHandler formats stringified objects for CollectionControl', () => {
|
||||||
|
const value = [{ a: 1 }, { b: 2 }];
|
||||||
|
const key = 'column_collection';
|
||||||
|
const result = formatValueHandler(value, key, controlsMap);
|
||||||
|
expect(result).toEqual(
|
||||||
|
`${JSON.stringify(value[0])}, ${JSON.stringify(value[1])}`,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('formatValueHandler formats MetricsControl values correctly', () => {
|
||||||
|
const value = [{ label: 'SUM(Sales)' }, { label: 'Metric2' }];
|
||||||
|
const key = 'metrics';
|
||||||
|
const result = formatValueHandler(value, key, controlsMap);
|
||||||
|
expect(result).toEqual('SUM(Sales), Metric2');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('formatValueHandler formats boolean values as string', () => {
|
||||||
|
const value1 = true;
|
||||||
|
const value2 = false;
|
||||||
|
const key = 'b';
|
||||||
|
const formattedValue1 = formatValueHandler(value1, key, controlsMap);
|
||||||
|
const formattedValue2 = formatValueHandler(value2, key, controlsMap);
|
||||||
|
expect(formattedValue1).toBe('true');
|
||||||
|
expect(formattedValue2).toBe('false');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('formatValueHandler formats array values correctly', () => {
|
||||||
|
const value = [
|
||||||
|
{ label: 'Label1' },
|
||||||
|
{ label: 'Label2' },
|
||||||
|
5,
|
||||||
|
6,
|
||||||
|
7,
|
||||||
|
8,
|
||||||
|
'hello',
|
||||||
|
'goodbye',
|
||||||
|
];
|
||||||
|
const result = formatValueHandler(value, undefined, controlsMap);
|
||||||
|
const expected = 'Label1, Label2, 5, 6, 7, 8, hello, goodbye';
|
||||||
|
expect(result).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('formatValueHandler formats string values correctly', () => {
|
||||||
|
const value = 'test';
|
||||||
|
const key = 'other_control';
|
||||||
|
const result = formatValueHandler(value, key, controlsMap);
|
||||||
|
expect(result).toEqual('test');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('formatValueHandler formats number values correctly', () => {
|
||||||
|
const value = 123;
|
||||||
|
const key = 'other_control';
|
||||||
|
const result = formatValueHandler(value, key, controlsMap);
|
||||||
|
expect(result).toEqual(123);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('formatValueHandler formats object values correctly', () => {
|
||||||
|
const value = { 1: 2, alpha: 'bravo' };
|
||||||
|
const key = 'other_control';
|
||||||
|
const expected = '{"1":2,"alpha":"bravo"}';
|
||||||
|
const result = formatValueHandler(value, key, controlsMap);
|
||||||
|
expect(result).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('isEqualish considers null, undefined, {} and [] as equal', () => {
|
||||||
|
expect(isEqualish(null, undefined)).toBe(true);
|
||||||
|
expect(isEqualish(null, [])).toBe(true);
|
||||||
|
expect(isEqualish(null, {})).toBe(true);
|
||||||
|
expect(isEqualish(undefined, {})).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('isEqualish considers empty strings equal to null', () => {
|
||||||
|
expect(isEqualish(undefined, '')).toBe(true);
|
||||||
|
expect(isEqualish(null, '')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('isEqualish considers deeply equal objects equal', () => {
|
||||||
|
const obj1 = { a: { b: { c: 1 } } };
|
||||||
|
const obj2 = { a: { b: { c: 1 } } };
|
||||||
|
expect(isEqualish('', '')).toBe(true);
|
||||||
|
expect(isEqualish(obj1, obj2)).toBe(true);
|
||||||
|
// Actually not equal
|
||||||
|
expect(isEqualish({ a: 1, b: 2, z: 9 }, { a: 1, b: 2, c: 3 })).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
import React from 'react';
|
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import { isEqual, isEmpty } from 'lodash';
|
import { isEqual, isEmpty } from 'lodash';
|
||||||
import { QueryFormData, styled, t } from '@superset-ui/core';
|
import { QueryFormData, styled, t } from '@superset-ui/core';
|
||||||
import { sanitizeFormData } from 'src/explore/exploreUtils/formData';
|
import { sanitizeFormData } from 'src/explore/exploreUtils/formData';
|
||||||
|
@ -67,12 +67,6 @@ export type RowType = {
|
||||||
control: string;
|
control: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
interface AlteredSliceTagState {
|
|
||||||
rows: RowType[];
|
|
||||||
hasDiffs: boolean;
|
|
||||||
controlsMap: ControlMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
const StyledLabel = styled.span`
|
const StyledLabel = styled.span`
|
||||||
${({ theme }) => `
|
${({ theme }) => `
|
||||||
font-size: ${theme.typography.sizes.s}px;
|
font-size: ${theme.typography.sizes.s}px;
|
||||||
|
@ -85,7 +79,9 @@ const StyledLabel = styled.span`
|
||||||
`}
|
`}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
function alterForComparison(value?: string | null | []): string | null {
|
export const alterForComparison = (
|
||||||
|
value?: string | null | [],
|
||||||
|
): string | null => {
|
||||||
// Treat `null`, `undefined`, and empty strings as equivalent
|
// Treat `null`, `undefined`, and empty strings as equivalent
|
||||||
if (value === undefined || value === null || value === '') {
|
if (value === undefined || value === null || value === '') {
|
||||||
return null;
|
return null;
|
||||||
|
@ -98,83 +94,23 @@ function alterForComparison(value?: string | null | []): string | null {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
};
|
||||||
|
|
||||||
class AlteredSliceTag extends React.Component<
|
export const formatValueHandler = (
|
||||||
AlteredSliceTagProps,
|
|
||||||
AlteredSliceTagState
|
|
||||||
> {
|
|
||||||
constructor(props: AlteredSliceTagProps) {
|
|
||||||
super(props);
|
|
||||||
const diffs = this.getDiffs(props);
|
|
||||||
const controlsMap: ControlMap = getControlsForVizType(
|
|
||||||
props.origFormData.viz_type,
|
|
||||||
) as ControlMap;
|
|
||||||
const rows = this.getRowsFromDiffs(diffs, controlsMap);
|
|
||||||
|
|
||||||
this.state = { rows, hasDiffs: !isEmpty(diffs), controlsMap };
|
|
||||||
}
|
|
||||||
|
|
||||||
UNSAFE_componentWillReceiveProps(newProps: AlteredSliceTagProps): void {
|
|
||||||
if (isEqual(this.props, newProps)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const diffs = this.getDiffs(newProps);
|
|
||||||
this.setState(prevState => ({
|
|
||||||
rows: this.getRowsFromDiffs(diffs, prevState.controlsMap),
|
|
||||||
hasDiffs: !isEmpty(diffs),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
getRowsFromDiffs(
|
|
||||||
diffs: { [key: string]: DiffType },
|
|
||||||
controlsMap: ControlMap,
|
|
||||||
): RowType[] {
|
|
||||||
return Object.entries(diffs).map(([key, diff]) => ({
|
|
||||||
control: controlsMap[key]?.label || key,
|
|
||||||
before: this.formatValue(diff.before, key, controlsMap),
|
|
||||||
after: this.formatValue(diff.after, key, controlsMap),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
getDiffs(props: AlteredSliceTagProps): { [key: string]: DiffType } {
|
|
||||||
const ofd = sanitizeFormData(props.origFormData);
|
|
||||||
const cfd = sanitizeFormData(props.currentFormData);
|
|
||||||
const fdKeys = Object.keys(cfd);
|
|
||||||
const diffs: { [key: string]: DiffType } = {};
|
|
||||||
fdKeys.forEach(fdKey => {
|
|
||||||
if (!ofd[fdKey] && !cfd[fdKey]) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (['filters', 'having', 'where'].includes(fdKey)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!this.isEqualish(ofd[fdKey], cfd[fdKey])) {
|
|
||||||
diffs[fdKey] = { before: ofd[fdKey], after: cfd[fdKey] };
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return diffs;
|
|
||||||
}
|
|
||||||
|
|
||||||
isEqualish(val1: string, val2: string): boolean {
|
|
||||||
return isEqual(alterForComparison(val1), alterForComparison(val2));
|
|
||||||
}
|
|
||||||
|
|
||||||
formatValue(
|
|
||||||
value: DiffItemType,
|
value: DiffItemType,
|
||||||
key: string,
|
key: string,
|
||||||
controlsMap: ControlMap,
|
controlsMap: ControlMap,
|
||||||
): string | number {
|
): string | number => {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
return 'N/A';
|
return 'N/A';
|
||||||
}
|
}
|
||||||
if (value === null) {
|
if (value === null) {
|
||||||
return 'null';
|
return 'null';
|
||||||
}
|
}
|
||||||
if (
|
if (typeof value === 'boolean') {
|
||||||
controlsMap[key]?.type === 'AdhocFilterControl' &&
|
return value ? 'true' : 'false';
|
||||||
Array.isArray(value)
|
}
|
||||||
) {
|
if (controlsMap[key]?.type === 'AdhocFilterControl' && Array.isArray(value)) {
|
||||||
if (!value.length) {
|
if (!value.length) {
|
||||||
return '[]';
|
return '[]';
|
||||||
}
|
}
|
||||||
|
@ -191,19 +127,16 @@ class AlteredSliceTag extends React.Component<
|
||||||
if (controlsMap[key]?.type === 'BoundsControl') {
|
if (controlsMap[key]?.type === 'BoundsControl') {
|
||||||
return `Min: ${value[0]}, Max: ${value[1]}`;
|
return `Min: ${value[0]}, Max: ${value[1]}`;
|
||||||
}
|
}
|
||||||
if (
|
if (controlsMap[key]?.type === 'CollectionControl' && Array.isArray(value)) {
|
||||||
controlsMap[key]?.type === 'CollectionControl' &&
|
return value.map((v: FilterItemType) => safeStringify(v)).join(', ');
|
||||||
Array.isArray(value)
|
|
||||||
) {
|
|
||||||
return value.map(v => safeStringify(v)).join(', ');
|
|
||||||
}
|
}
|
||||||
if (controlsMap[key]?.type === 'MetricsControl' && Array.isArray(value)) {
|
if (
|
||||||
|
controlsMap[key]?.type === 'MetricsControl' &&
|
||||||
|
value.constructor === Array
|
||||||
|
) {
|
||||||
const formattedValue = value.map((v: FilterItemType) => v?.label ?? v);
|
const formattedValue = value.map((v: FilterItemType) => v?.label ?? v);
|
||||||
return formattedValue.length ? formattedValue.join(', ') : '[]';
|
return formattedValue.length ? formattedValue.join(', ') : '[]';
|
||||||
}
|
}
|
||||||
if (typeof value === 'boolean') {
|
|
||||||
return value ? 'true' : 'false';
|
|
||||||
}
|
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
const formattedValue = value.map((v: FilterItemType) => v?.label ?? v);
|
const formattedValue = value.map((v: FilterItemType) => v?.label ?? v);
|
||||||
return formattedValue.length ? formattedValue.join(', ') : '[]';
|
return formattedValue.length ? formattedValue.join(', ') : '[]';
|
||||||
|
@ -212,9 +145,57 @@ class AlteredSliceTag extends React.Component<
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
return safeStringify(value);
|
return safeStringify(value);
|
||||||
}
|
};
|
||||||
|
|
||||||
renderModalBody(): React.ReactNode {
|
export const getRowsFromDiffs = (
|
||||||
|
diffs: { [key: string]: DiffType },
|
||||||
|
controlsMap: ControlMap,
|
||||||
|
): RowType[] =>
|
||||||
|
Object.entries(diffs).map(([key, diff]) => ({
|
||||||
|
control: controlsMap[key]?.label || key,
|
||||||
|
before: formatValueHandler(diff.before, key, controlsMap),
|
||||||
|
after: formatValueHandler(diff.after, key, controlsMap),
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const isEqualish = (val1: string, val2: string): boolean =>
|
||||||
|
isEqual(alterForComparison(val1), alterForComparison(val2));
|
||||||
|
|
||||||
|
const AlteredSliceTag: React.FC<AlteredSliceTagProps> = props => {
|
||||||
|
const [rows, setRows] = useState<RowType[]>([]);
|
||||||
|
const [hasDiffs, setHasDiffs] = useState<boolean>(false);
|
||||||
|
|
||||||
|
const getDiffs = useCallback(() => {
|
||||||
|
// Returns all properties that differ in the
|
||||||
|
// current form data and the saved form data
|
||||||
|
const ofd = sanitizeFormData(props.origFormData);
|
||||||
|
const cfd = sanitizeFormData(props.currentFormData);
|
||||||
|
|
||||||
|
const fdKeys = Object.keys(cfd);
|
||||||
|
const diffs: { [key: string]: DiffType } = {};
|
||||||
|
fdKeys.forEach(fdKey => {
|
||||||
|
if (!ofd[fdKey] && !cfd[fdKey]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (['filters', 'having', 'where'].includes(fdKey)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!isEqualish(ofd[fdKey], cfd[fdKey])) {
|
||||||
|
diffs[fdKey] = { before: ofd[fdKey], after: cfd[fdKey] };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return diffs;
|
||||||
|
}, [props.currentFormData, props.origFormData]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const diffs = getDiffs();
|
||||||
|
const controlsMap = getControlsForVizType(
|
||||||
|
props.origFormData?.viz_type,
|
||||||
|
) as ControlMap;
|
||||||
|
setRows(getRowsFromDiffs(diffs, controlsMap));
|
||||||
|
setHasDiffs(!isEmpty(diffs));
|
||||||
|
}, [getDiffs, props.origFormData?.viz_type]);
|
||||||
|
|
||||||
|
const modalBody = useMemo(() => {
|
||||||
const columns = [
|
const columns = [
|
||||||
{
|
{
|
||||||
accessor: 'control',
|
accessor: 'control',
|
||||||
|
@ -235,39 +216,35 @@ class AlteredSliceTag extends React.Component<
|
||||||
return (
|
return (
|
||||||
<TableView
|
<TableView
|
||||||
columns={columns}
|
columns={columns}
|
||||||
data={this.state.rows}
|
data={rows}
|
||||||
pageSize={50}
|
pageSize={50}
|
||||||
className="table-condensed"
|
className="table-condensed"
|
||||||
columnsForWrapText={columnsForWrapText}
|
columnsForWrapText={columnsForWrapText}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}, [rows]);
|
||||||
|
|
||||||
renderTriggerNode(): React.ReactNode {
|
const triggerNode = useMemo(
|
||||||
return (
|
() => (
|
||||||
<Tooltip id="difference-tooltip" title={t('Click to see difference')}>
|
<Tooltip id="difference-tooltip" title={t('Click to see difference')}>
|
||||||
<StyledLabel className="label">{t('Altered')}</StyledLabel>
|
<StyledLabel className="label">{t('Altered')}</StyledLabel>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
|
),
|
||||||
|
[],
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
if (!hasDiffs) {
|
||||||
// Return nothing if there are no differences
|
|
||||||
if (!this.state.hasDiffs) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// Render the label-warning 'Altered' tag which the user may
|
|
||||||
// click to open a modal containing a table summarizing the
|
|
||||||
// differences in the slice
|
|
||||||
return (
|
return (
|
||||||
<ModalTrigger
|
<ModalTrigger
|
||||||
triggerNode={this.renderTriggerNode()}
|
triggerNode={triggerNode}
|
||||||
modalTitle={t('Chart changes')}
|
modalTitle={t('Chart changes')}
|
||||||
modalBody={this.renderModalBody()}
|
modalBody={modalBody}
|
||||||
responsive
|
responsive
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
export default AlteredSliceTag;
|
export default AlteredSliceTag;
|
||||||
|
|
Loading…
Reference in New Issue