debian-mirror-gitlab/spec/javascripts/dropzone_input_spec.js

110 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-11-20 20:47:30 +05:30
import $ from 'jquery';
import { TEST_HOST } from 'spec/test_constants';
2020-01-01 13:55:28 +05:30
import dropzoneInput from '~/dropzone_input';
2020-03-09 13:42:32 +05:30
import PasteMarkdownTable from '~/behaviors/markdown/paste_markdown_table';
2018-11-20 20:47:30 +05:30
2019-12-04 20:38:33 +05:30
const TEST_FILE = new File([], 'somefile.jpg');
TEST_FILE.upload = {};
2018-11-20 20:47:30 +05:30
const TEST_UPLOAD_PATH = `${TEST_HOST}/upload/file`;
const TEST_ERROR_MESSAGE = 'A big error occurred!';
2018-12-13 13:39:08 +05:30
const TEMPLATE = `<form class="gfm-form" data-uploads-path="${TEST_UPLOAD_PATH}">
2018-11-20 20:47:30 +05:30
<textarea class="js-gfm-input"></textarea>
<div class="uploading-error-message"></div>
2018-12-13 13:39:08 +05:30
</form>`;
2018-11-20 20:47:30 +05:30
describe('dropzone_input', () => {
2019-12-26 22:10:19 +05:30
it('returns null when failed to initialize', () => {
const dropzone = dropzoneInput($('<form class="gfm-form"></form>'));
2018-11-20 20:47:30 +05:30
2019-12-26 22:10:19 +05:30
expect(dropzone).toBeNull();
});
2018-11-20 20:47:30 +05:30
2019-12-26 22:10:19 +05:30
it('returns valid dropzone when successfully initialize', () => {
const dropzone = dropzoneInput($(TEMPLATE));
2018-11-20 20:47:30 +05:30
2019-12-26 22:10:19 +05:30
expect(dropzone.version).toBeTruthy();
2018-11-20 20:47:30 +05:30
});
2020-03-09 13:42:32 +05:30
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();
pasteData.setData('text/plain', 'Hello World');
pasteData.setData('text/html', '<table><tr><td>Hello World</td></tr></table>');
origEvent.clipboardData = pasteData;
event.originalEvent = origEvent;
spyOn(PasteMarkdownTable.prototype, 'isTable').and.callThrough();
spyOn(PasteMarkdownTable.prototype, 'convertToTableMarkdown').and.callThrough();
$('.js-gfm-input').trigger(event);
expect(PasteMarkdownTable.prototype.isTable).toHaveBeenCalled();
expect(PasteMarkdownTable.prototype.convertToTableMarkdown).toHaveBeenCalled();
});
});
2019-12-26 22:10:19 +05:30
describe('shows error message', () => {
let form;
let dropzone;
let xhr;
let oldXMLHttpRequest;
2018-11-20 20:47:30 +05:30
2019-12-26 22:10:19 +05:30
beforeEach(() => {
form = $(TEMPLATE);
2018-11-20 20:47:30 +05:30
2019-12-26 22:10:19 +05:30
dropzone = dropzoneInput(form);
2018-11-20 20:47:30 +05:30
2019-12-26 22:10:19 +05:30
xhr = jasmine.createSpyObj(Object.keys(XMLHttpRequest.prototype));
oldXMLHttpRequest = window.XMLHttpRequest;
window.XMLHttpRequest = () => xhr;
});
2018-11-20 20:47:30 +05:30
2019-12-26 22:10:19 +05:30
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-11-20 20:47:30 +05:30
2019-12-26 22:10:19 +05:30
it('when AJAX fails with text', () => {
xhr = {
...xhr,
statusCode: 400,
readyState: 4,
responseText: TEST_ERROR_MESSAGE,
getResponseHeader: () => 'text/plain',
};
2018-11-20 20:47:30 +05:30
2019-12-26 22:10:19 +05:30
dropzone.processFile(TEST_FILE);
2018-11-20 20:47:30 +05:30
2019-12-26 22:10:19 +05:30
xhr.onload();
2018-11-20 20:47:30 +05:30
2019-12-26 22:10:19 +05:30
expect(form.find('.uploading-error-message').text()).toEqual(TEST_ERROR_MESSAGE);
});
2018-11-20 20:47:30 +05:30
});
});