Skip to content

Commit

Permalink
Add unit tests for hydration cases
Browse files Browse the repository at this point in the history
  • Loading branch information
georgecwan committed Oct 27, 2023
1 parent 9c9d099 commit 4875616
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/iris-grid/src/IrisGridTestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class IrisGridTestUtils {
makeTable({
columns = this.makeColumns(),
size = 1000000000,
sort = [],
sort = [] as Sort[],
layoutHints = {} as LayoutHints,
} = {}): Table {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
185 changes: 150 additions & 35 deletions packages/iris-grid/src/IrisGridUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,12 @@ import IrisGridTestUtils from './IrisGridTestUtils';
import IrisGridUtils, {
DehydratedSort,
LegacyDehydratedSort,
isPanelStateV1,
} from './IrisGridUtils';

const irisGridUtils = new IrisGridUtils(dh);
const irisGridTestUtils = new IrisGridTestUtils(dh);

function makeFilter() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new (dh as any).FilterCondition();
}

function makeColumns(count = 30) {
const columns: Column[] = [];

for (let i = 0; i < count; i += 1) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const column = new (dh as any).Column({ index: i, name: `name_${i}` });
columns.push(column);
}

return columns;
}

function makeTable({
columns = makeColumns(),
sort = [] as Sort[],
} = {}): Table {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new (dh as any).Table({ columns, sort });
}
function makeColumn(index: number): Column {
return irisGridTestUtils.makeColumn(
`${index}`,
Expand All @@ -47,6 +24,16 @@ function makeColumn(index: number): Column {
);
}

function makeTable({
columns = irisGridTestUtils.makeColumns(10, 'name_'),
sort = [] as Sort[],
} = {}): Table {
return irisGridTestUtils.makeTable({
columns,
sort,
});
}

describe('quickfilters tests', () => {
it('exports/imports empty list', () => {
const table = irisGridTestUtils.makeTable();
Expand All @@ -62,10 +49,10 @@ describe('quickfilters tests', () => {
});

it('exports/imports quickFilters', () => {
const table = makeTable();
const table = irisGridTestUtils.makeTable();
const column = 9;
const text = '>1000';
const filter = makeFilter();
const filter = irisGridTestUtils.makeFilter();
const quickFilters = new Map([[column, { text, filter }]]);

const exportedFilters = IrisGridUtils.dehydrateQuickFilters(quickFilters);
Expand Down Expand Up @@ -93,7 +80,7 @@ describe('quickfilters tests', () => {

describe('advanced filter tests', () => {
it('exports/imports empty list', () => {
const table = makeTable();
const table = irisGridTestUtils.makeTable();
const filters = new Map();
const exportedFilters = irisGridUtils.dehydrateAdvancedFilters(
table.columns,
Expand All @@ -112,7 +99,7 @@ describe('advanced filter tests', () => {
it('exports advanced filters', () => {
const table = makeTable();
const column = 7;
const filter = makeFilter();
const filter = irisGridTestUtils.makeFilter();
const options = {
filterItems: [{ selectedType: '', value: '', key: 0 }],
filterOperators: [],
Expand Down Expand Up @@ -151,7 +138,7 @@ describe('advanced filter tests', () => {
describe('sort exporting/importing', () => {
it('exports/imports empty sort', () => {
const sort = [];
const table = makeTable({ sort });
const table = irisGridTestUtils.makeTable({ sort });
const exportedSort = IrisGridUtils.dehydrateSort(sort);
expect(exportedSort).toEqual([]);

Expand All @@ -160,7 +147,7 @@ describe('sort exporting/importing', () => {
});

it('should export (dehydrate) sorts', () => {
const columns = makeColumns();
const columns = irisGridTestUtils.makeColumns(10, 'name_');
const sort = [columns[3].sort(), columns[7].sort().abs().desc()];
const dehydratedSorts = IrisGridUtils.dehydrateSort(sort);

Expand All @@ -171,9 +158,9 @@ describe('sort exporting/importing', () => {
});

describe('should import (hydrate) sorts', () => {
const columns = makeColumns();
const columns = irisGridTestUtils.makeColumns(10, 'name_');
const sort = [columns[3].sort(), columns[7].sort().abs().desc()];
const table = makeTable({ columns, sort });
const table = irisGridTestUtils.makeTable({ columns, sort });

const dehydratedSorts = IrisGridUtils.dehydrateSort(sort);

Expand Down Expand Up @@ -210,7 +197,7 @@ describe('sort exporting/importing', () => {
describe('pendingDataMap hydration/dehydration', () => {
it('dehydrates/hydrates empty map', () => {
const pendingDataMap = new Map();
const columns = makeColumns();
const columns = irisGridTestUtils.makeColumns(10, 'name_');
const dehydratedMap = irisGridUtils.dehydratePendingDataMap(
columns,
pendingDataMap
Expand Down Expand Up @@ -242,7 +229,7 @@ describe('pendingDataMap hydration/dehydration', () => {
},
],
]);
const columns = makeColumns();
const columns = irisGridTestUtils.makeColumns(10, 'name_');
const dehydratedMap = irisGridUtils.dehydratePendingDataMap(
columns,
pendingDataMap
Expand Down Expand Up @@ -488,7 +475,7 @@ describe('validate copy ranges', () => {

describe('changeFilterColumnNamesToIndexes', () => {
const DEFAULT_FILTER = {};
const columns = makeColumns(10);
const columns = irisGridTestUtils.makeColumns(10, 'name_');
it('Replaces column names with indexes', () => {
const filters = [
{ name: 'name_1', filter: DEFAULT_FILTER },
Expand Down Expand Up @@ -680,3 +667,131 @@ describe('dehydration methods', () => {
).toBe(true);
});
});

describe('hydration methods', () => {
it.each([
[
'hydrateIrisGridPanelStateV1',
{
isSelectingPartition: false,
partition: null,
partitionColumn: 'INVALID',
advancedSettings: [],
},
],
[
'hydrateIrisGridPanelStateV2',
{
isSelectingPartition: false,
partitions: [null],
partitionColumns: ['INVALID'],
advancedSettings: [],
},
],
])('%s invalid column error', (_label, panelState) => {
expect(() =>
IrisGridUtils.hydrateIrisGridPanelState(
irisGridTestUtils.makeModel(
irisGridTestUtils.makeTable({
columns: irisGridTestUtils.makeColumns(5, 'name_'),
})
),
panelState
)
).toThrow('Invalid partition column INVALID');
});

it.each([
[
'hydrateIrisGridPanelStateV1 null partition',
{
isSelectingPartition: false,
partition: null,
partitionColumn: 'name_0',
advancedSettings: [],
},
],
[
'hydrateIrisGridPanelStateV1 unselected partition',
{
isSelectingPartition: false,
partition: '',
partitionColumn: 'name_0',
advancedSettings: [],
},
],
[
'hydrateIrisGridPanelStateV1 one selected partition',
{
isSelectingPartition: true,
partition: '',
partitionColumn: 'name_0',
advancedSettings: [],
},
],
[
'hydrateIrisGridPanelStateV2 no partition columns',
{
isSelectingPartition: false,
partitions: [],
partitionColumns: [],
advancedSettings: [],
},
],
[
'hydrateIrisGridPanelStateV2 two unselected columns',
{
isSelectingPartition: true,
partitions: [null, null],
partitionColumns: ['name_0', 'name_1'],
advancedSettings: [],
},
],
[
'hydrateIrisGridPanelStateV2 two selected columns',
{
isSelectingPartition: true,
partitions: ['a', 'b'],
partitionColumns: ['name_0', 'name_1'],
advancedSettings: [],
},
],
[
'hydrateIrisGridPanelStateV2 mixed selection columns',
{
isSelectingPartition: true,
partitions: [null, 'b', null],
partitionColumns: ['name_0', 'name_1', 'name_2'],
advancedSettings: [],
},
],
[
'hydrateIrisGridPanelStateV2 mixed selection columns',
{
isSelectingPartition: true,
partitions: ['a', null, 'b'],
partitionColumns: ['name_0', 'name_1', 'name_2'],
advancedSettings: [],
},
],
])('%s partitions and columns match', (_label, panelState) => {
const result = IrisGridUtils.hydrateIrisGridPanelState(
irisGridTestUtils.makeModel(
irisGridTestUtils.makeTable({
columns: irisGridTestUtils.makeColumns(5, 'name_'),
})
),
panelState
);
expect(result.isSelectingPartition).toBe(panelState.isSelectingPartition);
if (isPanelStateV1(panelState)) {
expect(result.partitions).toEqual([panelState.partition]);
expect(result.partitionColumns[0].name).toBe(panelState.partitionColumn);
} else {
expect(result.partitions).toEqual(panelState.partitions);
panelState.partitionColumns.forEach((partition, index) => {
expect(result.partitionColumns[index].name === partition).toBeTruthy();
});
}
});
});

0 comments on commit 4875616

Please sign in to comment.