Search code examples
angularunit-testingjasmineform-data

Angular 12 unit test: Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'


I'm trying to write a unit test for the component method, but I'm getting this error:

TypeError: Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'.

I conldn't find an answer that worked for me.

Method:

  public changeProfilePicture(image: File) {
    let formData: FormData = new FormData();
    formData.append('file', image, image.name);
    this.store.dispatch(ProfilePictureActions.update({ formData, userId: this.userId }));
  }

Unit test:

  it('changed profile picture', () => {
    const mockStoreSpy = spyOn(mockStore, 'dispatch');

    const blobImage: Blob = new Blob(['image'] , { type: 'image/jpg' });
    const image: File = { ...blobImage, name: 'pictureName.jpg', lastModified: 1 };
    let formData: FormData = new FormData();
    formData.append('file', <Blob>image, image.name);

    component.changeProfilePicture(image);
    expect(mockStoreSpy).toHaveBeenCalledWith(ProfilePictureActions.update({ formData, userId }));
  });

Solution

  • This solved my problem.

    const image: File = new File([blobImage], 'pictureName.jpg', { type: 'image/jpg' });