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

109 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 = {
2018-12-13 13:39:08 +05:30
result: 'base64,w4I=',
};
2020-03-13 15:44:24 +05:30
const textFile = new File(['plain text'], 'textFile');
2018-12-13 13:39:08 +05:30
const binaryFile = {
name: 'binaryFile',
type: 'image/png',
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
});
it('splits content on base64 if binary', () => {
2018-12-13 13:39:08 +05:30
vm.createFile(binaryTarget, binaryFile);
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
expect(FileReader.prototype.readAsText).not.toHaveBeenCalledWith(textFile);
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',
content: binaryTarget.result.split('base64,')[1],
2019-07-31 22:56:46 +05:30
rawPath: binaryTarget.result,
2018-05-09 12:01:36 +05:30
});
});
});
});