Commit 9f517a96 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch '194207-migrate-spec-javascripts-blob-to-jest' into 'master'

Migrate balsamiq_viewer_spec to jest

See merge request gitlab-org/gitlab!26655
parents d6ec547c 07ec93ac
...@@ -3,6 +3,8 @@ import axios from '~/lib/utils/axios_utils'; ...@@ -3,6 +3,8 @@ 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';
jest.mock('sql.js');
describe('BalsamiqViewer', () => { describe('BalsamiqViewer', () => {
const mockArrayBuffer = new ArrayBuffer(10); const mockArrayBuffer = new ArrayBuffer(10);
let balsamiqViewer; let balsamiqViewer;
...@@ -34,22 +36,22 @@ describe('BalsamiqViewer', () => { ...@@ -34,22 +36,22 @@ describe('BalsamiqViewer', () => {
}); });
it('should call `axios.get` on `endpoint` param with responseType set to `arraybuffer', () => { it('should call `axios.get` on `endpoint` param with responseType set to `arraybuffer', () => {
spyOn(axios, 'get').and.returnValue(requestSuccess); jest.spyOn(axios, 'get').mockReturnValue(requestSuccess);
spyOn(bv, 'renderFile').and.stub(); jest.spyOn(bv, 'renderFile').mockReturnValue();
bv.loadFile(endpoint); bv.loadFile(endpoint);
expect(axios.get).toHaveBeenCalledWith( expect(axios.get).toHaveBeenCalledWith(
endpoint, endpoint,
jasmine.objectContaining({ expect.objectContaining({
responseType: 'arraybuffer', responseType: 'arraybuffer',
}), }),
); );
}); });
it('should call `renderFile` on request success', done => { it('should call `renderFile` on request success', done => {
spyOn(axios, 'get').and.returnValue(requestSuccess); jest.spyOn(axios, 'get').mockReturnValue(requestSuccess);
spyOn(bv, 'renderFile').and.callFake(() => {}); jest.spyOn(bv, 'renderFile').mockImplementation(() => {});
bv.loadFile(endpoint) bv.loadFile(endpoint)
.then(() => { .then(() => {
...@@ -60,8 +62,8 @@ describe('BalsamiqViewer', () => { ...@@ -60,8 +62,8 @@ describe('BalsamiqViewer', () => {
}); });
it('should not call `renderFile` on request failure', done => { it('should not call `renderFile` on request failure', done => {
spyOn(axios, 'get').and.returnValue(Promise.reject()); jest.spyOn(axios, 'get').mockReturnValue(Promise.reject());
spyOn(bv, 'renderFile'); jest.spyOn(bv, 'renderFile').mockImplementation(() => {});
bv.loadFile(endpoint) bv.loadFile(endpoint)
.then(() => { .then(() => {
...@@ -80,19 +82,21 @@ describe('BalsamiqViewer', () => { ...@@ -80,19 +82,21 @@ describe('BalsamiqViewer', () => {
let previews; let previews;
beforeEach(() => { beforeEach(() => {
viewer = jasmine.createSpyObj('viewer', ['appendChild']); viewer = {
appendChild: jest.fn(),
};
previews = [document.createElement('ul'), document.createElement('ul')]; previews = [document.createElement('ul'), document.createElement('ul')];
balsamiqViewer = jasmine.createSpyObj('balsamiqViewer', [ balsamiqViewer = {
'initDatabase', initDatabase: jest.fn(),
'getPreviews', getPreviews: jest.fn(),
'renderPreview', renderPreview: jest.fn(),
]); };
balsamiqViewer.viewer = viewer; balsamiqViewer.viewer = viewer;
balsamiqViewer.getPreviews.and.returnValue(previews); balsamiqViewer.getPreviews.mockReturnValue(previews);
balsamiqViewer.renderPreview.and.callFake(preview => preview); balsamiqViewer.renderPreview.mockImplementation(preview => preview);
viewer.appendChild.and.callFake(containerElement => { viewer.appendChild.mockImplementation(containerElement => {
container = containerElement; container = containerElement;
}); });
...@@ -108,7 +112,7 @@ describe('BalsamiqViewer', () => { ...@@ -108,7 +112,7 @@ describe('BalsamiqViewer', () => {
}); });
it('should call .renderPreview for each preview', () => { it('should call .renderPreview for each preview', () => {
const allArgs = balsamiqViewer.renderPreview.calls.allArgs(); const allArgs = balsamiqViewer.renderPreview.mock.calls;
expect(allArgs.length).toBe(2); expect(allArgs.length).toBe(2);
...@@ -132,19 +136,15 @@ describe('BalsamiqViewer', () => { ...@@ -132,19 +136,15 @@ describe('BalsamiqViewer', () => {
}); });
describe('initDatabase', () => { describe('initDatabase', () => {
let database;
let uint8Array; let uint8Array;
let data; let data;
beforeEach(() => { beforeEach(() => {
uint8Array = {}; uint8Array = {};
database = {};
data = 'data'; data = 'data';
balsamiqViewer = {}; balsamiqViewer = {};
window.Uint8Array = jest.fn();
spyOn(window, 'Uint8Array').and.returnValue(uint8Array); window.Uint8Array.mockReturnValue(uint8Array);
spyOn(sqljs, 'Database').and.returnValue(database);
BalsamiqViewer.prototype.initDatabase.call(balsamiqViewer, data); BalsamiqViewer.prototype.initDatabase.call(balsamiqViewer, data);
}); });
...@@ -158,7 +158,7 @@ describe('BalsamiqViewer', () => { ...@@ -158,7 +158,7 @@ describe('BalsamiqViewer', () => {
}); });
it('should set .database', () => { it('should set .database', () => {
expect(balsamiqViewer.database).toBe(database); expect(balsamiqViewer.database).not.toBe(null);
}); });
}); });
...@@ -168,15 +168,17 @@ describe('BalsamiqViewer', () => { ...@@ -168,15 +168,17 @@ describe('BalsamiqViewer', () => {
let getPreviews; let getPreviews;
beforeEach(() => { beforeEach(() => {
database = jasmine.createSpyObj('database', ['exec']); database = {
exec: jest.fn(),
};
thumbnails = [{ values: [0, 1, 2] }]; thumbnails = [{ values: [0, 1, 2] }];
balsamiqViewer = { balsamiqViewer = {
database, database,
}; };
spyOn(BalsamiqViewer, 'parsePreview').and.callFake(preview => preview.toString()); jest.spyOn(BalsamiqViewer, 'parsePreview').mockImplementation(preview => preview.toString());
database.exec.and.returnValue(thumbnails); database.exec.mockReturnValue(thumbnails);
getPreviews = BalsamiqViewer.prototype.getPreviews.call(balsamiqViewer); getPreviews = BalsamiqViewer.prototype.getPreviews.call(balsamiqViewer);
}); });
...@@ -186,7 +188,7 @@ describe('BalsamiqViewer', () => { ...@@ -186,7 +188,7 @@ describe('BalsamiqViewer', () => {
}); });
it('should call .parsePreview for each value', () => { it('should call .parsePreview for each value', () => {
const allArgs = BalsamiqViewer.parsePreview.calls.allArgs(); const allArgs = BalsamiqViewer.parsePreview.mock.calls;
expect(allArgs.length).toBe(3); expect(allArgs.length).toBe(3);
...@@ -207,7 +209,9 @@ describe('BalsamiqViewer', () => { ...@@ -207,7 +209,9 @@ describe('BalsamiqViewer', () => {
let getResource; let getResource;
beforeEach(() => { beforeEach(() => {
database = jasmine.createSpyObj('database', ['exec']); database = {
exec: jest.fn(),
};
resourceID = 4; resourceID = 4;
resource = ['resource']; resource = ['resource'];
...@@ -215,7 +219,7 @@ describe('BalsamiqViewer', () => { ...@@ -215,7 +219,7 @@ describe('BalsamiqViewer', () => {
database, database,
}; };
database.exec.and.returnValue(resource); database.exec.mockReturnValue(resource);
getResource = BalsamiqViewer.prototype.getResource.call(balsamiqViewer, resourceID); getResource = BalsamiqViewer.prototype.getResource.call(balsamiqViewer, resourceID);
}); });
...@@ -241,14 +245,18 @@ describe('BalsamiqViewer', () => { ...@@ -241,14 +245,18 @@ describe('BalsamiqViewer', () => {
innerHTML = '<a>innerHTML</a>'; innerHTML = '<a>innerHTML</a>';
previewElement = { previewElement = {
outerHTML: '<p>outerHTML</p>', outerHTML: '<p>outerHTML</p>',
classList: jasmine.createSpyObj('classList', ['add']), classList: {
add: jest.fn(),
},
}; };
preview = {}; preview = {};
balsamiqViewer = jasmine.createSpyObj('balsamiqViewer', ['renderTemplate']); balsamiqViewer = {
renderTemplate: jest.fn(),
};
spyOn(document, 'createElement').and.returnValue(previewElement); jest.spyOn(document, 'createElement').mockReturnValue(previewElement);
balsamiqViewer.renderTemplate.and.returnValue(innerHTML); balsamiqViewer.renderTemplate.mockReturnValue(innerHTML);
renderPreview = BalsamiqViewer.prototype.renderPreview.call(balsamiqViewer, preview); renderPreview = BalsamiqViewer.prototype.renderPreview.call(balsamiqViewer, preview);
}); });
...@@ -290,10 +298,12 @@ describe('BalsamiqViewer', () => { ...@@ -290,10 +298,12 @@ describe('BalsamiqViewer', () => {
</div> </div>
`; `;
balsamiqViewer = jasmine.createSpyObj('balsamiqViewer', ['getResource']); balsamiqViewer = {
getResource: jest.fn(),
};
spyOn(BalsamiqViewer, 'parseTitle').and.returnValue(name); jest.spyOn(BalsamiqViewer, 'parseTitle').mockReturnValue(name);
balsamiqViewer.getResource.and.returnValue(resource); balsamiqViewer.getResource.mockReturnValue(resource);
renderTemplate = BalsamiqViewer.prototype.renderTemplate.call(balsamiqViewer, preview); renderTemplate = BalsamiqViewer.prototype.renderTemplate.call(balsamiqViewer, preview);
}); });
...@@ -306,7 +316,7 @@ describe('BalsamiqViewer', () => { ...@@ -306,7 +316,7 @@ describe('BalsamiqViewer', () => {
expect(BalsamiqViewer.parseTitle).toHaveBeenCalledWith(resource); expect(BalsamiqViewer.parseTitle).toHaveBeenCalledWith(resource);
}); });
it('should return the template string', function() { it('should return the template string', () => {
expect(renderTemplate.replace(/\s/g, '')).toEqual(template.replace(/\s/g, '')); expect(renderTemplate.replace(/\s/g, '')).toEqual(template.replace(/\s/g, ''));
}); });
}); });
...@@ -318,7 +328,7 @@ describe('BalsamiqViewer', () => { ...@@ -318,7 +328,7 @@ describe('BalsamiqViewer', () => {
beforeEach(() => { beforeEach(() => {
preview = ['{}', '{ "id": 1 }']; preview = ['{}', '{ "id": 1 }'];
spyOn(JSON, 'parse').and.callThrough(); jest.spyOn(JSON, 'parse');
parsePreview = BalsamiqViewer.parsePreview(preview); parsePreview = BalsamiqViewer.parsePreview(preview);
}); });
...@@ -337,7 +347,7 @@ describe('BalsamiqViewer', () => { ...@@ -337,7 +347,7 @@ describe('BalsamiqViewer', () => {
beforeEach(() => { beforeEach(() => {
title = { values: [['{}', '{}', '{"name":"name"}']] }; title = { values: [['{}', '{}', '{"name":"name"}']] };
spyOn(JSON, 'parse').and.callThrough(); jest.spyOn(JSON, 'parse');
parseTitle = BalsamiqViewer.parseTitle(title); parseTitle = BalsamiqViewer.parseTitle(title);
}); });
......
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