2022-11-25 23:54:43 +05:30
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import Upload from '~/ide/components/new_dropdown/upload.vue';
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
describe('new dropdown upload', () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
let wrapper;
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper = mount(Upload, {
|
|
|
|
propsData: {
|
|
|
|
path: '',
|
|
|
|
},
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
describe('openFile', () => {
|
|
|
|
it('calls for each file', () => {
|
|
|
|
const files = ['test', 'test2', 'test3'];
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
jest.spyOn(wrapper.vm, 'readFile').mockImplementation(() => {});
|
|
|
|
jest.spyOn(wrapper.vm.$refs.fileUpload, 'files', 'get').mockReturnValue(files);
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper.vm.openFile();
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.vm.readFile.mock.calls.length).toBe(3);
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
files.forEach((file, i) => {
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.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',
|
|
|
|
};
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper.vm.readFile(file);
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
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-03-08 18:12:59 +05:30
|
|
|
const textFile = new File(['plain text'], 'textFile', { type: 'test/mime-text' });
|
|
|
|
const binaryFile = new File(['😺'], 'binaryFile', { type: 'test/mime-binary' });
|
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
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('calls readAsText and creates file in plain text (without encoding) if the file content is plain text', async () => {
|
2022-07-16 23:28:13 +05:30
|
|
|
const waitForCreate = new Promise((resolve) => {
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper.vm.$on('create', resolve);
|
2022-07-16 23:28:13 +05:30
|
|
|
});
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper.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);
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
await waitForCreate;
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.emitted('create')[0]).toStrictEqual([
|
|
|
|
{
|
|
|
|
name: textFile.name,
|
|
|
|
type: 'blob',
|
|
|
|
content: 'plain text',
|
|
|
|
rawPath: '',
|
|
|
|
mimeType: 'test/mime-text',
|
|
|
|
},
|
|
|
|
]);
|
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', () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper.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
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.emitted('create')[0]).toStrictEqual([
|
|
|
|
{
|
|
|
|
name: binaryFile.name,
|
|
|
|
type: 'blob',
|
|
|
|
content: 'ðððð',
|
|
|
|
rawPath: 'blob:https://gitlab.com/048c7ac1-98de-4a37-ab1b-0206d0ea7e1b',
|
|
|
|
mimeType: 'test/mime-binary',
|
|
|
|
},
|
|
|
|
]);
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|