debian-mirror-gitlab/spec/frontend/ide/components/new_dropdown/upload_spec.js

106 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import Vue from 'vue';
2020-05-24 23:13:21 +05:30
import createComponent from 'helpers/vue_mount_component_helper';
2020-01-01 13:55:28 +05:30
import upload from '~/ide/components/new_dropdown/upload.vue';
2018-05-09 12:01:36 +05:30
describe('new dropdown upload', () => {
let vm;
beforeEach(() => {
const Component = Vue.extend(upload);
vm = createComponent(Component, {
path: '',
});
vm.entryName = 'testing';
2020-05-24 23:13:21 +05:30
jest.spyOn(vm, '$emit');
2018-05-09 12:01:36 +05:30
});
afterEach(() => {
vm.$destroy();
});
2018-11-20 20:47:30 +05:30
describe('openFile', () => {
it('calls for each file', () => {
const files = ['test', 'test2', 'test3'];
2020-05-24 23:13:21 +05:30
jest.spyOn(vm, 'readFile').mockImplementation(() => {});
jest.spyOn(vm.$refs.fileUpload, 'files', 'get').mockReturnValue(files);
2018-11-20 20:47:30 +05:30
vm.openFile();
2020-05-24 23:13:21 +05:30
expect(vm.readFile.mock.calls.length).toBe(3);
2018-11-20 20:47:30 +05:30
files.forEach((file, i) => {
2020-05-24 23:13:21 +05:30
expect(vm.readFile.mock.calls[i]).toEqual([file]);
2018-11-20 20:47:30 +05:30
});
});
});
2018-05-09 12:01:36 +05:30
describe('readFile', () => {
beforeEach(() => {
2020-05-24 23:13:21 +05:30
jest.spyOn(FileReader.prototype, 'readAsDataURL').mockImplementation(() => {});
2018-05-09 12:01:36 +05:30
});
2018-12-13 13:39:08 +05:30
it('calls readAsDataURL for all files', () => {
2018-05-09 12:01:36 +05:30
const file = {
type: 'images/png',
};
vm.readFile(file);
expect(FileReader.prototype.readAsDataURL).toHaveBeenCalledWith(file);
});
});
describe('createFile', () => {
2018-12-13 13:39:08 +05:30
const textTarget = {
result: 'base64,cGxhaW4gdGV4dA==',
2018-05-09 12:01:36 +05:30
};
const binaryTarget = {
2021-01-03 14:25:43 +05:30
result: 'base64,8PDw8A==', // ðððð
2018-12-13 13:39:08 +05:30
};
2020-03-13 15:44:24 +05:30
2021-01-03 14:25:43 +05:30
const textFile = new File(['plain text'], 'textFile');
const binaryFile = new File(['😺'], 'binaryFile');
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
beforeEach(() => {
2020-05-24 23:13:21 +05:30
jest.spyOn(FileReader.prototype, 'readAsText');
2020-03-13 15:44:24 +05:30
});
it('calls readAsText and creates file in plain text (without encoding) if the file content is plain text', done => {
const waitForCreate = new Promise(resolve => vm.$on('create', resolve));
2018-12-13 13:39:08 +05:30
vm.createFile(textTarget, textFile);
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
expect(FileReader.prototype.readAsText).toHaveBeenCalledWith(textFile);
waitForCreate
.then(() => {
expect(vm.$emit).toHaveBeenCalledWith('create', {
name: textFile.name,
type: 'blob',
content: 'plain text',
rawPath: '',
});
})
.then(done)
.catch(done.fail);
2018-05-09 12:01:36 +05:30
});
2021-01-03 14:25:43 +05:30
it('creates a blob URL for the content if binary', () => {
2018-12-13 13:39:08 +05:30
vm.createFile(binaryTarget, binaryFile);
2018-05-09 12:01:36 +05:30
2021-01-03 14:25:43 +05:30
expect(FileReader.prototype.readAsText).not.toHaveBeenCalled();
2020-03-13 15:44:24 +05:30
2018-05-09 12:01:36 +05:30
expect(vm.$emit).toHaveBeenCalledWith('create', {
2018-12-13 13:39:08 +05:30
name: binaryFile.name,
2018-05-09 12:01:36 +05:30
type: 'blob',
2021-01-03 14:25:43 +05:30
content: 'ðððð',
rawPath: 'blob:https://gitlab.com/048c7ac1-98de-4a37-ab1b-0206d0ea7e1b',
2018-05-09 12:01:36 +05:30
});
});
});
});