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

100 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import Vue from 'vue';
import upload from '~/ide/components/new_dropdown/upload.vue';
import createComponent from 'spec/helpers/vue_mount_component_helper';
describe('new dropdown upload', () => {
let vm;
beforeEach(() => {
const Component = Vue.extend(upload);
vm = createComponent(Component, {
path: '',
});
vm.entryName = 'testing';
spyOn(vm, '$emit');
});
afterEach(() => {
vm.$destroy();
});
2018-11-20 20:47:30 +05:30
describe('openFile', () => {
it('calls for each file', () => {
const files = ['test', 'test2', 'test3'];
spyOn(vm, 'readFile');
spyOnProperty(vm.$refs.fileUpload, 'files').and.returnValue(files);
vm.openFile();
expect(vm.readFile.calls.count()).toBe(3);
files.forEach((file, i) => {
expect(vm.readFile.calls.argsFor(i)).toEqual([file]);
});
});
});
2018-05-09 12:01:36 +05:30
describe('readFile', () => {
beforeEach(() => {
spyOn(FileReader.prototype, 'readAsDataURL');
});
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=',
};
const textFile = {
name: 'textFile',
type: 'text/plain',
2018-05-09 12:01:36 +05:30
};
2018-12-13 13:39:08 +05:30
const binaryFile = {
name: 'binaryFile',
type: 'image/png',
2018-05-09 12:01:36 +05:30
};
2018-12-13 13:39:08 +05:30
it('creates file in plain text (without encoding) if the file content is plain text', () => {
vm.createFile(textTarget, textFile);
2018-05-09 12:01:36 +05:30
expect(vm.$emit).toHaveBeenCalledWith('create', {
2018-12-13 13:39:08 +05:30
name: textFile.name,
2018-05-09 12:01:36 +05:30
type: 'blob',
2018-12-13 13:39:08 +05:30
content: 'plain text',
2018-05-09 12:01:36 +05:30
base64: false,
2019-07-31 22:56:46 +05:30
binary: false,
rawPath: '',
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
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],
base64: true,
2019-07-31 22:56:46 +05:30
binary: true,
rawPath: binaryTarget.result,
2018-05-09 12:01:36 +05:30
});
});
});
});