Skip to content

Instantly share code, notes, and snippets.

@Sebdevar
Forked from josephhanson/MockFile.js
Last active October 19, 2020 20:32
Show Gist options
  • Save Sebdevar/acd395e5c62f065cb48028a6cfd7018b to your computer and use it in GitHub Desktop.
Save Sebdevar/acd395e5c62f065cb48028a6cfd7018b to your computer and use it in GitHub Desktop.
Test Utility function that creates a mock file with specified name, size and MIMETYPE
/**
* Creates a file containing the requested parameters.
*
* @param {string} name The name of the file, including the extension.
* @param {number} size The file's size in bytes.
* @param {string} mimeType The file's MIMETYPE, @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types}
*/
export const createMockFile = (name?: string, size?: number, mimeType?: string): File => {
const fileName = name || 'mock.txt';
const fileSize = size || 1024;
const fileMimeType = mimeType || 'plain/txt';
let fileBits = '';
for (let i = 0; i < fileSize; i += 1) {
fileBits += 'a';
}
return new File([fileBits], fileName, { type: fileMimeType });
};
describe("Mock file for file upload testing", function () {
it("should have default values", function() {
const file = createMockFile();
expect(file.name).toBe('mock.txt');
expect(file.size).toBe(1024);
expect(file.type).toBe('plain/txt');
});
it("should have specified values", function () {
const fileName = 'pic.jpg';
const fileSize = 1024 * 1024 * 2;
const fileType = 'image/jpeg';
const file = createMockFile(fileName, fileSize, fileType);
expect(file.name).toBe(fileName);
expect(file.size).toBe(fileSize);
expect(file.type).toBe(fileType);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment