2021-09-09 08:09:09 -04:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2018-09-05 08:22:52 -04:00
|
|
|
import $ from 'jquery';
|
2020-06-15 08:08:44 -04:00
|
|
|
import mock from 'xhr-mock';
|
2022-05-13 08:08:49 -04:00
|
|
|
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2020-08-17 17:09:56 -04:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { TEST_HOST } from 'spec/test_constants';
|
2019-12-31 13:09:10 -05:00
|
|
|
import PasteMarkdownTable from '~/behaviors/markdown/paste_markdown_table';
|
2021-02-14 13:09:20 -05:00
|
|
|
import dropzoneInput from '~/dropzone_input';
|
2021-09-09 08:09:09 -04:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import httpStatusCodes from '~/lib/utils/http_status';
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-09-10 05:10:51 -04:00
|
|
|
const TEST_FILE = new File([], 'somefile.jpg');
|
|
|
|
TEST_FILE.upload = {};
|
|
|
|
|
2018-09-05 08:22:52 -04:00
|
|
|
const TEST_UPLOAD_PATH = `${TEST_HOST}/upload/file`;
|
|
|
|
const TEST_ERROR_MESSAGE = 'A big error occurred!';
|
2018-10-17 03:13:26 -04:00
|
|
|
const TEMPLATE = `<form class="gfm-form" data-uploads-path="${TEST_UPLOAD_PATH}">
|
2018-09-05 08:22:52 -04:00
|
|
|
<textarea class="js-gfm-input"></textarea>
|
|
|
|
<div class="uploading-error-message"></div>
|
2018-10-17 03:13:26 -04:00
|
|
|
</form>`;
|
2018-09-05 08:22:52 -04:00
|
|
|
|
|
|
|
describe('dropzone_input', () => {
|
2019-11-18 10:06:27 -05:00
|
|
|
it('returns null when failed to initialize', () => {
|
|
|
|
const dropzone = dropzoneInput($('<form class="gfm-form"></form>'));
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
expect(dropzone).toBeNull();
|
|
|
|
});
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
it('returns valid dropzone when successfully initialize', () => {
|
|
|
|
const dropzone = dropzoneInput($(TEMPLATE));
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
expect(dropzone.version).toBeTruthy();
|
2018-09-05 08:22:52 -04:00
|
|
|
});
|
|
|
|
|
2019-12-31 13:09:10 -05:00
|
|
|
describe('handlePaste', () => {
|
2021-11-18 13:14:27 -05:00
|
|
|
let form;
|
|
|
|
|
2021-09-09 08:09:09 -04:00
|
|
|
const triggerPasteEvent = (clipboardData = {}) => {
|
|
|
|
const event = $.Event('paste');
|
|
|
|
const origEvent = new Event('paste');
|
|
|
|
|
|
|
|
origEvent.clipboardData = clipboardData;
|
|
|
|
event.originalEvent = origEvent;
|
|
|
|
|
|
|
|
$('.js-gfm-input').trigger(event);
|
|
|
|
};
|
|
|
|
|
2019-12-31 13:09:10 -05:00
|
|
|
beforeEach(() => {
|
2022-05-13 08:08:49 -04:00
|
|
|
loadHTMLFixture('issues/new-issue.html');
|
2019-12-31 13:09:10 -05:00
|
|
|
|
2021-11-18 13:14:27 -05:00
|
|
|
form = $('#new_issue');
|
2019-12-31 13:09:10 -05:00
|
|
|
form.data('uploads-path', TEST_UPLOAD_PATH);
|
|
|
|
dropzoneInput(form);
|
|
|
|
});
|
|
|
|
|
2021-11-18 13:14:27 -05:00
|
|
|
afterEach(() => {
|
|
|
|
form = null;
|
2022-05-13 08:08:49 -04:00
|
|
|
|
|
|
|
resetHTMLFixture();
|
2021-11-18 13:14:27 -05:00
|
|
|
});
|
|
|
|
|
2019-12-31 13:09:10 -05:00
|
|
|
it('pastes Markdown tables', () => {
|
2021-09-09 08:09:09 -04:00
|
|
|
jest.spyOn(PasteMarkdownTable.prototype, 'isTable');
|
|
|
|
jest.spyOn(PasteMarkdownTable.prototype, 'convertToTableMarkdown');
|
2020-06-15 08:08:44 -04:00
|
|
|
|
2021-09-09 08:09:09 -04:00
|
|
|
triggerPasteEvent({
|
2020-06-15 08:08:44 -04:00
|
|
|
types: ['text/plain', 'text/html'],
|
|
|
|
getData: () => '<table><tr><td>Hello World</td></tr></table>',
|
|
|
|
items: [],
|
2021-09-09 08:09:09 -04:00
|
|
|
});
|
2019-12-31 13:09:10 -05:00
|
|
|
|
2020-01-09 04:07:51 -05:00
|
|
|
expect(PasteMarkdownTable.prototype.isTable).toHaveBeenCalled();
|
2019-12-31 13:09:10 -05:00
|
|
|
expect(PasteMarkdownTable.prototype.convertToTableMarkdown).toHaveBeenCalled();
|
|
|
|
});
|
2021-09-09 08:09:09 -04:00
|
|
|
|
|
|
|
it('passes truncated long filename to post request', async () => {
|
|
|
|
const axiosMock = new MockAdapter(axios);
|
|
|
|
const longFileName = 'a'.repeat(300);
|
|
|
|
|
|
|
|
triggerPasteEvent({
|
|
|
|
types: ['text/plain', 'text/html', 'text/rtf', 'Files'],
|
|
|
|
getData: () => longFileName,
|
2021-10-28 08:10:22 -04:00
|
|
|
files: [new File([new Blob()], longFileName, { type: 'image/png' })],
|
2021-09-09 08:09:09 -04:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
kind: 'file',
|
|
|
|
type: 'image/png',
|
|
|
|
getAsFile: () => new Blob(),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
axiosMock.onPost().reply(httpStatusCodes.OK, { link: { markdown: 'foo' } });
|
|
|
|
await waitForPromises();
|
|
|
|
expect(axiosMock.history.post[0].data.get('file').name).toHaveLength(246);
|
|
|
|
});
|
2021-10-28 08:10:22 -04:00
|
|
|
|
2021-11-18 13:14:27 -05:00
|
|
|
it('disables generated image file when clipboardData have both image and text', () => {
|
|
|
|
const TEST_PLAIN_TEXT = 'This wording is a plain text.';
|
|
|
|
triggerPasteEvent({
|
|
|
|
types: ['text/plain', 'Files'],
|
|
|
|
getData: () => TEST_PLAIN_TEXT,
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
kind: 'text',
|
|
|
|
type: 'text/plain',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
kind: 'file',
|
|
|
|
type: 'image/png',
|
|
|
|
getAsFile: () => new Blob(),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(form.find('.js-gfm-input')[0].value).toBe('');
|
|
|
|
});
|
|
|
|
|
2021-10-28 08:10:22 -04:00
|
|
|
it('display original file name in comment box', async () => {
|
|
|
|
const axiosMock = new MockAdapter(axios);
|
|
|
|
triggerPasteEvent({
|
|
|
|
types: ['Files'],
|
|
|
|
files: [new File([new Blob()], 'test.png', { type: 'image/png' })],
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
kind: 'file',
|
|
|
|
type: 'image/png',
|
|
|
|
getAsFile: () => new Blob(),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
axiosMock.onPost().reply(httpStatusCodes.OK, { link: { markdown: 'foo' } });
|
|
|
|
await waitForPromises();
|
|
|
|
expect(axiosMock.history.post[0].data.get('file').name).toEqual('test.png');
|
|
|
|
});
|
2019-12-31 13:09:10 -05:00
|
|
|
});
|
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
describe('shows error message', () => {
|
|
|
|
let form;
|
|
|
|
let dropzone;
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
beforeEach(() => {
|
2020-06-15 08:08:44 -04:00
|
|
|
mock.setup();
|
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
form = $(TEMPLATE);
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
dropzone = dropzoneInput(form);
|
|
|
|
});
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
afterEach(() => {
|
2020-06-15 08:08:44 -04:00
|
|
|
mock.teardown();
|
2019-11-18 10:06:27 -05:00
|
|
|
});
|
|
|
|
|
2020-06-15 08:08:44 -04:00
|
|
|
beforeEach(() => {});
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2020-06-15 08:08:44 -04:00
|
|
|
it.each`
|
|
|
|
responseType | responseBody
|
|
|
|
${'application/json'} | ${JSON.stringify({ message: TEST_ERROR_MESSAGE })}
|
|
|
|
${'text/plain'} | ${TEST_ERROR_MESSAGE}
|
|
|
|
`('when AJAX fails with json', ({ responseType, responseBody }) => {
|
|
|
|
mock.post(TEST_UPLOAD_PATH, {
|
|
|
|
status: 400,
|
|
|
|
body: responseBody,
|
|
|
|
headers: { 'Content-Type': responseType },
|
|
|
|
});
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
dropzone.processFile(TEST_FILE);
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2020-06-15 08:08:44 -04:00
|
|
|
return waitForPromises().then(() => {
|
|
|
|
expect(form.find('.uploading-error-message').text()).toEqual(TEST_ERROR_MESSAGE);
|
|
|
|
});
|
2019-11-18 10:06:27 -05:00
|
|
|
});
|
2018-09-05 08:22:52 -04:00
|
|
|
});
|
|
|
|
});
|