2017-08-17 22:00:37 +05:30
|
|
|
import sqljs from 'sql.js';
|
2021-03-08 18:12:59 +05:30
|
|
|
import ClassSpecHelper from 'helpers/class_spec_helper';
|
2017-08-17 22:00:37 +05:30
|
|
|
import BalsamiqViewer from '~/blob/balsamiq/balsamiq_viewer';
|
2021-03-11 19:13:27 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
jest.mock('sql.js');
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe('BalsamiqViewer', () => {
|
2019-12-21 20:55:43 +05:30
|
|
|
const mockArrayBuffer = new ArrayBuffer(10);
|
2017-08-17 22:00:37 +05:30
|
|
|
let balsamiqViewer;
|
|
|
|
let viewer;
|
|
|
|
|
|
|
|
describe('class constructor', () => {
|
|
|
|
beforeEach(() => {
|
2017-09-10 17:25:29 +05:30
|
|
|
viewer = {};
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
balsamiqViewer = new BalsamiqViewer(viewer);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set .viewer', () => {
|
|
|
|
expect(balsamiqViewer.viewer).toBe(viewer);
|
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe('loadFile', () => {
|
2019-12-21 20:55:43 +05:30
|
|
|
let bv;
|
2017-09-10 17:25:29 +05:30
|
|
|
const endpoint = 'endpoint';
|
2019-12-21 20:55:43 +05:30
|
|
|
const requestSuccess = Promise.resolve({
|
|
|
|
data: mockArrayBuffer,
|
|
|
|
status: 200,
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-12-21 20:55:43 +05:30
|
|
|
viewer = {};
|
|
|
|
bv = new BalsamiqViewer(viewer);
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
it('should call `axios.get` on `endpoint` param with responseType set to `arraybuffer', () => {
|
2020-04-08 14:13:33 +05:30
|
|
|
jest.spyOn(axios, 'get').mockReturnValue(requestSuccess);
|
|
|
|
jest.spyOn(bv, 'renderFile').mockReturnValue();
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
bv.loadFile(endpoint);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(axios.get).toHaveBeenCalledWith(
|
|
|
|
endpoint,
|
2020-04-08 14:13:33 +05:30
|
|
|
expect.objectContaining({
|
2019-12-21 20:55:43 +05:30
|
|
|
responseType: 'arraybuffer',
|
|
|
|
}),
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('should call `renderFile` on request success', (done) => {
|
2020-04-08 14:13:33 +05:30
|
|
|
jest.spyOn(axios, 'get').mockReturnValue(requestSuccess);
|
|
|
|
jest.spyOn(bv, 'renderFile').mockImplementation(() => {});
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
bv.loadFile(endpoint)
|
|
|
|
.then(() => {
|
|
|
|
expect(bv.renderFile).toHaveBeenCalledWith(mockArrayBuffer);
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('should not call `renderFile` on request failure', (done) => {
|
2020-04-08 14:13:33 +05:30
|
|
|
jest.spyOn(axios, 'get').mockReturnValue(Promise.reject());
|
|
|
|
jest.spyOn(bv, 'renderFile').mockImplementation(() => {});
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
bv.loadFile(endpoint)
|
|
|
|
.then(() => {
|
|
|
|
done.fail('Expected loadFile to throw error!');
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
expect(bv.renderFile).not.toHaveBeenCalled();
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('renderFile', () => {
|
|
|
|
let container;
|
|
|
|
let previews;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-04-08 14:13:33 +05:30
|
|
|
viewer = {
|
|
|
|
appendChild: jest.fn(),
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
previews = [document.createElement('ul'), document.createElement('ul')];
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
balsamiqViewer = {
|
|
|
|
initDatabase: jest.fn(),
|
|
|
|
getPreviews: jest.fn(),
|
|
|
|
renderPreview: jest.fn(),
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
balsamiqViewer.viewer = viewer;
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
balsamiqViewer.getPreviews.mockReturnValue(previews);
|
2021-03-08 18:12:59 +05:30
|
|
|
balsamiqViewer.renderPreview.mockImplementation((preview) => preview);
|
|
|
|
viewer.appendChild.mockImplementation((containerElement) => {
|
2017-08-17 22:00:37 +05:30
|
|
|
container = containerElement;
|
|
|
|
});
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
BalsamiqViewer.prototype.renderFile.call(balsamiqViewer, mockArrayBuffer);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .initDatabase', () => {
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(balsamiqViewer.initDatabase).toHaveBeenCalledWith(mockArrayBuffer);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .getPreviews', () => {
|
|
|
|
expect(balsamiqViewer.getPreviews).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .renderPreview for each preview', () => {
|
2020-04-08 14:13:33 +05:30
|
|
|
const allArgs = balsamiqViewer.renderPreview.mock.calls;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(allArgs.length).toBe(2);
|
|
|
|
|
|
|
|
previews.forEach((preview, i) => {
|
|
|
|
expect(allArgs[i][0]).toBe(preview);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set the container HTML', () => {
|
|
|
|
expect(container.innerHTML).toBe('<ul></ul><ul></ul>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add inline preview classes', () => {
|
|
|
|
expect(container.classList[0]).toBe('list-inline');
|
|
|
|
expect(container.classList[1]).toBe('previews');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call viewer.appendChild', () => {
|
|
|
|
expect(viewer.appendChild).toHaveBeenCalledWith(container);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('initDatabase', () => {
|
|
|
|
let uint8Array;
|
|
|
|
let data;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
uint8Array = {};
|
|
|
|
data = 'data';
|
|
|
|
balsamiqViewer = {};
|
2020-04-08 14:13:33 +05:30
|
|
|
window.Uint8Array = jest.fn();
|
|
|
|
window.Uint8Array.mockReturnValue(uint8Array);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
BalsamiqViewer.prototype.initDatabase.call(balsamiqViewer, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should instantiate Uint8Array', () => {
|
|
|
|
expect(window.Uint8Array).toHaveBeenCalledWith(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call sqljs.Database', () => {
|
|
|
|
expect(sqljs.Database).toHaveBeenCalledWith(uint8Array);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set .database', () => {
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(balsamiqViewer.database).not.toBe(null);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getPreviews', () => {
|
|
|
|
let database;
|
|
|
|
let thumbnails;
|
|
|
|
let getPreviews;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-04-08 14:13:33 +05:30
|
|
|
database = {
|
|
|
|
exec: jest.fn(),
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
thumbnails = [{ values: [0, 1, 2] }];
|
|
|
|
|
|
|
|
balsamiqViewer = {
|
|
|
|
database,
|
|
|
|
};
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
jest
|
|
|
|
.spyOn(BalsamiqViewer, 'parsePreview')
|
|
|
|
.mockImplementation((preview) => preview.toString());
|
2020-04-08 14:13:33 +05:30
|
|
|
database.exec.mockReturnValue(thumbnails);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
getPreviews = BalsamiqViewer.prototype.getPreviews.call(balsamiqViewer);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call database.exec', () => {
|
|
|
|
expect(database.exec).toHaveBeenCalledWith('SELECT * FROM thumbnails');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .parsePreview for each value', () => {
|
2020-04-08 14:13:33 +05:30
|
|
|
const allArgs = BalsamiqViewer.parsePreview.mock.calls;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(allArgs.length).toBe(3);
|
|
|
|
|
|
|
|
thumbnails[0].values.forEach((value, i) => {
|
|
|
|
expect(allArgs[i][0]).toBe(value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return an array of parsed values', () => {
|
|
|
|
expect(getPreviews).toEqual(['0', '1', '2']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getResource', () => {
|
|
|
|
let database;
|
|
|
|
let resourceID;
|
|
|
|
let resource;
|
|
|
|
let getResource;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-04-08 14:13:33 +05:30
|
|
|
database = {
|
|
|
|
exec: jest.fn(),
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
resourceID = 4;
|
|
|
|
resource = ['resource'];
|
|
|
|
|
|
|
|
balsamiqViewer = {
|
|
|
|
database,
|
|
|
|
};
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
database.exec.mockReturnValue(resource);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
getResource = BalsamiqViewer.prototype.getResource.call(balsamiqViewer, resourceID);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call database.exec', () => {
|
2018-12-13 13:39:08 +05:30
|
|
|
expect(database.exec).toHaveBeenCalledWith(
|
|
|
|
`SELECT * FROM resources WHERE id = '${resourceID}'`,
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the selected resource', () => {
|
|
|
|
expect(getResource).toBe(resource[0]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('renderPreview', () => {
|
|
|
|
let previewElement;
|
|
|
|
let innerHTML;
|
|
|
|
let preview;
|
|
|
|
let renderPreview;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
innerHTML = '<a>innerHTML</a>';
|
|
|
|
previewElement = {
|
|
|
|
outerHTML: '<p>outerHTML</p>',
|
2020-04-08 14:13:33 +05:30
|
|
|
classList: {
|
|
|
|
add: jest.fn(),
|
|
|
|
},
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
|
|
|
preview = {};
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
balsamiqViewer = {
|
|
|
|
renderTemplate: jest.fn(),
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
jest.spyOn(document, 'createElement').mockReturnValue(previewElement);
|
|
|
|
balsamiqViewer.renderTemplate.mockReturnValue(innerHTML);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
renderPreview = BalsamiqViewer.prototype.renderPreview.call(balsamiqViewer, preview);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call classList.add', () => {
|
|
|
|
expect(previewElement.classList.add).toHaveBeenCalledWith('preview');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .renderTemplate', () => {
|
|
|
|
expect(balsamiqViewer.renderTemplate).toHaveBeenCalledWith(preview);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set .innerHTML', () => {
|
|
|
|
expect(previewElement.innerHTML).toBe(innerHTML);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return element', () => {
|
|
|
|
expect(renderPreview).toBe(previewElement);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('renderTemplate', () => {
|
|
|
|
let preview;
|
|
|
|
let name;
|
|
|
|
let resource;
|
|
|
|
let template;
|
|
|
|
let renderTemplate;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
preview = { resourceID: 1, image: 'image' };
|
|
|
|
name = 'name';
|
|
|
|
resource = 'resource';
|
|
|
|
template = `
|
2018-11-08 19:23:39 +05:30
|
|
|
<div class="card">
|
|
|
|
<div class="card-header">name</div>
|
|
|
|
<div class="card-body">
|
2017-08-17 22:00:37 +05:30
|
|
|
<img class="img-thumbnail" src="data:image/png;base64,image"/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
balsamiqViewer = {
|
|
|
|
getResource: jest.fn(),
|
|
|
|
};
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
jest.spyOn(BalsamiqViewer, 'parseTitle').mockReturnValue(name);
|
|
|
|
balsamiqViewer.getResource.mockReturnValue(resource);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
renderTemplate = BalsamiqViewer.prototype.renderTemplate.call(balsamiqViewer, preview);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .getResource', () => {
|
|
|
|
expect(balsamiqViewer.getResource).toHaveBeenCalledWith(preview.resourceID);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .parseTitle', () => {
|
|
|
|
expect(BalsamiqViewer.parseTitle).toHaveBeenCalledWith(resource);
|
|
|
|
});
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it('should return the template string', () => {
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(renderTemplate.replace(/\s/g, '')).toEqual(template.replace(/\s/g, ''));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('parsePreview', () => {
|
|
|
|
let preview;
|
|
|
|
let parsePreview;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
preview = ['{}', '{ "id": 1 }'];
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
jest.spyOn(JSON, 'parse');
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
parsePreview = BalsamiqViewer.parsePreview(preview);
|
|
|
|
});
|
|
|
|
|
|
|
|
ClassSpecHelper.itShouldBeAStaticMethod(BalsamiqViewer, 'parsePreview');
|
|
|
|
|
|
|
|
it('should return the parsed JSON', () => {
|
|
|
|
expect(parsePreview).toEqual(JSON.parse('{ "id": 1 }'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('parseTitle', () => {
|
|
|
|
let title;
|
|
|
|
let parseTitle;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
title = { values: [['{}', '{}', '{"name":"name"}']] };
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
jest.spyOn(JSON, 'parse');
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
parseTitle = BalsamiqViewer.parseTitle(title);
|
|
|
|
});
|
|
|
|
|
|
|
|
ClassSpecHelper.itShouldBeAStaticMethod(BalsamiqViewer, 'parsePreview');
|
|
|
|
|
|
|
|
it('should return the name value', () => {
|
|
|
|
expect(parseTitle).toBe('name');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|