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

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-11-20 20:47:30 +05:30
import $ from 'jquery';
import dropzoneInput from '~/dropzone_input';
import { TEST_HOST } from 'spec/test_constants';
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', () => {
let form;
let dropzone;
let xhr;
let oldXMLHttpRequest;
beforeEach(() => {
form = $(TEMPLATE);
dropzone = dropzoneInput(form);
xhr = jasmine.createSpyObj(Object.keys(XMLHttpRequest.prototype));
oldXMLHttpRequest = window.XMLHttpRequest;
window.XMLHttpRequest = () => xhr;
});
afterEach(() => {
window.XMLHttpRequest = oldXMLHttpRequest;
});
it('shows error message, 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);
});
it('shows error message, when AJAX fails with text', () => {
xhr = {
...xhr,
statusCode: 400,
readyState: 4,
responseText: TEST_ERROR_MESSAGE,
getResponseHeader: () => 'text/plain',
};
dropzone.processFile(TEST_FILE);
xhr.onload();
expect(form.find('.uploading-error-message').text()).toEqual(TEST_ERROR_MESSAGE);
});
});