From 897d8d547c5888bc63d3c3ecc0d0dd971d70e6c0 Mon Sep 17 00:00:00 2001 From: "Luke \"Jared\" Bennett" Date: Tue, 11 Apr 2017 11:28:17 +0100 Subject: Further review changes --- .../blob/balsamiq/balsamiq_viewer_spec.js | 110 +++++++++++++++------ 1 file changed, 79 insertions(+), 31 deletions(-) (limited to 'spec/javascripts/blob/balsamiq/balsamiq_viewer_spec.js') diff --git a/spec/javascripts/blob/balsamiq/balsamiq_viewer_spec.js b/spec/javascripts/blob/balsamiq/balsamiq_viewer_spec.js index 10db4175ca4..557eb721a2b 100644 --- a/spec/javascripts/blob/balsamiq/balsamiq_viewer_spec.js +++ b/spec/javascripts/blob/balsamiq/balsamiq_viewer_spec.js @@ -216,6 +216,35 @@ describe('BalsamiqViewer', () => { }); }); + describe('getTitle', () => { + let database; + let resourceID; + let resource; + let getTitle; + + beforeEach(() => { + database = jasmine.createSpyObj('database', ['exec']); + resourceID = 4; + resource = 'resource'; + + balsamiqViewer = { + database, + }; + + database.exec.and.returnValue(resource); + + getTitle = BalsamiqViewer.prototype.getTitle.call(balsamiqViewer, resourceID); + }); + + it('should call database.exec', () => { + expect(database.exec).toHaveBeenCalledWith(`SELECT * FROM resources WHERE id = '${resourceID}'`); + }); + + it('should return the selected resource', () => { + expect(getTitle).toBe(resource); + }); + }); + describe('renderPreview', () => { let previewElement; let innerHTML; @@ -261,54 +290,50 @@ describe('BalsamiqViewer', () => { describe('renderTemplate', () => { let preview; - let database; + let name; let title; + let template; let renderTemplate; beforeEach(() => { - preview = { reosourceID: 1, image: 'image' }; - title = [{ values: [['{}', '{}', '{ "name": "name" }']] }]; - database = jasmine.createSpyObj('database', ['exec']); - - database.exec.and.returnValue(title); + preview = { resourceID: 1, image: 'image' }; + name = 'name'; + title = 'title'; + template = ` +
+
name
+
+ +
+
+ `; - balsamiqViewer = { - database, - }; + balsamiqViewer = jasmine.createSpyObj('balsamiqViewer', ['getTitle']); - spyOn(JSON, 'parse').and.callThrough(); - spyOn(String.prototype, 'replace').and.callThrough(); + spyOn(BalsamiqViewer, 'parseTitle').and.returnValue(name); + spyOn(BalsamiqViewer, 'PREVIEW_TEMPLATE').and.returnValue(template); + balsamiqViewer.getTitle.and.returnValue(title); renderTemplate = BalsamiqViewer.prototype.renderTemplate.call(balsamiqViewer, preview); }); - it('should call database.exec', () => { - expect(database.exec).toHaveBeenCalledWith(`SELECT * FROM resources WHERE id = '${preview.resourceID}'`); + it('should call .getTitle', () => { + expect(balsamiqViewer.getTitle).toHaveBeenCalledWith(preview.resourceID); }); - it('should call JSON.parse', () => { - expect(JSON.parse).toHaveBeenCalledWith(title[0].values[0][2]); + it('should call .parseTitle', () => { + expect(BalsamiqViewer.parseTitle).toHaveBeenCalledWith(title); }); - it('should call String.prototype.replace', () => { - const allArgs = String.prototype.replace.calls.allArgs(); - - expect(allArgs.length).toBe(2); - expect(allArgs[0]).toEqual([/{{name}}/g, 'name']); - expect(allArgs[1]).toEqual([/{{image}}/g, 'image']); + it('should call .PREVIEW_TEMPLATE', () => { + expect(BalsamiqViewer.PREVIEW_TEMPLATE).toHaveBeenCalledWith({ + name, + image: preview.image, + }); }); it('should return the template string', function () { - const template = ` -
-
name
-
- -
-
- `; - - expect(renderTemplate.replace(/\s/g, '')).toEqual(template.replace(/\s/g, '')); + expect(renderTemplate.trim()).toBe(template.trim()); }); }); @@ -335,6 +360,29 @@ describe('BalsamiqViewer', () => { }); }); + describe('parseTitle', () => { + let title; + let parseTitle; + + beforeEach(() => { + title = [{ values: [['{}', '{}', '{"name":"name"}']] }]; + + spyOn(JSON, 'parse').and.callThrough(); + + parseTitle = BalsamiqViewer.parseTitle(title); + }); + + ClassSpecHelper.itShouldBeAStaticMethod(BalsamiqViewer, 'parsePreview'); + + it('should call JSON.parse', () => { + expect(JSON.parse).toHaveBeenCalledWith(title[0].values[0][2]); + }); + + it('should return the name value', () => { + expect(parseTitle).toBe('name'); + }); + }); + describe('onError', () => { let onError; -- cgit v1.2.1