Skip to content
Open
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
Expand Up @@ -24,7 +24,7 @@ export const adaptivityColumnsControllerExtender = (
.filter((column) => !hasHiddenWidth(column));
}

public isAdaptiveHiddenColumn(column: Column): boolean {
protected isAdaptiveHiddenColumn(column: Column): boolean {
return super.isAdaptiveHiddenColumn(column)
|| this.adaptiveColumnsController.isColumnHidden(column);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,335 @@
import {
afterEach, beforeEach, describe, expect, it,
} from '@jest/globals';
import type { Response as SendRequestResult } from '@js/common/ai-integration';
import type { Properties as DataGridProperties } from '@js/ui/data_grid';
import { AIIntegration } from '@ts/core/ai_integration/core/ai_integration';
import type { ColumnsController } from '@ts/grids/grid_core/columns_controller/columns_controller';
import type { Column } from '@ts/grids/grid_core/columns_controller/types';

import {
afterTest,
beforeTest,
createDataGrid,
} from '../../__tests__/__mock__/helpers/utils';

type ColumnOptions = NonNullable<DataGridProperties['columns']>[number];

const SELECTION: DataGridProperties = { selection: { mode: 'multiple', showCheckBoxesMode: 'always' } };
const EDITING: DataGridProperties = { editing: { allowUpdating: true } };

const AI_COLUMN: ColumnOptions = {
type: 'ai',
name: 'ai',
caption: 'AI',
ai: {
aiIntegration: new AIIntegration({
sendRequest: (): SendRequestResult => ({ promise: Promise.resolve('{}'), abort: (): void => {} }),
}),
},
};

const getColumnsController = async (options: DataGridProperties): Promise<ColumnsController> => {
const { instance } = await createDataGrid({ dataSource: [], ...options });

return instance.getController('columns');
};

const findByCommand = (columns: Column[], command: string): Column | undefined => columns
.find((column) => column.command === command);

const getNames = (columns: Column[]): (string | undefined)[] => columns
.map(({ dataField, command }) => dataField ?? command);

describe('getVisibleColumns', () => {
beforeEach(beforeTest);
afterEach(afterTest);

describe('with command columns', () => {
it('should return copies of the grid columns', async () => {
const columnsController = await getColumnsController({ columns: ['a', 'b'] });
const columns = columnsController.getColumns();

const visibleColumns = columnsController.getVisibleColumns();

expect(visibleColumns).toEqual(columns);
expect(visibleColumns[0]).not.toBe(columns[0]);
expect(visibleColumns[1]).not.toBe(columns[1]);
});

it('should add a command column', async () => {
const columnsController = await getColumnsController({ columns: ['a'], ...SELECTION });

const visibleColumns = columnsController.getVisibleColumns();

expect(getNames(visibleColumns)).toEqual(['select', 'a']);
expect(findByCommand(visibleColumns, 'select')?.fixed).toBe(false);
});

it('should not add command columns when the grid has no columns', async () => {
const columnsController = await getColumnsController({ columns: [], ...SELECTION });

expect(columnsController.getVisibleColumns()).toEqual([]);
});

it.each<[string, DataGridProperties]>([
['column fixing is enabled', { columns: ['a'], columnFixing: { enabled: true } }],
['a column is fixed', { columns: [{ dataField: 'a', fixed: true }] }],
])('should fix a command column when %s', async (_, options) => {
const columnsController = await getColumnsController({ ...options, ...SELECTION });

expect(findByCommand(columnsController.getVisibleColumns(), 'select')?.fixed).toBe(true);
});

it('should keep the fixed option of a command column', async () => {
const columnsController = await getColumnsController({
columns: ['a', AI_COLUMN],
columnFixing: { enabled: true },
});

expect(findByCommand(columnsController.getVisibleColumns(), 'ai')?.fixed).toBe(false);
});

it('should merge a column into the command column of its type', async () => {
const columnsController = await getColumnsController({
columns: ['a', { type: 'buttons', caption: 'Actions' }],
...EDITING,
});

const [, column] = columnsController.getVisibleColumns();

expect(column).toMatchObject({
type: 'buttons', command: 'edit', caption: 'Actions', width: 'auto', fixed: false,
});
});

it('should not change the grid column it merges', async () => {
const columnsController = await getColumnsController({
columns: ['a', { type: 'buttons', cssClass: 'custom' }],
...EDITING,
});

columnsController.getVisibleColumns();

const [, column] = columnsController.getColumns();
expect(column.command).toBeUndefined();
expect(column.cssClass).toBe('custom');
});

it('should let the column options override the command column options', async () => {
const columnsController = await getColumnsController({
columns: ['a', { type: 'buttons', width: 100, fixed: true }],
...EDITING,
});

const column = findByCommand(columnsController.getVisibleColumns(), 'edit');

expect(column?.width).toBe(100);
expect(column?.fixed).toBe(true);
});

it('should keep calculateCellValue of the command column', async () => {
const columnsController = await getColumnsController({ columns: ['a', AI_COLUMN] });
const commandColumn = columnsController.columnOption('command:ai');

const column = findByCommand(columnsController.getVisibleColumns(), 'ai');

const [, gridColumn] = columnsController.getColumns();
expect(column?.calculateCellValue).toBe(commandColumn?.calculateCellValue);
expect(column?.calculateCellValue).not.toBe(gridColumn.calculateCellValue);
});

it('should keep calculateCellValue of the column when the command column has none', async () => {
const columnsController = await getColumnsController({
columns: ['a', { type: 'buttons' }],
...EDITING,
});

const column = findByCommand(columnsController.getVisibleColumns(), 'edit');

expect(column?.calculateCellValue).toBe(columnsController.getColumns()[1].calculateCellValue);
});

it.each([
['both columns have one', 'custom', 'dx-command-edit custom'],
['only the command column has one', undefined, 'dx-command-edit'],
])('should join the css classes when %s', async (_, cssClass, expected) => {
const columnsController = await getColumnsController({
columns: ['a', { type: 'buttons', cssClass }],
...EDITING,
});

expect(findByCommand(columnsController.getVisibleColumns(), 'edit')?.cssClass).toBe(expected);
});

it('should add only the command columns that no column customizes', async () => {
const columnsController = await getColumnsController({
columns: ['a', { type: 'buttons', caption: 'Actions' }],
...EDITING,
...SELECTION,
});

expect(getNames(columnsController.getVisibleColumns())).toEqual(['select', 'a', 'edit']);
});
});

describe('with grouping', () => {
const DATA_SOURCE = [{
id: 1, a: 1, b: 2, c: 3,
}];
const GROUPED_COLUMNS: ColumnOptions[] = ['a', { dataField: 'b', groupIndex: 0 }, { dataField: 'c', groupIndex: 1 }];

it('should apply the options of a groupExpand column to every group column', async () => {
const columnsController = await getColumnsController({
dataSource: DATA_SOURCE,
columns: [...GROUPED_COLUMNS, {
type: 'groupExpand', caption: 'Group', cssClass: 'custom', width: 50,
}],
});
const options = {
type: 'groupExpand', command: 'expand', caption: 'Group', cssClass: 'custom', width: 50,
};

const [, firstGroupColumn, secondGroupColumn] = columnsController.getVisibleColumns();

expect(firstGroupColumn).toMatchObject(options);
expect(secondGroupColumn).toMatchObject(options);
});

it('should keep the position of each group column', async () => {
const columnsController = await getColumnsController({
dataSource: DATA_SOURCE,
columns: [...GROUPED_COLUMNS, { type: 'groupExpand', width: 50 }],
});
const [, b, c] = columnsController.getColumns();

const [, firstGroupColumn, secondGroupColumn] = columnsController.getVisibleColumns();

expect(firstGroupColumn).toMatchObject({
dataField: 'b', index: b.index, visibleIndex: b.visibleIndex, headerId: b.headerId, groupIndex: 0,
});
expect(secondGroupColumn).toMatchObject({
dataField: 'c', index: c.index, visibleIndex: c.visibleIndex, headerId: c.headerId, groupIndex: 1,
});
});

it('should allow fixing and reordering only for the first group column', async () => {
const columnsController = await getColumnsController({
dataSource: DATA_SOURCE,
columns: [...GROUPED_COLUMNS, { type: 'groupExpand' }],
});

const [, firstGroupColumn, secondGroupColumn] = columnsController.getVisibleColumns();

expect(firstGroupColumn).toMatchObject({ allowFixing: true, allowReordering: true });
expect(secondGroupColumn).toMatchObject({ allowFixing: false, allowReordering: false });
});

it('should put the group columns customized by a groupExpand column with the fixed columns', async () => {
const columnsController = await getColumnsController({
dataSource: DATA_SOURCE,
columns: ['a', { dataField: 'b', groupIndex: 0 }, { type: 'groupExpand', caption: 'Group' }],
columnFixing: { enabled: true },
});

const visibleColumns = columnsController.getVisibleColumns();

expect(visibleColumns.map(({ type, dataField }) => type ?? dataField)).toEqual(['groupExpand', 'a']);
expect(visibleColumns[0].fixed).toBe(true);
});

it('should not add the grid columns to the group columns', async () => {
const columnsController = await getColumnsController({
dataSource: DATA_SOURCE,
columns: ['a', { dataField: 'b', groupIndex: 0 }],
});

const visibleColumns = columnsController.getVisibleColumns();

expect(visibleColumns.map(({ type, dataField }) => type ?? dataField)).toEqual(['groupExpand', 'a']);
});
});

it('should let a detailExpand column override every option of the detail expand column', async () => {
const columnsController = await getColumnsController({
dataSource: [{ id: 1, a: 1 }],
columns: ['a', { type: 'detailExpand', width: 40 }],
masterDetail: { enabled: true },
});
const [, column] = columnsController.getColumns();

const detailExpandColumn = columnsController.getVisibleColumns()
.find(({ type }) => type === 'detailExpand');

expect(detailExpandColumn).toMatchObject({
command: 'expand', width: 40, index: column.index, visibleIndex: column.visibleIndex,
});
});
});

describe('moveColumn', () => {
beforeEach(beforeTest);
afterEach(afterTest);

const moveColumn = async (
options: DataGridProperties,
fromVisibleIndex: number,
toVisibleIndex: number,
): Promise<number[]> => {
const columnsController = await getColumnsController(options);

columnsController.moveColumn(fromVisibleIndex, toVisibleIndex, 'headers', 'headers');

return columnsController.getColumns().map(({ visibleIndex }) => visibleIndex as number);
};

it('should move a column to the place of a target column with the same fixing', async () => {
const [, b, c] = await moveColumn({ columns: [{ dataField: 'a', fixed: true }, 'b', 'c'] }, 2, 1);

expect(c).toBeLessThan(b);
});

it('should move a column to the end when the target column is fixed and the column is not', async () => {
const [, b, c] = await moveColumn({ columns: [{ dataField: 'a', fixed: true }, 'b', 'c'] }, 1, 0);

expect(b).toBeGreaterThan(c);
});

it('should not treat a sticky column as fixed', async () => {
const [a, , c] = await moveColumn({
columns: [{ dataField: 'a', fixed: true, fixedPosition: 'sticky' }, 'b', 'c'],
}, 2, 0);

expect(c).toBeLessThan(a);
});

it.each<[string, ColumnOptions]>([
['a data column', 'c'],
['an AI column', AI_COLUMN],
])('should ignore the column fixing of the grid for %s', async (_, target) => {
const [a, b, c] = await moveColumn({ columns: ['a', 'b', target], columnFixing: { enabled: true } }, 0, 2);

expect(a).toBeGreaterThan(b);
expect(a).toBeLessThan(c);
});

it('should treat a command column as fixed when the column fixing of the grid is enabled', async () => {
const [a, , buttons] = await moveColumn({
columns: ['a', 'b', { type: 'buttons' }],
columnFixing: { enabled: true },
...EDITING,
}, 0, 2);

expect(a).toBeGreaterThan(buttons);
});

it('should treat a command column as not fixed when the column fixing of the grid is disabled', async () => {
const [a, b, buttons] = await moveColumn({
columns: ['a', 'b', { type: 'buttons' }],
...EDITING,
}, 0, 2);

expect(a).toBeGreaterThan(b);
expect(a).toBeLessThan(buttons);
});
});
Loading
Loading