Commit c309487f authored by Fabio Huser's avatar Fabio Huser Committed by Phil Hughes

fix(dropzone): prevent dropzone exception if target element missing

parent 71953508
...@@ -290,5 +290,5 @@ export default function dropzoneInput(form) { ...@@ -290,5 +290,5 @@ export default function dropzoneInput(form) {
formTextarea.focus(); formTextarea.focus();
}); });
return Dropzone.forElement($formDropzone.get(0)); return $formDropzone.get(0) ? Dropzone.forElement($formDropzone.get(0)) : null;
} }
---
title: Prevent Dropzone.js initialisation error by checking target element existence
merge_request: 20256
author: Fabio Huser
type: fixed
...@@ -13,54 +13,68 @@ const TEMPLATE = `<form class="gfm-form" data-uploads-path="${TEST_UPLOAD_PATH}" ...@@ -13,54 +13,68 @@ const TEMPLATE = `<form class="gfm-form" data-uploads-path="${TEST_UPLOAD_PATH}"
</form>`; </form>`;
describe('dropzone_input', () => { describe('dropzone_input', () => {
let form; it('returns null when failed to initialize', () => {
let dropzone; const dropzone = dropzoneInput($('<form class="gfm-form"></form>'));
let xhr;
let oldXMLHttpRequest;
beforeEach(() => { expect(dropzone).toBeNull();
form = $(TEMPLATE); });
dropzone = dropzoneInput(form); it('returns valid dropzone when successfully initialize', () => {
const dropzone = dropzoneInput($(TEMPLATE));
xhr = jasmine.createSpyObj(Object.keys(XMLHttpRequest.prototype)); expect(dropzone.version).toBeTruthy();
oldXMLHttpRequest = window.XMLHttpRequest;
window.XMLHttpRequest = () => xhr;
}); });
afterEach(() => { describe('shows error message', () => {
window.XMLHttpRequest = oldXMLHttpRequest; let form;
}); let dropzone;
let xhr;
let oldXMLHttpRequest;
it('shows error message, when AJAX fails with json', () => { beforeEach(() => {
xhr = { form = $(TEMPLATE);
...xhr,
statusCode: 400,
readyState: 4,
responseText: JSON.stringify({ message: TEST_ERROR_MESSAGE }),
getResponseHeader: () => 'application/json',
};
dropzone.processFile(TEST_FILE); dropzone = dropzoneInput(form);
xhr.onload(); xhr = jasmine.createSpyObj(Object.keys(XMLHttpRequest.prototype));
oldXMLHttpRequest = window.XMLHttpRequest;
window.XMLHttpRequest = () => xhr;
});
expect(form.find('.uploading-error-message').text()).toEqual(TEST_ERROR_MESSAGE); 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);
});
it('shows error message, when AJAX fails with text', () => { it('when AJAX fails with text', () => {
xhr = { xhr = {
...xhr, ...xhr,
statusCode: 400, statusCode: 400,
readyState: 4, readyState: 4,
responseText: TEST_ERROR_MESSAGE, responseText: TEST_ERROR_MESSAGE,
getResponseHeader: () => 'text/plain', getResponseHeader: () => 'text/plain',
}; };
dropzone.processFile(TEST_FILE); dropzone.processFile(TEST_FILE);
xhr.onload(); xhr.onload();
expect(form.find('.uploading-error-message').text()).toEqual(TEST_ERROR_MESSAGE); expect(form.find('.uploading-error-message').text()).toEqual(TEST_ERROR_MESSAGE);
});
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment