From 5e1be3bc53db3aa94eb6b14dfb6cd86fb41ba90d Mon Sep 17 00:00:00 2001 From: Carlos Date: Mon, 16 Oct 2017 15:20:40 +0200 Subject: [PATCH] client side unit test for vnWatcher until submit[controller.save] plus small refactor for other files plus deletion of unused file --- client/client/src/notes/notes.spec.js | 9 +- .../src/autocomplete/autocomplete.spec.js | 2 +- client/core/src/watcher/watcher.js | 4 +- client/core/src/watcher/watcher.spec.js | 99 ++++++++++++++++++- jasmine.js | 9 -- 5 files changed, 104 insertions(+), 19 deletions(-) delete mode 100644 jasmine.js diff --git a/client/client/src/notes/notes.spec.js b/client/client/src/notes/notes.spec.js index 78d8092fc6..0ae3c1f5c3 100644 --- a/client/client/src/notes/notes.spec.js +++ b/client/client/src/notes/notes.spec.js @@ -33,11 +33,14 @@ describe('Client', () => { it(`should request to GET the client notes`, () => { let controller = $componentController('vnClientNotes', {$httpBackend: $httpBackend, $state: $state}); controller.client = {id: '1234'}; - let json = JSON.stringify({where: {clientFk: '1234'}, order: 'created DESC'}); - $httpBackend.when('GET', `/client/api/clientObservations?filter=${json}`).respond('ok'); - $httpBackend.expectGET(`/client/api/clientObservations?filter=${json}`, {Accept: 'application/json, text/plain, */*'}); + let jsonString = JSON.stringify({where: {clientFk: '1234'}, order: 'created DESC'}); + let json = {data: 'some data'}; + $httpBackend.when('GET', `/client/api/clientObservations?filter=${jsonString}`).respond(json); + $httpBackend.expectGET(`/client/api/clientObservations?filter=${jsonString}`, {Accept: 'application/json, text/plain, */*'}); controller.getObservation(); $httpBackend.flush(); + + expect(controller.observations).toEqual(json); }); }); diff --git a/client/core/src/autocomplete/autocomplete.spec.js b/client/core/src/autocomplete/autocomplete.spec.js index 5c920bba8e..28e02d1b4c 100644 --- a/client/core/src/autocomplete/autocomplete.spec.js +++ b/client/core/src/autocomplete/autocomplete.spec.js @@ -154,7 +154,7 @@ describe('Component vnAutocomplete', () => { }); }); - describe('findItem()', () => { + describe('findItems()', () => { it(`should return items empty array if the controller does not provide a url and have no items defined`, () => { let controller = $componentController('vnIconMenu', {$scope, $element, $httpBackend, $timeout}); controller.findItems('some search value'); diff --git a/client/core/src/watcher/watcher.js b/client/core/src/watcher/watcher.js index bb6b753b20..2dbc5a2bc7 100644 --- a/client/core/src/watcher/watcher.js +++ b/client/core/src/watcher/watcher.js @@ -59,7 +59,7 @@ export default class Watcher extends Component { * Submits the data and goes back in the history. */ submitBack() { - this.submit().then( + return this.submit().then( () => this.window.history.back() ); } @@ -69,7 +69,7 @@ export default class Watcher extends Component { * @param {String} state The state name */ submitGo(state) { - this.submit().then( + return this.submit().then( () => this.$state.go(state) ); } diff --git a/client/core/src/watcher/watcher.spec.js b/client/core/src/watcher/watcher.spec.js index d47a7523a4..bb53b262a3 100644 --- a/client/core/src/watcher/watcher.spec.js +++ b/client/core/src/watcher/watcher.spec.js @@ -25,7 +25,7 @@ describe('Component vnWatcher', () => { $translate = _$translate_; })); - describe('$onInit() ', () => { + describe('$onInit()', () => { it(`should call fetchData() if controllers get and url properties are defined`, () => { let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}); controller.get = () => {}; @@ -46,7 +46,7 @@ describe('Component vnWatcher', () => { }); }); - describe('$onChanges() ', () => { + describe('$onChanges()', () => { it(`should call updateOriginalData() if controllers data is defined`, () => { let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}); controller.data = []; @@ -57,8 +57,8 @@ describe('Component vnWatcher', () => { }); }); - describe('$onDestroy() ', () => { - it(`should allways call deregisterCallback()`, () => { + describe('$onDestroy()', () => { + it(`should call deregisterCallback()`, () => { let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}); spyOn(controller, 'deregisterCallback'); controller.$onDestroy(); @@ -66,4 +66,95 @@ describe('Component vnWatcher', () => { expect(controller.deregisterCallback).toHaveBeenCalledWith(); }); }); + + describe('fetchData()', () => { + it(`should perform a query then store the received data into controller.data and call updateOriginalData()`, () => { + let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}); + spyOn(controller, 'updateOriginalData'); + let json = {data: 'some data'}; + controller.data = [1]; + controller.idField = 0; + controller.url = 'test.com'; + $httpBackend.whenGET('test.com/1').respond(json); + $httpBackend.expectGET('test.com/1'); + controller.fetchData(); + $httpBackend.flush(); + + expect(controller.data).toEqual({data: 'some data'}); + expect(controller.updateOriginalData).toHaveBeenCalledWith(); + }); + }); + + describe('submitBack()', () => { + it(`should call controller.window.history.back() function after calling controllers submit() function`, done => { + let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}); + spyOn(controller, 'submit').and.returnValue(Promise.resolve()); + spyOn(controller.window.history, 'back'); + controller.submitBack() + .then(() => { + expect(controller.submit).toHaveBeenCalledWith(); + expect(controller.window.history.back).toHaveBeenCalledWith(); + done(); + }); + }); + }); + + describe('submitGo()', () => { + it(`should call controller.$state.go() function after calling controllers submit() function`, done => { + let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}); + spyOn(controller, 'submit').and.returnValue(Promise.resolve()); + spyOn(controller.$state, 'go'); + let state = 'the state'; + controller.submitGo(state) + .then(() => { + expect(controller.submit).toHaveBeenCalledWith(); + expect(controller.$state.go).toHaveBeenCalledWith(state); + done(); + }); + }); + }); + + describe('submit()', () => { + describe('when controller.form', () => { + it(`should call controller.form.setSubminted if controller.form is defined`, () => { + let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}); + controller.form = {$setSubmitted: () => {}}; + spyOn(controller.form, '$setSubmitted'); + controller.submit(); + + expect(controller.form.$setSubmitted).toHaveBeenCalledWith(); + }); + + it(`should call controller.invalidForm if controller.form.$valid is not defined`, () => { + let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}); + controller.form = {$setSubmitted: () => {}}; + spyOn(controller, 'invalidForm'); + controller.submit(); + + expect(controller.invalidForm).toHaveBeenCalledWith(jasmine.any(Function)); + }); + }); + + describe('when !controller.dataChanged()', () => { + it(`should call controller.noChanges()`, () => { + let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}); + spyOn(controller, 'noChanges'); + controller.submit(); + + expect(controller.noChanges).toHaveBeenCalledWith(jasmine.any(Function)); + }); + }); + + describe('when controller.save()', () => { + it(`should set controller.save.model property`, () => { + let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate}); + controller.save = {}; + controller.data = {originalInfo: 'original data', info: 'new data'}; + controller.orgData = {originalInfo: 'original data'}; + controller.submit(); + + expect(controller.save.model).toEqual({info: 'new data'}); + }); + }); + }); }); diff --git a/jasmine.js b/jasmine.js deleted file mode 100644 index e1fdf9fdd1..0000000000 --- a/jasmine.js +++ /dev/null @@ -1,9 +0,0 @@ -const Jasmine = require('jasmine'); -const jasmine = new Jasmine(); - -jasmine.loadConfig({ - spec_dir: '', - spec_files: ['*[Ss]pec.js'] -}); - -jasmine.execute();