mirror of
https://github.com/apache/superset.git
synced 2026-04-20 08:34:37 +00:00
fix(table): cross-filtering breaks after renaming column labels via Custom SQL (#38858)
This commit is contained in:
@@ -1787,5 +1787,145 @@ describe('plugin-chart-table', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('should build columnLabelToNameMap for adhoc columns with custom labels', () => {
|
||||
const result = transformProps({
|
||||
...testData.basic,
|
||||
rawFormData: {
|
||||
...testData.basic.rawFormData,
|
||||
query_mode: QueryMode.Aggregate,
|
||||
groupby: [
|
||||
{
|
||||
sqlExpression: 'name',
|
||||
label: 'Name_Renamed',
|
||||
expressionType: 'SQL',
|
||||
},
|
||||
],
|
||||
metrics: ['sum__num'],
|
||||
},
|
||||
emitCrossFilters: true,
|
||||
queriesData: [
|
||||
{
|
||||
...testData.basic.queriesData[0],
|
||||
colnames: ['Name_Renamed', 'sum__num'],
|
||||
coltypes: [GenericDataType.String, GenericDataType.Numeric],
|
||||
data: [{ Name_Renamed: 'Michael', sum__num: 2467063 }],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.columnLabelToNameMap).toEqual({
|
||||
Name_Renamed: 'name',
|
||||
});
|
||||
});
|
||||
|
||||
test('should not populate columnLabelToNameMap for physical columns', () => {
|
||||
const result = transformProps({
|
||||
...testData.basic,
|
||||
rawFormData: {
|
||||
...testData.basic.rawFormData,
|
||||
query_mode: QueryMode.Aggregate,
|
||||
groupby: ['name'],
|
||||
metrics: ['sum__num'],
|
||||
},
|
||||
emitCrossFilters: true,
|
||||
queriesData: [
|
||||
{
|
||||
...testData.basic.queriesData[0],
|
||||
colnames: ['name', 'sum__num'],
|
||||
coltypes: [GenericDataType.String, GenericDataType.Numeric],
|
||||
data: [{ name: 'Michael', sum__num: 2467063 }],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.columnLabelToNameMap).toEqual({});
|
||||
});
|
||||
|
||||
test('should not populate columnLabelToNameMap when adhoc label matches sqlExpression', () => {
|
||||
const result = transformProps({
|
||||
...testData.basic,
|
||||
rawFormData: {
|
||||
...testData.basic.rawFormData,
|
||||
query_mode: QueryMode.Aggregate,
|
||||
groupby: [
|
||||
{
|
||||
sqlExpression: 'name',
|
||||
label: 'name',
|
||||
expressionType: 'SQL',
|
||||
},
|
||||
],
|
||||
metrics: ['sum__num'],
|
||||
},
|
||||
emitCrossFilters: true,
|
||||
queriesData: [
|
||||
{
|
||||
...testData.basic.queriesData[0],
|
||||
colnames: ['name', 'sum__num'],
|
||||
coltypes: [GenericDataType.String, GenericDataType.Numeric],
|
||||
data: [{ name: 'Michael', sum__num: 2467063 }],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.columnLabelToNameMap).toEqual({});
|
||||
});
|
||||
|
||||
test('cross-filter on adhoc column with custom label emits original column name', () => {
|
||||
const setDataMask = jest.fn();
|
||||
const baseProps = transformProps({
|
||||
...testData.basic,
|
||||
rawFormData: {
|
||||
...testData.basic.rawFormData,
|
||||
query_mode: QueryMode.Aggregate,
|
||||
groupby: [
|
||||
{
|
||||
sqlExpression: 'name',
|
||||
label: 'Name_Renamed',
|
||||
expressionType: 'SQL',
|
||||
},
|
||||
],
|
||||
metrics: ['sum__num'],
|
||||
},
|
||||
filterState: { filters: {} },
|
||||
ownState: {},
|
||||
hooks: {
|
||||
onAddFilter: jest.fn(),
|
||||
setDataMask,
|
||||
onContextMenu: jest.fn(),
|
||||
},
|
||||
emitCrossFilters: true,
|
||||
queriesData: [
|
||||
{
|
||||
...testData.basic.queriesData[0],
|
||||
colnames: ['Name_Renamed', 'sum__num'],
|
||||
coltypes: [GenericDataType.String, GenericDataType.Numeric],
|
||||
data: [
|
||||
{ Name_Renamed: 'Michael', sum__num: 2467063 },
|
||||
{ Name_Renamed: 'Joe', sum__num: 2467 },
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
render(
|
||||
<ProviderWrapper>
|
||||
<TableChart {...baseProps} emitCrossFilters sticky={false} />
|
||||
</ProviderWrapper>,
|
||||
);
|
||||
|
||||
// Verify the table rendered with data
|
||||
expect(screen.getByText('Michael')).toBeInTheDocument();
|
||||
|
||||
// Find the td cell containing "Michael" and click it
|
||||
const cell = screen.getByText('Michael').closest('td')!;
|
||||
fireEvent.click(cell);
|
||||
|
||||
expect(setDataMask).toHaveBeenCalled();
|
||||
const lastCall =
|
||||
setDataMask.mock.calls[setDataMask.mock.calls.length - 1][0];
|
||||
const { filters } = lastCall.extraFormData;
|
||||
expect(filters).toHaveLength(1);
|
||||
// Should emit the original column name, not the label
|
||||
expect(filters[0].col).toBe('name');
|
||||
expect(filters[0].val).toEqual(['Michael']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user