Skip to content

Commit 5ef02d0

Browse files
skrustevmddragnev
andauthored
fix(grid): Fix state persistance timing issue for columns initialization. (#17434)
* fix(grid): Fix state persistance timing issue for columns initialization. * chore(*): Update failing tests to tick after render. --------- Co-authored-by: Martin Dragnev <37667452+mddragnev@users.noreply.github.com>
1 parent 01c77fa commit 5ef02d0

2 files changed

Lines changed: 31 additions & 13 deletions

File tree

projects/igniteui-angular/grids/core/src/state-base.directive.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { IgxColumnLayoutComponent } from './columns/column-layout.component';
88
import { IPivotConfiguration, IPivotDimension } from './pivot-grid.interface';
99
import { PivotUtil } from './pivot-util';
1010
import { IgxPivotDateDimension } from './pivot-grid-dimensions';
11+
import { first } from 'rxjs/operators';
1112

1213
export interface IGridState {
1314
columns?: IColumnState[];
@@ -279,10 +280,22 @@ export class IgxGridStateBaseDirective {
279280
newColumns.push(ref);
280281
}
281282
});
282-
context.currGrid.updateColumns(newColumns);
283-
newColumns.forEach(col => {
284-
(context.currGrid as any).columnInit.emit(col);
285-
});
283+
284+
if (!context.currGrid._init && !context.currGrid._rendered) {
285+
// If grid is not rendered but is initialized (during ngAfterViewInit) wait for it to render.
286+
// Otherwise pushing mid detect change (mainly while calculating sizes from `calculateGridWidth` and `calculateGridSizes`) messes up headers detect changing.
287+
context.currGrid.rendered.pipe(first()).subscribe(() => {
288+
context.currGrid.updateColumns(newColumns);
289+
newColumns.forEach(col => {
290+
(context.currGrid as any).columnInit.emit(col);
291+
});
292+
});
293+
} else {
294+
context.currGrid.updateColumns(newColumns);
295+
newColumns.forEach(col => {
296+
(context.currGrid as any).columnInit.emit(col);
297+
});
298+
}
286299
}
287300
},
288301
groupBy: {

projects/igniteui-angular/grids/core/src/state.directive.spec.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TestBed, waitForAsync } from '@angular/core/testing';
1+
import { TestBed, waitForAsync, fakeAsync, tick } from '@angular/core/testing';
22
import { Component, TemplateRef, ViewChild } from '@angular/core';
33
import { SampleTestData } from '../../../test-utils/sample-test-data.spec';
44
import { IgxGridStateDirective } from './state.directive';
@@ -375,9 +375,10 @@ describe('IgxGridState - input properties #grid', () => {
375375
expect(gridState).toBe(columnsState);
376376
});
377377

378-
it('setState should correctly restore grid columns state from object', () => {
378+
it('setState should correctly restore grid columns state from object', fakeAsync(() => {
379379
const fix = TestBed.createComponent(IgxGridStateComponent);
380380
fix.detectChanges();
381+
tick();
381382
const state = fix.componentInstance.state;
382383
const grid = fix.componentInstance.grid;
383384
spyOn(grid.columnInit, 'emit').and.callThrough();
@@ -394,7 +395,7 @@ describe('IgxGridState - input properties #grid', () => {
394395
gridState = state.getState(true, 'columns');
395396
expect(gridState).toBe(columnsState);
396397
expect(grid.columnInit.emit).toHaveBeenCalledTimes(columnsStateObject.columns.length);
397-
});
398+
}));
398399

399400
it('setState should correctly restore grid columns state properties: collapsible and expanded', () => {
400401
const fix = TestBed.createComponent(CollapsibleColumnGroupTestComponent);
@@ -421,9 +422,10 @@ describe('IgxGridState - input properties #grid', () => {
421422
expect(addressInfoGroup.expanded).toBe(false);
422423
});
423424

424-
it('setState should correctly restore grid columns with Column Groups and same headers', () => {
425+
it('setState should correctly restore grid columns with Column Groups and same headers', fakeAsync(() => {
425426
const fix = TestBed.createComponent(IgxGridStateComponent);
426427
fix.detectChanges();
428+
tick();
427429
const state = fix.componentInstance.state;
428430
const initialState = '{"columns":[{"pinned":true,"sortable":true,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"testCss","headerGroupClasses":"","maxWidth":"300px","groupable":false,"hidden":false,"dataType":"number","hasSummary":false,"field":"ProductID","width":"150px","header":"Product ID","resizable":true,"searchable":false,"key":"ProductID","columnGroup":false,"disableHiding":false,"disablePinning":false},{"pinned":false,"sortable":true,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","maxWidth":"300px","groupable":true,"hidden":false,"dataType":"string","hasSummary":false,"field":"ProductName","width":"150px","header":"Product Name","resizable":true,"searchable":true,"selectable":false,"key":"ProductName","columnGroup":false,"disableHiding":false,"disablePinning":false},{"pinned":false,"sortable":false,"filterable":true,"editable":true,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","maxWidth":"300px","groupable":false,"hidden":false,"dataType":"boolean","hasSummary":true,"field":"InStock","width":"140px","header":"In Stock","resizable":true,"searchable":true,"key":"InStock","columnGroup":false,"disableHiding":false,"disablePinning":true},{"pinned":false,"sortable":true,"filterable":false,"editable":true,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","maxWidth":"300px","groupable":true,"hidden":false,"dataType":"date","hasSummary":false,"field":"OrderDate","width":"110px","header":"Date ordered","resizable":false,"searchable":true,"key":"OrderDate","columnGroup":false,"disableHiding":false,"disablePinning":false}]}';
429431
const columnsState = '{"columns":[{"pinned":false,"sortable":false,"filterable":false,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"testCss","headerGroupClasses":"","maxWidth":"300px","groupable":false,"hidden":false,"dataType":"string","hasSummary":false,"field":"ProductID","width":"150px","header":"General Information","resizable":true,"searchable":true,"key":"ProductID","columnGroup":false,"disableHiding":false,"disablePinning":false},{"pinned":false,"sortable":false,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","maxWidth":"300px","groupable":true,"hidden":false,"dataType":"string","hasSummary":false,"field":"","width":"398px","header":"General Information","resizable":false,"searchable":true,"selectable":true,"key":"ProductName_UnitsInStock","columnGroup":true,"disableHiding":false,"disablePinning":false,"collapsible":false,"expanded":true},{"pinned":false,"sortable":true,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","maxWidth":"300px","groupable":false,"hidden":false,"dataType":"boolean","hasSummary":false,"field":"ProductName","width":"199px","header":"","resizable":true,"searchable":true,"selectable":true,"key":"ProductName","parentKey":"ProductName_UnitsInStock","columnGroup":false,"disableHiding":false,"disablePinning":false},{"pinned":false,"sortable":true,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","groupable":false,"hidden":false,"dataType":"string","hasSummary":false,"field":"UnitsInStock","width":"199px","header":"","resizable":true,"searchable":true,"selectable":true,"key":"UnitsInStock","parentKey":"ProductName_UnitsInStock","columnGroup":false,"disableHiding":false,"disablePinning":false},{"pinned":false,"sortable":true,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","maxWidth":"300px","groupable":false,"hidden":false,"dataType":"string","hasSummary":false,"field":"InStock","width":"199px","header":"","resizable":true,"searchable":true,"selectable":true,"key":"InStock","columnGroup":false,"disableHiding":false,"disablePinning":true}]}';
@@ -437,11 +439,12 @@ describe('IgxGridState - input properties #grid', () => {
437439

438440
gridState = state.getState(false, 'columns') as IGridState;
439441
HelperFunctions.verifyColumns(columnsStateObject.columns, gridState);
440-
});
442+
}));
441443

442-
it('setState should reuse columns with matching keys and create new ones for the rest.', () => {
444+
it('setState should reuse columns with matching keys and create new ones for the rest.', fakeAsync(() => {
443445
const fix = TestBed.createComponent(IgxGridStateComponent);
444446
fix.detectChanges();
447+
tick();
445448
const state = fix.componentInstance.state;
446449
const grid = fix.componentInstance.grid;
447450
const originalColumns = [...grid.columns];
@@ -490,7 +493,7 @@ describe('IgxGridState - input properties #grid', () => {
490493
expect(x.bodyTemplate).toBe(fix.componentInstance.template);
491494
});
492495
expect(grid.columns[grid.columns.length - 1 ].field).toBe("AnotherColumn");
493-
});
496+
}));
494497

495498
it('setState should correctly restore grid paging state from string', () => {
496499
const fix = TestBed.createComponent(IgxGridStateComponent);
@@ -766,9 +769,11 @@ describe('IgxGridState - input properties #grid', () => {
766769
expect(gridState).toBe(expansionState);
767770
});
768771

769-
it('should correctly restore mrl column states.', () => {
772+
it('should correctly restore mrl column states.', fakeAsync(() => {
770773
const fix = TestBed.createComponent(IgxGridMRLStateComponent);
771774
fix.detectChanges();
775+
tick();
776+
772777
const grid = fix.componentInstance.grid;
773778
const state = fix.componentInstance.state;
774779

@@ -801,7 +806,7 @@ describe('IgxGridState - input properties #grid', () => {
801806
expect(prodIdColumn.rowEnd).toBe(4);
802807
expect(prodIdColumn.colStart).toBe(1);
803808
expect(prodIdColumn.colEnd).toBe(1);
804-
});
809+
}));
805810

806811
it('getState should not mutate live sorting expressions (strategy/owner)', () => {
807812
const fix = TestBed.createComponent(IgxGridStateComponent);

0 commit comments

Comments
 (0)