【问题标题】:Angular 8 Testing - Devextreme Grid getDataSource() not Working with State StoringAngular 8 测试 - Devextreme Grid getDataSource() 不使用状态存储
【发布时间】:2020-04-16 01:33:23
【问题描述】:

我正在编写一个 Angular 8 测试来检查 dx-data-grid 是否正确列出并呈现数据。这是我的设置大致如下所示:

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>

打字稿

export class MyComponent {
    @Input() data;
}

测试

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));
    });

});

上面显示的测试失败并出现以下错误

TypeError: Cannot read property 'load' of undefined

但是,当从模板中删除以下 html 时:

<dxo-state-storing [enabled]="true" type="localStorage" storageKey="storage-key"></dxo-state-storing>

...测试通过!

【问题讨论】:

    标签: angular devexpress karma-jasmine devextreme angular-test


    【解决方案1】:

    dx-data-grid 小部件在 setTimeout 中从 localStorage 加载状态。尝试使用 fakeAsync() 和 tick() 来模拟测试中的时间。

    【讨论】:

      【解决方案2】:

      我基本上找到了解决方法,但我对此并不满意:

      it('should list all items', async () => {
          component.data= mockData;
          fixture.detectChanges();
      
          // workaround
          await new Promise( resolve => setTimeout( () => resolve(), 0 ) );
          // ---
      
          const gridElement = fixture.debugElement.query(By.css('#grid'));
          const dxGrid = <DxDataGridComponent>gridElement.componentInstance;
          dxGrid.instance.option('loadingTimeout', undefined);
          dxGrid.instance.getDataSource().load();
      
          const dataSource = dxGrid.dataSource;
          expect(dataSource.length).toBe(mockData.length);
          mockData.forEach(i => expect(dataSource).toContain(i));
      });
      

      看来,当我想加载它的数据时,网格并没有完全初始化。

      【讨论】:

      • 这个解决方案似乎不再适用于 Angular 9.1.x。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多