I am thinking because of the delay
, your assertion inside of your subscribe
never runs.
Try this to ensure that the assertions inside of the subscribe are running
test('should test the download file', () => {
const fakeurl = 'http://fakeurl';
service.downloadFile(fakeurl).subscribe((resp: IDownload) => {
expect(1).toBe(2); // this test should fail
expect(resp).toBe(mockDownloadedFile);
});
const req = httpMock.expectOne(request => request.url.includes(fakeurl), 'call api');
expect(req.request.method).toBe('GET');
req.flush(new Blob());
subscripton.unsubscribe(); // unusbscribe from previous subscription
});
Ensure the above test fails to ensure that it is actually going inside of the subscribe block for assertions. If it passes, you know it is not going inside of the subscribe block.
For your case, I would take advantage of fakeAsync
to advance time in a fake way:
import { fakeAsync, tick } from "@angular/core/testing";
....
test('should test the download file', fakeAsync(() => { // add fakeAsync here
const fakeurl = 'http://fakeurl';
let response: IDownload;
const subscription = service.downloadFile(fakeurl).subscribe((resp: IDownload) => {
expect(1).toBe(2); // ensure this fails and remove it once you see it failing
response = resp; // assign response here
});
const req = httpMock.expectOne(request => request.url.includes(fakeurl), 'call api');
expect(req.request.method).toBe('GET');
req.flush(new Blob());
tick(3500); // advance the timer by 3500ms so it gets passed the delay
expect(response).toBe(mockDownloadedFile);
}));
Now I think since you're using jest
, you will have an issue of using fakeAsync
. Maybe change the test
to an it
and see if that will work for you.