Skip to content

Commit 8cf9644

Browse files
committed
test(Grid): conditional rowStyles and rowClasses #9969
1 parent 77a5993 commit 8cf9644

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

projects/igniteui-angular/src/lib/grids/grid/grid-row-pinning.spec.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ import { IPinningConfig } from '../grid.common';
1010
import { SampleTestData } from '../../test-utils/sample-test-data.spec';
1111
import { GridFunctions } from '../../test-utils/grid-functions.spec';
1212
import { SortingDirection } from '../../data-operations/sorting-expression.interface';
13-
import { IgxGridTransaction } from '../tree-grid/public_api';
14-
import { IgxTransactionService } from '../../services/public_api';
1513
import { GridSummaryFunctions } from '../../test-utils/grid-functions.spec';
1614
import { IgxStringFilteringOperand } from '../../data-operations/filtering-condition';
1715
import { IgxPaginatorComponent } from '../../paginator/paginator.component';
1816
import { wait, UIInteractions } from '../../test-utils/ui-interactions.spec';
1917
import { setupGridScrollDetection } from '../../test-utils/helper-utils.spec';
18+
import { GridRowConditionalStylingComponent } from '../../test-utils/grid-base-components.spec';
2019

2120
describe('Row Pinning #grid', () => {
2221
const FIXED_ROW_CONTAINER = '.igx-grid__tr--pinned ';
@@ -33,7 +32,8 @@ describe('Row Pinning #grid', () => {
3332
GridRowPinningWithMRLComponent,
3433
GridRowPinningWithMDVComponent,
3534
GridRowPinningWithTransactionsComponent,
36-
GridRowPinningWithInitialPinningComponent
35+
GridRowPinningWithInitialPinningComponent,
36+
GridRowConditionalStylingComponent
3737
],
3838
imports: [
3939
NoopAnimationsModule,
@@ -1265,6 +1265,50 @@ describe('Row Pinning #grid', () => {
12651265
expect(grid.hasPinnedRecords).toBeTrue();
12661266
});
12671267
});
1268+
1269+
describe('Conditional row styling', () => {
1270+
1271+
beforeEach(fakeAsync(() => {
1272+
fix = TestBed.createComponent(GridRowConditionalStylingComponent);
1273+
fix.detectChanges();
1274+
grid = fix.componentInstance.grid;
1275+
tick();
1276+
fix.detectChanges();
1277+
}));
1278+
1279+
it('Shoud be able to conditionally style rows. Check is the class present in the row native element class list', () => {
1280+
fix.detectChanges();
1281+
const firstRow = grid.gridAPI.get_row_by_index(0);
1282+
const fourthRow = grid.gridAPI.get_row_by_index(3);
1283+
1284+
expect(firstRow).toBeDefined();
1285+
expect(firstRow.element.nativeElement.classList.contains('eventRow')).toBeTrue();
1286+
expect(firstRow.element.nativeElement.classList.contains('oddRow')).toBeFalse();
1287+
expect(fourthRow.element.nativeElement.classList.contains('eventRow')).toBeFalse();
1288+
expect(fourthRow.element.nativeElement.classList.contains('oddRow')).toBeTrue();
1289+
});
1290+
1291+
it('Should apply custom CSS bindings to the grid cells/rows. Check the style attribute to match each binding', () => {
1292+
const evenColStyles = {
1293+
background: (row) => row.index % 2 === 0 ? 'gray' : 'white',
1294+
animation: '0.75s popin'
1295+
};
1296+
1297+
fix.detectChanges();
1298+
grid.rowStyles = evenColStyles;
1299+
grid.notifyChanges(true);
1300+
fix.detectChanges();
1301+
const firstRow = grid.gridAPI.get_row_by_index(0);
1302+
const fourthRow = grid.gridAPI.get_row_by_index(3);
1303+
1304+
const expectedEvenStyles = 'background: gray; animation: 0.75s ease 0s 1 normal none running popin;';
1305+
const expectedOddStyles = 'background: white; animation: 0.75s ease 0s 1 normal none running popin;';
1306+
1307+
expect(firstRow.element.nativeElement.style.cssText).toEqual(expectedEvenStyles);
1308+
expect(fourthRow.element.nativeElement.style.cssText).toEqual(expectedOddStyles);
1309+
});
1310+
1311+
});
12681312
});
12691313

12701314
@Component({

projects/igniteui-angular/src/lib/test-utils/grid-base-components.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ export class GridWithToolbarComponent extends GridWithSizeComponent {
118118
public data = SampleTestData.contactInfoData();
119119
}
120120

121+
@Component({
122+
template: GridTemplateStrings.declareGrid(` [autoGenerate]="true" [rowClasses]="rowClasses"`,
123+
'', '')
124+
})
125+
export class GridRowConditionalStylingComponent extends GridWithSizeComponent {
126+
127+
public data = SampleTestData.contactInfoData();
128+
public evenRowCondition = (row) => row.index % 2 === 0;
129+
public oddRowCondition = (row) => row.index % 2 !== 0;
130+
131+
// eslint-disable-next-line @typescript-eslint/member-ordering
132+
public rowClasses = {
133+
eventRow: this.evenRowCondition,
134+
oddRow: this.oddRowCondition
135+
};
136+
}
121137
@Component({
122138
template: `<div>
123139
<igx-column-actions igxColumnHiding [columns]="grid.columns" *ngIf="showInline" [hideFilter]="hideFilter"></igx-column-actions>

0 commit comments

Comments
 (0)