2022-11-25 23:54:43 +05:30
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import Upload from '~/ide/components/new_dropdown/upload.vue';
|
2023-07-09 08:55:56 +05:30
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
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
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
function createComponent() {
|
2022-11-25 23:54:43 +05:30
|
|
|
wrapper = mount(Upload, {
|
|
|
|
propsData: {
|
|
|
|
path: '',
|
|
|
|
},
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
2023-07-09 08:55:56 +05:30
|
|
|
}
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
const uploadFile = (file) => {
|
|
|
|
const input = wrapper.find('input[type="file"]');
|
|
|
|
Object.defineProperty(input.element, 'files', { value: [file] });
|
|
|
|
input.trigger('change', file);
|
|
|
|
};
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
const waitForFileToLoad = async () => {
|
|
|
|
await waitForPromises();
|
|
|
|
return waitForPromises();
|
|
|
|
};
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
createComponent();
|
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',
|
|
|
|
};
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
uploadFile(file);
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
expect(FileReader.prototype.readAsDataURL).toHaveBeenCalledWith(file);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createFile', () => {
|
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 () => {
|
2023-07-09 08:55:56 +05:30
|
|
|
uploadFile(textFile);
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
// Text file has an additional load, so need to wait twice
|
|
|
|
await waitForFileToLoad();
|
|
|
|
await waitForFileToLoad();
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(FileReader.prototype.readAsText).toHaveBeenCalledWith(textFile);
|
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
|
|
|
});
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
it('creates a blob URL for the content if binary', async () => {
|
|
|
|
uploadFile(binaryFile);
|
|
|
|
|
|
|
|
await waitForFileToLoad();
|
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',
|
2023-07-09 08:55:56 +05:30
|
|
|
content: 'ðº', // '😺'
|
2022-11-25 23:54:43 +05:30
|
|
|
rawPath: 'blob:https://gitlab.com/048c7ac1-98de-4a37-ab1b-0206d0ea7e1b',
|
|
|
|
mimeType: 'test/mime-binary',
|
|
|
|
},
|
|
|
|
]);
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|