Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed sorting of dates with null values #1634

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {DateTimeInput} from '@gravity-ui/date-utils';
import {dateTimeUtc} from '@gravity-ui/date-utils';
import type {ColumnDef, SortingFnOption} from '@tanstack/react-table';
import {createColumnHelper} from '@tanstack/react-table';
Expand All @@ -23,17 +24,17 @@ function getSortingFunction(args: {
const columnType: TableCommonCell['type'] = get(th, 'type');
if (columnType === 'date') {
return function (row1, row2) {
const cell1Value = String(row1.original[columnIndex].value);
const cell2Value = String(row2.original[columnIndex].value);
const cell1Value = row1.original[columnIndex].value as DateTimeInput;
const cell2Value = row2.original[columnIndex].value as DateTimeInput;

const date1 = dateTimeUtc({input: cell1Value});
const date2 = dateTimeUtc({input: cell2Value});

if (date1.isValid() && date1.isAfter(date2)) {
if (date1 > date2 || (date1.isValid() && !date2.isValid())) {
return 1;
}

if (date2.isValid() && date2.isAfter(date1)) {
if (date2 > date1 || (date2.isValid() && !date1.isValid())) {
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,68 @@ datalensTest.describe('Wizard', () => {
];
expect(await wizardPage.chartkit.getRowsTexts()).toEqual(expectedOrder);
});

datalensTest('Sorting dates with null values', async ({page}) => {
const wizardPage = new WizardPage({page});
const chartContainer = page.locator(slct(WizardPageQa.SectionPreview));
const previewLoader = chartContainer.locator(slct(ChartKitQa.Loader));

await wizardPage.createNewFieldWithFormula('year', 'year([Order_date])');
await wizardPage.sectionVisualization.addFieldByClick(PlaceholderName.Filters, 'year');
await wizardPage.filterEditor.selectFilterOperation(Operations.EQ);
await wizardPage.filterEditor.setInputValue('2015');
await wizardPage.filterEditor.apply();

await wizardPage.createNewFieldWithFormula('month', 'datetrunc([Order_date], "month")');
await wizardPage.sectionVisualization.addFieldByClick(
PlaceholderName.FlatTableColumns,
'month',
);

await wizardPage.createNewFieldWithFormula(
'with_null',
'if(month([Order_date]) = 2) then null else datetrunc([month], "month") end',
);
await wizardPage.sectionVisualization.addFieldByClick(
PlaceholderName.FlatTableColumns,
'with_null',
);

await expect(previewLoader).not.toBeVisible();

const initialOrder = [
['01.01.2015', '01.01.2015'],
['01.02.2015', 'null'],
['01.03.2015', '01.03.2015'],
['01.04.2015', '01.04.2015'],
['01.05.2015', '01.05.2015'],
['01.06.2015', '01.06.2015'],
['01.07.2015', '01.07.2015'],
['01.08.2015', '01.08.2015'],
['01.09.2015', '01.09.2015'],
['01.10.2015', '01.10.2015'],
['01.11.2015', '01.11.2015'],
['01.12.2015', '01.12.2015'],
];
expect(await wizardPage.chartkit.getRowsTexts()).toEqual(initialOrder);

// Sort values by clicking on header
await chartContainer.locator('thead', {hasText: 'with_null'}).first().click();
const expectedOrder = [
['01.12.2015', '01.12.2015'],
['01.11.2015', '01.11.2015'],
['01.10.2015', '01.10.2015'],
['01.09.2015', '01.09.2015'],
['01.08.2015', '01.08.2015'],
['01.07.2015', '01.07.2015'],
['01.06.2015', '01.06.2015'],
['01.05.2015', '01.05.2015'],
['01.04.2015', '01.04.2015'],
['01.03.2015', '01.03.2015'],
['01.01.2015', '01.01.2015'],
['01.02.2015', 'null'],
];
expect(await wizardPage.chartkit.getRowsTexts()).toEqual(expectedOrder);
});
});
});
Loading