2018-09-05 08:22:52 -04:00
|
|
|
import $ from 'jquery';
|
|
|
|
import { TEST_HOST } from 'spec/test_constants';
|
2019-12-09 07:07:58 -05:00
|
|
|
import dropzoneInput from '~/dropzone_input';
|
2019-12-31 13:09:10 -05:00
|
|
|
import PasteMarkdownTable from '~/behaviors/markdown/paste_markdown_table';
|
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', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
loadFixtures('issues/new-issue.html');
|
|
|
|
|
|
|
|
const form = $('#new_issue');
|
|
|
|
form.data('uploads-path', TEST_UPLOAD_PATH);
|
|
|
|
dropzoneInput(form);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('pastes Markdown tables', () => {
|
|
|
|
const event = $.Event('paste');
|
|
|
|
const origEvent = new Event('paste');
|
|
|
|
const pasteData = new DataTransfer();
|
2020-01-09 04:07:51 -05:00
|
|
|
pasteData.setData('text/plain', 'Hello World');
|
|
|
|
pasteData.setData('text/html', '<table><tr><td>Hello World</td></tr></table>');
|
2019-12-31 13:09:10 -05:00
|
|
|
origEvent.clipboardData = pasteData;
|
|
|
|
event.originalEvent = origEvent;
|
|
|
|
|
2020-01-09 04:07:51 -05:00
|
|
|
spyOn(PasteMarkdownTable.prototype, 'isTable').and.callThrough();
|
2019-12-31 13:09:10 -05:00
|
|
|
spyOn(PasteMarkdownTable.prototype, 'convertToTableMarkdown').and.callThrough();
|
|
|
|
|
|
|
|
$('.js-gfm-input').trigger(event);
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
describe('shows error message', () => {
|
|
|
|
let form;
|
|
|
|
let dropzone;
|
|
|
|
let xhr;
|
|
|
|
let oldXMLHttpRequest;
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
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
|
|
|
xhr = jasmine.createSpyObj(Object.keys(XMLHttpRequest.prototype));
|
|
|
|
oldXMLHttpRequest = window.XMLHttpRequest;
|
|
|
|
window.XMLHttpRequest = () => xhr;
|
|
|
|
});
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
afterEach(() => {
|
|
|
|
window.XMLHttpRequest = oldXMLHttpRequest;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when AJAX fails with json', () => {
|
|
|
|
xhr = {
|
|
|
|
...xhr,
|
|
|
|
statusCode: 400,
|
|
|
|
readyState: 4,
|
|
|
|
responseText: JSON.stringify({ message: TEST_ERROR_MESSAGE }),
|
|
|
|
getResponseHeader: () => 'application/json',
|
|
|
|
};
|
|
|
|
|
|
|
|
dropzone.processFile(TEST_FILE);
|
|
|
|
|
|
|
|
xhr.onload();
|
|
|
|
|
|
|
|
expect(form.find('.uploading-error-message').text()).toEqual(TEST_ERROR_MESSAGE);
|
|
|
|
});
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
it('when AJAX fails with text', () => {
|
|
|
|
xhr = {
|
|
|
|
...xhr,
|
|
|
|
statusCode: 400,
|
|
|
|
readyState: 4,
|
|
|
|
responseText: TEST_ERROR_MESSAGE,
|
|
|
|
getResponseHeader: () => 'text/plain',
|
|
|
|
};
|
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
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
xhr.onload();
|
2018-09-05 08:22:52 -04:00
|
|
|
|
2019-11-18 10:06:27 -05:00
|
|
|
expect(form.find('.uploading-error-message').text()).toEqual(TEST_ERROR_MESSAGE);
|
|
|
|
});
|
2018-09-05 08:22:52 -04:00
|
|
|
});
|
|
|
|
});
|