How to create an ArrayBuffer Variable in Angular unit test, Jasmine/Karma

dota2pro

Hi i have a function that downloads an excel file coming from backend

component.ts

 getExcelExport(resultsFilterRootObject: ResultsFilterRootObject) {
    return this.http.post(urls.getExcelExportCPPMetrics , resultsFilterRootObject, {
      responseType: 'arraybuffer',
      observe: 'response'
    })
      .pipe(
        tap(
          data => {
            const blob = new Blob([data.body], {type: 'application/vnd.ms-excel'});
            const filename = 'vehicle-metrics-template.xls';
            FileSaver.saveAs(blob, filename);
          },
          catchError(MetricsService.handleError)
        )
      );
  }

component.spec.ts

  it('should download Excel ', () => {

  //  const expectedResult: ArrayBuffer = new ArrayBuffer(8); Tried this fails too
    const expectedResult = new TextEncoder();
    expectedResult.encode("This is a string converted to a Uint8Array");

    httpClientSpy.post.and.returnValue(asyncData(expectedResult));

    metricsService.getExcelExportCPPMetrics(resultsFilterRootObject).subscribe(
      heroes => expect(heroes).toEqual(expectedResult, 'expected VehicleSalesResultRootObject'),
      fail
    );
    expect(httpClientSpy.post.calls.count()).toBe(1, 'one call');
  });

I keep getting error error TS2345: Argument of type 'TextEncoder' is not assignable to parameter of type 'Expected<HttpResponse<ArrayBuffer>>'.

Type 'TextEncoder' is missing the following properties from type 'ObjectContaining<HttpResponse<ArrayBuffer>>': jasmineMatches, jasmineToString

Basically if I can create a variable of type ArrayBuffer in the Unit Test this problem would be solved

any idea on this ?

Buczkowski

Note that post method with params responseType: 'arraybuffer' and observe: 'response' returns value Observable<HttpResponse<ArrayBuffer>> which is not directly ArrayBuffer as provided there:

post(url: string, body: any | null, options: {
    headers?: HttpHeaders | {
        [header: string]: string | string[];
    };
    observe: 'response';
    params?: HttpParams | {
        [param: string]: string | string[];
    };
    reportProgress?: boolean;
    responseType: 'arraybuffer';
    withCredentials?: boolean;
}): Observable<HttpResponse<ArrayBuffer>>;

What you can do is return Observable with simple object that has property which are you using - body:

  it('should download Excel ', () => {
    const expectedResult: ArrayBuffer = new ArrayBuffer(8);
    // httpClientSpy.post.and.returnValue(asyncData(expectedResult));
    httpClientSpy.post.and.returnValue(of({body: expectedResult})); // Or that below one if "asyncData" return Observable

    metricsService.getExcelExportCPPMetrics(resultsFilterRootObject).subscribe(
      data => expect(data.body).toEqual(expectedResult, 'expected VehicleSalesResultRootObject'),
      fail
    );
    expect(httpClientSpy.post.calls.count()).toBe(1, 'one call');
  });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive