|
| 1 | +import { Component, ViewChild } from '@angular/core'; |
| 2 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | + |
| 4 | +import { ClientSideRowModelModule } from '@ag-grid-community/client-side-row-model'; |
| 5 | +import { GridApi, GridOptions, GridReadyEvent, Module } from '@ag-grid-community/core'; |
| 6 | +import { AgGridAngular } from '@ag-grid-community/angular'; |
| 7 | + |
| 8 | +@Component({ |
| 9 | + selector: 'app-grid-wrapper', |
| 10 | + standalone: true, |
| 11 | + imports: [AgGridAngular], |
| 12 | + template: `<ag-grid-angular |
| 13 | + [gridOptions]="gridOptions" |
| 14 | + [columnDefs]="columnDefs" |
| 15 | + [rowData]="rowData" |
| 16 | + [modules]="modules" |
| 17 | + (gridReady)="onGridReady($event)" |
| 18 | + [suppressBrowserResizeObserver]="suppressBrowserResizeObserver" |
| 19 | + (firstDataRendered)="onFirstDataRendered($event)"></ag-grid-angular>`, |
| 20 | +}) |
| 21 | +export class GridWrapperComponent { |
| 22 | + modules: Module[] = [ClientSideRowModelModule]; |
| 23 | + rowData: any[] | null = null; |
| 24 | + columnDefs = [{ field: 'make' }, { field: 'model' }, { field: 'price' }]; |
| 25 | + |
| 26 | + gridOptions: GridOptions = {}; |
| 27 | + gridApi!: GridApi; |
| 28 | + |
| 29 | + suppressBrowserResizeObserver = false; |
| 30 | + |
| 31 | + @ViewChild(AgGridAngular) agGrid!: AgGridAngular; |
| 32 | + |
| 33 | + onGridReady(params: GridReadyEvent) { |
| 34 | + this.gridApi = params.api; |
| 35 | + // this.gridApi.setGridOption('rowData', [{ make: 'Toyota', model: 'Celica', price: 35000 }]); |
| 36 | + this.rowData = [{ make: 'Toyota', model: 'Celica', price: 35000 }]; |
| 37 | + } |
| 38 | + onFirstDataRendered(params: any) {} |
| 39 | +} |
| 40 | + |
| 41 | +describe('Grid OnReady', () => { |
| 42 | + let component: GridWrapperComponent; |
| 43 | + let fixture: ComponentFixture<GridWrapperComponent>; |
| 44 | + |
| 45 | + beforeEach(async () => { |
| 46 | + await TestBed.configureTestingModule({ |
| 47 | + imports: [GridWrapperComponent, AgGridAngular], |
| 48 | + }).compileComponents(); |
| 49 | + |
| 50 | + fixture = TestBed.createComponent(GridWrapperComponent); |
| 51 | + component = fixture.componentInstance; |
| 52 | + }); |
| 53 | + |
| 54 | + it('gridReady is completed by the time a timeout finishes', (done) => { |
| 55 | + fixture.detectChanges(); |
| 56 | + setTimeout(() => { |
| 57 | + expect(component.gridApi).toBeDefined(); |
| 58 | + done(); |
| 59 | + }, 0); |
| 60 | + }); |
| 61 | + |
| 62 | + const runGridReadyTest = async () => { |
| 63 | + spyOn(component, 'onGridReady').and.callThrough(); |
| 64 | + spyOn(component, 'onFirstDataRendered').and.callThrough(); |
| 65 | + |
| 66 | + fixture.detectChanges(); |
| 67 | + await fixture.whenStable(); |
| 68 | + |
| 69 | + expect(component.gridApi).toBeDefined(); |
| 70 | + |
| 71 | + fixture.detectChanges(); // force rowData binding to be applied |
| 72 | + await fixture.whenStable(); |
| 73 | + |
| 74 | + // If calling the gridApi.setRowData we don't need to call the fixture.detectChanges() |
| 75 | + |
| 76 | + expect(component.onGridReady).toHaveBeenCalled(); |
| 77 | + expect(component.onFirstDataRendered).toHaveBeenCalled(); |
| 78 | + }; |
| 79 | + |
| 80 | + it('Fixture goes stable and calls gridReady', async () => { |
| 81 | + await runGridReadyTest(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('Fixture goes stable even with suppressBrowserResizeObserver= true', async () => { |
| 85 | + // Test with the fallback polling to mimic Jest not supporting ResizeObserver |
| 86 | + // We must have the polling run outside of the Angular zone |
| 87 | + component.suppressBrowserResizeObserver = true; |
| 88 | + |
| 89 | + await runGridReadyTest(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('Grid Ready run Auto', async () => { |
| 93 | + spyOn(component, 'onGridReady').and.callThrough(); |
| 94 | + spyOn(component, 'onFirstDataRendered').and.callThrough(); |
| 95 | + fixture.autoDetectChanges(); |
| 96 | + await fixture.whenStable(); |
| 97 | + |
| 98 | + expect(component.gridApi).toBeDefined(); |
| 99 | + |
| 100 | + expect(component.onGridReady).toHaveBeenCalled(); |
| 101 | + expect(component.onFirstDataRendered).toHaveBeenCalled(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments