feat: show search filtered total (#36083)

This commit is contained in:
Kaito Watanabe
2025-12-01 16:29:23 -05:00
committed by GitHub
parent db995ad5bf
commit 6e0960c3f5
5 changed files with 185 additions and 21 deletions

View File

@@ -17,7 +17,13 @@
* under the License.
*/
import '@testing-library/jest-dom';
import { render, screen } from '@superset-ui/core/spec';
import {
render,
screen,
fireEvent,
waitFor,
within,
} from '@superset-ui/core/spec';
import { cloneDeep } from 'lodash';
import TableChart, { sanitizeHeaderId } from '../src/TableChart';
import transformProps from '../src/transformProps';
@@ -1085,6 +1091,52 @@ describe('plugin-chart-table', () => {
'rgba(172, 225, 196, 1)',
);
});
it('recalculates totals when user filters data', async () => {
const formDataWithTotals = {
...testData.basic.formData,
show_totals: true,
include_search: true,
server_pagination: false,
metrics: ['sum__num'],
};
const data = testData.basic.queriesData[0].data;
const totalBeforeFilter = data.reduce(
(sum, row) => sum + Number(row.sum__num || 0),
0,
);
const totalAfterFilter =
data.find(item => item.name === 'Michael')?.sum__num || 0;
const props = transformProps({
...testData.basic,
formData: formDataWithTotals,
});
props.totals = { sum__num: totalBeforeFilter };
props.includeSearch = true;
render(
<ProviderWrapper>
<TableChart {...props} sticky={false} />
</ProviderWrapper>,
);
const table = screen.getByRole('table');
const totalCellBefore = within(table).getByText(
String(totalBeforeFilter),
);
expect(totalCellBefore).toBeInTheDocument();
const searchInput = screen.getByRole('textbox');
fireEvent.change(searchInput, { target: { value: 'Michael' } });
await waitFor(() => {
const totalCellAfter = within(table).getByText(
String(totalAfterFilter),
);
expect(totalCellAfter).toBeInTheDocument();
});
});
});
});
});