I am writing an Angular 8 test that checks if a dx-data-grid
correctly lists and renders the data. Here is what my setup roughly looks like:
HTML
<dx-data-grid id="grid" [dataSource]="data">
<dxo-state-storing [enabled]="true" type="localStorage" storageKey="storage-key"></dxo-state-storing>
<dxi-column dataField="field1"></dxi-column>
<dxi-column dataField="field2"></dxi-column>
</dx-data-grid>
Typescript
export class MyComponent {
@Input() data;
}
Test
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({ /* ... */ }).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should list all items', () => {
component.data= mockData;
fixture.detectChanges();
const gridElement = fixture.debugElement.query(
By.css('#grid')
);
const dxGrid = <DxDataGridComponent>gridElement.componentInstance;
dxGrid.instance.option('loadingTimeout', undefined);
// Here is where the error occurs
// dxGrid.instance.getDataSource() is undefined
dxGrid.instance.getDataSource().load();
const dataSource = dxGrid.dataSource;
expect(dataSource.length).toBe(mockData.length);
mockData.forEach(i => expect(dataSource).toContain(i));
});
});
The test shown above fails with the following error
TypeError: Cannot read property 'load' of undefined
However, wehen removing the following html from the template:
<dxo-state-storing [enabled]="true" type="localStorage" storageKey="storage-key"></dxo-state-storing>
...the test passes!
The dx-data-grid widget loads state from localStorage in setTimeout. Try to use fakeAsync() and tick() to simulate time in the test.