Skip to content

Commit

Permalink
test(igxGrid): adding a custom Sorting Strategy test #2734 (#3096)
Browse files Browse the repository at this point in the history
  • Loading branch information
PavlovVasil authored and kdinev committed Nov 22, 2018
1 parent 5d6df94 commit a658e00
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions projects/igniteui-angular/src/lib/grids/grid/grid.sorting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { IgxGridModule } from './index';
import { GridTemplateStrings, ColumnDefinitions } from '../../test-utils/template-strings.spec';
import { BasicGridComponent } from '../../test-utils/grid-base-components.spec';
import { SampleTestData } from '../../test-utils/sample-test-data.spec';
import { DefaultSortingStrategy } from '../../data-operations/sorting-strategy';
import { DefaultSortingStrategy, ISortingStrategy } from '../../data-operations/sorting-strategy';
import { IgxGridCellComponent } from '../cell.component';
import { configureTestSuite } from '../../test-utils/configure-suite';

const SORTING_ICON_NONE_CONTENT = 'none';
Expand All @@ -22,7 +23,8 @@ describe('IgxGrid - Grid Sorting', () => {

TestBed.configureTestingModule({
declarations: [
GridDeclaredColumnsComponent
GridDeclaredColumnsComponent,
SortByParityComponent
],
imports: [IgxGridModule.forRoot()]
})
Expand Down Expand Up @@ -315,6 +317,30 @@ describe('IgxGrid - Grid Sorting', () => {
fixture.detectChanges();
expect(sortingIcon.nativeElement.textContent.trim()).toEqual(SORTING_ICON_NONE_CONTENT);
});

it(`Should allow sorting using a custom Sorting Strategy.`, () => {
fixture = TestBed.createComponent(SortByParityComponent);
grid = fixture.componentInstance.grid;
fixture.componentInstance.data.push(
{ ID: 8, Name: 'Brad', LastName: 'Walker', Region: 'DD' },
{ ID: 9, Name: 'Mary', LastName: 'Smith', Region: 'OC' },
{ ID: 10, Name: 'Brad', LastName: 'Smith', Region: 'BD' },
);
fixture.detectChanges();
grid.sort({
fieldName: 'ID',
dir: SortingDirection.Desc,
ignoreCase: false,
strategy: new SortByParityComponent()
});
fixture.detectChanges();
const oddHalf: IgxGridCellComponent[] = grid.getColumnByName('ID').cells.slice(0, 5);
const evenHalf: IgxGridCellComponent[] = grid.getColumnByName('ID').cells.slice(5);
const isFirstHalfOdd: boolean = oddHalf.every(cell => cell.value % 2 === 1);
const isSecondHalfEven: boolean = evenHalf.every(cell => cell.value % 2 === 0);
expect(isFirstHalfOdd).toEqual(true);
expect(isSecondHalfEven).toEqual(true);
});
});

@Component({
Expand All @@ -333,6 +359,31 @@ export class GridDeclaredColumnsComponent extends BasicGridComponent {
public width = '800px';
}

@Component({
template: GridTemplateStrings.declareGrid(
'',
'',
ColumnDefinitions.idFirstLastNameSortable)
})
export class SortByParityComponent extends GridDeclaredColumnsComponent implements ISortingStrategy {
public sort(data: any[], fieldName: string, dir: SortingDirection) {
const key = fieldName;
const reverse = (dir === SortingDirection.Desc ? -1 : 1);
const cmpFunc = (obj1, obj2) => {
return this.compareObjects(obj1, obj2, key, reverse);
};
return data.sort(cmpFunc);
}
protected sortByParity(a: any, b: any) {
return a % 2 === 0 ? -1 : b % 2 === 0 ? 1 : 0;
}
protected compareObjects(obj1: object, obj2: object, key: string, reverse: number) {
const a = obj1[key];
const b = obj2[key];
return reverse * this.sortByParity(a, b);
}
}

function getCurrentCellFromGrid(grid, row, cell) {
const gridRow = grid.rowList.toArray()[row];
const gridCell = gridRow.cells.toArray()[cell];
Expand Down

0 comments on commit a658e00

Please sign in to comment.