import sqljs from 'sql.js'; import ClassSpecHelper from 'helpers/class_spec_helper'; import axios from '~/lib/utils/axios_utils'; import BalsamiqViewer from '~/blob/balsamiq/balsamiq_viewer'; jest.mock('sql.js'); describe('BalsamiqViewer', () => { const mockArrayBuffer = new ArrayBuffer(10); let balsamiqViewer; let viewer; describe('class constructor', () => { beforeEach(() => { viewer = {}; balsamiqViewer = new BalsamiqViewer(viewer); }); it('should set .viewer', () => { expect(balsamiqViewer.viewer).toBe(viewer); }); }); describe('loadFile', () => { let bv; const endpoint = 'endpoint'; const requestSuccess = Promise.resolve({ data: mockArrayBuffer, status: 200, }); beforeEach(() => { viewer = {}; bv = new BalsamiqViewer(viewer); }); it('should call `axios.get` on `endpoint` param with responseType set to `arraybuffer', () => { jest.spyOn(axios, 'get').mockReturnValue(requestSuccess); jest.spyOn(bv, 'renderFile').mockReturnValue(); bv.loadFile(endpoint); expect(axios.get).toHaveBeenCalledWith( endpoint, expect.objectContaining({ responseType: 'arraybuffer', }), ); }); it('should call `renderFile` on request success', (done) => { jest.spyOn(axios, 'get').mockReturnValue(requestSuccess); jest.spyOn(bv, 'renderFile').mockImplementation(() => {}); bv.loadFile(endpoint) .then(() => { expect(bv.renderFile).toHaveBeenCalledWith(mockArrayBuffer); }) .then(done) .catch(done.fail); }); it('should not call `renderFile` on request failure', (done) => { jest.spyOn(axios, 'get').mockReturnValue(Promise.reject()); jest.spyOn(bv, 'renderFile').mockImplementation(() => {}); bv.loadFile(endpoint) .then(() => { done.fail('Expected loadFile to throw error!'); }) .catch(() => { expect(bv.renderFile).not.toHaveBeenCalled(); }) .then(done) .catch(done.fail); }); }); describe('renderFile', () => { let container; let previews; beforeEach(() => { viewer = { appendChild: jest.fn(), }; previews = [document.createElement('ul'), document.createElement('ul')]; balsamiqViewer = { initDatabase: jest.fn(), getPreviews: jest.fn(), renderPreview: jest.fn(), }; balsamiqViewer.viewer = viewer; balsamiqViewer.getPreviews.mockReturnValue(previews); balsamiqViewer.renderPreview.mockImplementation((preview) => preview); viewer.appendChild.mockImplementation((containerElement) => { container = containerElement; }); BalsamiqViewer.prototype.renderFile.call(balsamiqViewer, mockArrayBuffer); }); it('should call .initDatabase', () => { expect(balsamiqViewer.initDatabase).toHaveBeenCalledWith(mockArrayBuffer); }); it('should call .getPreviews', () => { expect(balsamiqViewer.getPreviews).toHaveBeenCalled(); }); it('should call .renderPreview for each preview', () => { const allArgs = balsamiqViewer.renderPreview.mock.calls; 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('
outerHTML
', classList: { add: jest.fn(), }, }; preview = {}; balsamiqViewer = { renderTemplate: jest.fn(), }; jest.spyOn(document, 'createElement').mockReturnValue(previewElement); balsamiqViewer.renderTemplate.mockReturnValue(innerHTML); 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 = `