Hi i have a function that downloads an excel file ing from backend
ponent.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)
)
);
}
ponent.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 ?
Hi i have a function that downloads an excel file ing from backend
ponent.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)
)
);
}
ponent.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 ?
Share Improve this question edited May 3, 2019 at 20:07 dota2pro asked May 3, 2019 at 19:56 dota2prodota2pro 7,8748 gold badges51 silver badges87 bronze badges1 Answer
Reset to default 9Note 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');
});