首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角8测试-不使用状态存储的Devextreme ()

角8测试-不使用状态存储的Devextreme ()
EN

Stack Overflow用户
提问于 2019-07-09 06:11:13
回答 2查看 1.5K关注 0票数 0

我正在编写一个角8测试,以检查dx-data-grid是否正确地列出和呈现数据。下面是我的设置大致的样子:

代码语言:javascript
复制
<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>

类型标

代码语言:javascript
复制
export class MyComponent {
    @Input() data;
}

测试

代码语言:javascript
复制
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));
    });

});

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

代码语言:javascript
复制
TypeError: Cannot read property 'load' of undefined

但是,我们从模板中删除以下html:

代码语言:javascript
复制
<dxo-state-storing [enabled]="true" type="localStorage" storageKey="storage-key"></dxo-state-storing>

...the考试合格!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-19 06:55:59

dx-数据网格小部件从localStorage在setTimeout中加载状态。尝试使用fakeAsync()和抽签()来模拟测试中的时间。

票数 1
EN

Stack Overflow用户

发布于 2019-07-18 09:29:34

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

代码语言:javascript
复制
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));
});

所以,当我想要加载它的数据时,网格似乎没有被完全初始化。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56946270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档