Commit 5f2b202a authored by Filipa Lacerda's avatar Filipa Lacerda

Merge branch 'kp-replace-xmlhttprequest-with-axios' into 'master'

Replace `XMLHttpRequest` with `axios` to fetch Balsamiq mockups

See merge request gitlab-org/gitlab!17241
parents c2f79d0c 613c7a3b
import sqljs from 'sql.js'; import sqljs from 'sql.js';
import { template as _template } from 'underscore'; import { template as _template } from 'underscore';
import axios from '~/lib/utils/axios_utils';
import { successCodes } from '~/lib/utils/http_status';
const PREVIEW_TEMPLATE = _template(` const PREVIEW_TEMPLATE = _template(`
<div class="card"> <div class="card">
...@@ -16,30 +18,25 @@ class BalsamiqViewer { ...@@ -16,30 +18,25 @@ class BalsamiqViewer {
} }
loadFile(endpoint) { loadFile(endpoint) {
return new Promise((resolve, reject) => { return axios
const xhr = new XMLHttpRequest(); .get(endpoint, {
responseType: 'arraybuffer',
xhr.open('GET', endpoint, true); validateStatus(status) {
xhr.responseType = 'arraybuffer'; return status !== successCodes.OK;
xhr.onload = loadEvent => this.fileLoaded(loadEvent, resolve, reject); },
xhr.onerror = reject; })
.then(({ data }) => {
xhr.send(); this.renderFile(data);
})
.catch(e => {
throw new Error(e);
}); });
} }
fileLoaded(loadEvent, resolve, reject) { renderFile(fileBuffer) {
if (loadEvent.target.status !== 200) return reject();
this.renderFile(loadEvent);
return resolve();
}
renderFile(loadEvent) {
const container = document.createElement('ul'); const container = document.createElement('ul');
this.initDatabase(loadEvent.target.response); this.initDatabase(fileBuffer);
const previews = this.getPreviews(); const previews = this.getPreviews();
previews.forEach(preview => { previews.forEach(preview => {
......
import sqljs from 'sql.js'; import sqljs from 'sql.js';
import axios from '~/lib/utils/axios_utils';
import BalsamiqViewer from '~/blob/balsamiq/balsamiq_viewer'; import BalsamiqViewer from '~/blob/balsamiq/balsamiq_viewer';
import ClassSpecHelper from '../../helpers/class_spec_helper'; import ClassSpecHelper from '../../helpers/class_spec_helper';
describe('BalsamiqViewer', () => { describe('BalsamiqViewer', () => {
const mockArrayBuffer = new ArrayBuffer(10);
let balsamiqViewer; let balsamiqViewer;
let viewer; let viewer;
...@@ -19,44 +21,65 @@ describe('BalsamiqViewer', () => { ...@@ -19,44 +21,65 @@ describe('BalsamiqViewer', () => {
}); });
describe('loadFile', () => { describe('loadFile', () => {
let xhr; let bv;
let loadFile;
const endpoint = 'endpoint'; const endpoint = 'endpoint';
const requestSuccess = Promise.resolve({
data: mockArrayBuffer,
status: 200,
});
beforeEach(() => { beforeEach(() => {
xhr = jasmine.createSpyObj('xhr', ['open', 'send']); viewer = {};
bv = new BalsamiqViewer(viewer);
});
balsamiqViewer = jasmine.createSpyObj('balsamiqViewer', ['renderFile']); it('should call `axios.get` on `endpoint` param with responseType set to `arraybuffer', () => {
spyOn(axios, 'get').and.returnValue(requestSuccess);
spyOn(bv, 'renderFile').and.stub();
spyOn(window, 'XMLHttpRequest').and.returnValue(xhr); bv.loadFile(endpoint);
loadFile = BalsamiqViewer.prototype.loadFile.call(balsamiqViewer, endpoint); expect(axios.get).toHaveBeenCalledWith(
endpoint,
jasmine.objectContaining({
responseType: 'arraybuffer',
}),
);
}); });
it('should call .open', () => { it('should call `renderFile` on request success', done => {
expect(xhr.open).toHaveBeenCalledWith('GET', endpoint, true); spyOn(axios, 'get').and.returnValue(requestSuccess);
}); spyOn(bv, 'renderFile').and.callFake(() => {});
it('should set .responseType', () => { bv.loadFile(endpoint)
expect(xhr.responseType).toBe('arraybuffer'); .then(() => {
expect(bv.renderFile).toHaveBeenCalledWith(mockArrayBuffer);
})
.then(done)
.catch(done.fail);
}); });
it('should call .send', () => { it('should not call `renderFile` on request failure', done => {
expect(xhr.send).toHaveBeenCalled(); spyOn(axios, 'get').and.returnValue(Promise.reject());
}); spyOn(bv, 'renderFile');
it('should return a promise', () => { bv.loadFile(endpoint)
expect(loadFile).toEqual(jasmine.any(Promise)); .then(() => {
done.fail('Expected loadFile to throw error!');
})
.catch(() => {
expect(bv.renderFile).not.toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
}); });
}); });
describe('renderFile', () => { describe('renderFile', () => {
let container; let container;
let loadEvent;
let previews; let previews;
beforeEach(() => { beforeEach(() => {
loadEvent = { target: { response: {} } };
viewer = jasmine.createSpyObj('viewer', ['appendChild']); viewer = jasmine.createSpyObj('viewer', ['appendChild']);
previews = [document.createElement('ul'), document.createElement('ul')]; previews = [document.createElement('ul'), document.createElement('ul')];
...@@ -73,11 +96,11 @@ describe('BalsamiqViewer', () => { ...@@ -73,11 +96,11 @@ describe('BalsamiqViewer', () => {
container = containerElement; container = containerElement;
}); });
BalsamiqViewer.prototype.renderFile.call(balsamiqViewer, loadEvent); BalsamiqViewer.prototype.renderFile.call(balsamiqViewer, mockArrayBuffer);
}); });
it('should call .initDatabase', () => { it('should call .initDatabase', () => {
expect(balsamiqViewer.initDatabase).toHaveBeenCalledWith(loadEvent.target.response); expect(balsamiqViewer.initDatabase).toHaveBeenCalledWith(mockArrayBuffer);
}); });
it('should call .getPreviews', () => { it('should call .getPreviews', () => {
......
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