小编Nea*_*sis的帖子

Angular 测试:模拟包含 blob 的 HttpResponse

我正在开发 Angular 9 应用程序,并且正在编写单元测试,这要归功于 jest。我的 Angular 服务调用 Spring API,该 API 返回 CSV 文件以供下载。我想进行单元测试,但无法模拟 HttpResponse。

* MyService.ts *

downloadCsv(id: string) {
    return this.http.get('/api/ + id + `/csv/`,
      { responseType: 'blob' as 'json', observe: 'response' });
  }
Run Code Online (Sandbox Code Playgroud)

* MyService.spec.ts *

  it(`should call the GET API and return a csv file as result`, () => {
    // GIVEN 
    const expectedData: Blob = new Blob(['a', 'b', 'c', 'd']);

    // WHEN
    let actualData = {};
    service.downloadCsv('1').subscribe(data => {
      actualData = data;
    });

    // THEN
    backendMock.expectOne((req: …
Run Code Online (Sandbox Code Playgroud)

unit-testing blob httpresponse jestjs angular

5
推荐指数
1
解决办法
1万
查看次数

Angular PrimeNg TreeNode:将类转换为TreeNode,无法读取未定义的属性映射

我正在开发一个由 JHipster 生成并使用 Angular 4.3 的应用程序。我正在尝试使用PrimeNG 的树组件

我正在尝试将对象数组转换为 TreeNode 数组,以便像树一样显示。

我的打字稿模型如下所示:

export class Continent implements BaseEntity {
    constructor(
        public id?: number,
        public name?: string,
        public countries?: Country[]
) { }
Run Code Online (Sandbox Code Playgroud)

我遵循了这个主题,它描述了如何转换接口(但在我的例子中我有类)并且我有这些函数(哪里有错误):

private continentsToTreeNodes(continents: Continent[]) {
    for (let cont of continents) {
        this.continentsNodes.push(this.continentToTreeNode(cont));
    }
}

private continentToTreeNode(cont: Continent): TreeNode {
    return {
        label: cont.name,
        data: cont,
        children: cont.countries.map(this.continentToTreeNode) // error at this line : cannot read property map of undefined
    };
}
Run Code Online (Sandbox Code Playgroud)

这些函数在我的组件初始化时执行:

export class MyComponent implements OnInit …
Run Code Online (Sandbox Code Playgroud)

treenode typescript jhipster primeng angular

1
推荐指数
1
解决办法
8565
查看次数