Je veux écrire un test unitaire pour une méthode de téléchargement de photos. Mais j'obtiens le message Failed: this.task.snapshotChanges(...).pipe is not a function TypeError: this.task.snapshotChanges(...).pipe is not a function
Erreur.
Pour simplifier cette question, j'ai regroupé tout le code dans une seule méthode :
Composant
public startUpload(event: FileList) {
const file: File = event.item(0);
const pathRef = `users/${this.uid}`;
this.task = this.service.uploadPhoto(pathRef, file);
this.fileRef = this.service.getFileReference(pathRef);
this.percentage = this.task.percentageChanges();
this.snapshot = this.task.snapshotChanges();
this.task.snapshotChanges().pipe(last(), switchMap(() => // it fails here - need to propperly mock this
this.fileRef.getDownloadURL()))
.subscribe(url => this.service.updatePhoto(url));
}
Composant.spec
it('should upload file', async(() => {
const supportedFile = new File([''], 'filename.png', {type: 'image/', lastModified: 2233});
const fileList = {
item: () => {
return supportedFile;
}
};
const spy = (<jasmine.Spy>serviceStub.uploadPhoto).and.returnValue({
percentageChanges: () => of(null),
snapshotChanges: () => {
return {
getDownloadURL() {
return of(null);
}
};
}
});
component.startUpload(<any>fileList);
expect(spy).toHaveBeenCalledWith(`users/${component.uid}`, supportedFile);
}));