Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating new File in Typescript returns empty type even if type was specified

I try to generate a mocked File for an Angular unit test and I face some problems.

Here is my code, on top of spec file:

let content = "Hello Zip";
let data = new Blob([content], { type: 'application/zip' });
let arrayOfBlob = new Array<Blob>();
arrayOfBlob.push(data);
let applicationZip = new File(arrayOfBlob, "Mock.zip");

If I try to do:

console.log(applicationZip);

Here is the result:

lastModified: 1492785142174

lastModifiedDate: Fri Apr 21 2017 10:32:22 GMT-0400

name:"Mock.zip"

size:9

type: ""

webkitRelativePath:""

proto: File

On of the method I want to create a test for must check the validity of file's mime type.

With my code, the type is always null. I tried to set it to plain/text for example and it doesn't change anything.

So I guess my File mock creation is somehow incorrect but I can't figure the error.

like image 236
BlackHoleGalaxy Avatar asked Apr 21 '17 14:04

BlackHoleGalaxy


1 Answers

Specify the type as part of the File constructor. File accepts a third parameter where you can specify the type:

let applicationZip = new File(arrayOfBlob, "Mock.zip", { type: 'application/zip' });
like image 191
Saravana Avatar answered Nov 15 '22 06:11

Saravana