Eriendel
Eriendel

Reputation: 979

How to mock File in javascript?

I'm developing some small project to exercise my TDD skills. The project consists of an audio player which has the ability to drag'n'drop files in a playlist. I'm using Jasmine as a testing framework. The problem I faced is that I can't mock javascript files to test my file upload functionality. I tried to create a File like this:

new File(new Blob(), "name");

but Chrome does not allow creating files manually. File's constructor is illegal to use. I found a solution with grunt.js which consists of returning some files from grunt, but I don't really wanna use server-side for such a small test project. Is there any workaround for this problem?

Upvotes: 65

Views: 77869

Answers (4)

SamMorrowDrums
SamMorrowDrums

Reputation: 572

You don't need to create a blob, you can do this which applies the genuine image string directly (I used this converter), or you can do follow the example below (if you don't actually care about having a valid image]):

<img id="test" />
const img = window.btoa("I don't care about a broken image");
document.getElementById('test').src = 'data:image/png;base64,' + img;

The btoa function is just to create base64 from a string.

Upvotes: 1

Holtwick
Holtwick

Reputation: 1966

In order to get the arrayBuffer working, I faked it this way in my tests:


class File extends Blob {
  name: string
  constructor(a: any, name: string) {
    super(a)
    this.name = name
  }
}

// @ts-ignore
globalThis.File = File

Upvotes: 0

Bob
Bob

Reputation: 1095

Here's how I create a dummy pdf file to use with pdf.js

  1. Create a base64 string of your file. I use base64 command in my shell.

    base64 < src/mockData/dummy.pdf
    #outputs something like: VEhJUyBJUyBUSEUgQU5TV0VSCg==
    
  2. Copy this string into your source code.

    const dataBase64 = "VEhJUyBJUyBUSEUgQU5TV0VSCg==";
    //note this not a pdf, but for demo purposes because a pdf file is too big
    
  3. Decode the base64 and create a file

    const arrayBuffer = Uint8Array.from(window.atob(dataBase64), c => c.charCodeAt(0));
    const file = new File([arrayBuffer], "dummy.pdf", {type: 'application/pdf'});
    

Upvotes: 6

Chris Weber
Chris Weber

Reputation: 5836

Chrome will let you create a new file:

var f = new File([""], "filename", { type: 'text/html' });

However IE11 (and other browsers?) will not.

Here's is my (poor?) fake File:

var blob = new Blob([""], { type: 'text/html' });
blob["lastModifiedDate"] = "";
blob["name"] = "filename";
var fakeF = blob;

You can fill in the values as you see fit. You can fill the blob with whatever you need. (See the other answer for how to use an image).

I've tested this in IE11, Chrome and Firefox. So far I seems to work, at least for my unit testing purposes.

Bonus: Here it is in typescript:

let blob = new Blob([""], { type: 'text/html' });
blob["lastModifiedDate"] = "";
blob["name"] = "filename";

let fakeF = <File>blob;

Upvotes: 114

Related Questions