From 92e17545716a20dc2a504c8e8ddb31b97f6d8a3a Mon Sep 17 00:00:00 2001 From: Carlos Date: Wed, 4 Oct 2017 13:27:39 +0200 Subject: [PATCH] client unit test for paging plus rly small refactors --- client/core/src/multi-check/multi-check.js | 8 +++ client/core/src/paging/paging.js | 9 ++- client/core/src/paging/paging.spec.js | 72 ++++++++++++++++++++++ client/core/src/watcher/watcher.js | 13 ++++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 client/core/src/paging/paging.spec.js diff --git a/client/core/src/multi-check/multi-check.js b/client/core/src/multi-check/multi-check.js index ba3f7b79f..3447e9b46 100644 --- a/client/core/src/multi-check/multi-check.js +++ b/client/core/src/multi-check/multi-check.js @@ -13,19 +13,24 @@ export default class MultiCheck { this.type = {}; this.showDropDown = false; } + get models() { return this._models; } + set models(value) { this._models = value; } + get checkAll() { return this._checkAll; } + set checkAll(value) { this._checkAll = value; this.switchChecks(); } + switchChecks() { if (this.models) this.models.forEach( @@ -47,10 +52,12 @@ export default class MultiCheck { } ); } + $onChanges() { this.type = {}; this.checkAll = 0; } + $doCheck() { if (this.type && this.type.id) { switch (this.type.id) { @@ -68,6 +75,7 @@ export default class MultiCheck { } } } + MultiCheck.$inject = []; module.component('vnMultiCheck', { diff --git a/client/core/src/paging/paging.js b/client/core/src/paging/paging.js index e04f9ee5e..02d1a7fc0 100644 --- a/client/core/src/paging/paging.js +++ b/client/core/src/paging/paging.js @@ -5,6 +5,7 @@ export default class Paging { get numPages() { return Math.ceil(this.numItems / this.numPerPage); } + constructor($http, $scope) { this.$http = $http; this.$scope = $scope; @@ -13,6 +14,7 @@ export default class Paging { this.numItems = 0; $scope.$watch('$ctrl.index.model.length', () => this.onModelUpdated()); } + $onChanges(changes) { if (!this.index) return; this.numPerPage = this.index.filter.size; @@ -20,14 +22,15 @@ export default class Paging { if (changes.total) this.numItems = changes.total.currentValue; } + onModelUpdated() { let index = this.index; let filter = index.filter; - if (filter.page >= this.numPages - && index.model.length >= this.numPerPage) + if (filter.page >= this.numPages && index.model.length >= this.numPerPage) this.numItems = filter.page * filter.size + 1; } + onPageChange(page) { this.index.filter.page = page; if (typeof this.pageChange === 'undefined') { @@ -37,6 +40,7 @@ export default class Paging { } } } + Paging.$inject = ['$http', '$scope']; export const NAME = 'vnPaging'; @@ -49,4 +53,5 @@ export const COMPONENT = { }, controller: Paging }; + module.component(NAME, COMPONENT); diff --git a/client/core/src/paging/paging.spec.js b/client/core/src/paging/paging.spec.js new file mode 100644 index 000000000..de13f183f --- /dev/null +++ b/client/core/src/paging/paging.spec.js @@ -0,0 +1,72 @@ +import './paging.js'; + +describe('Component vnPaging', () => { + let $componentController; + let $scope; + let $httpBackend; + + beforeEach(() => { + angular.mock.module('client'); + }); + + beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_) => { + $componentController = _$componentController_; + $scope = $rootScope.$new(); + $httpBackend = _$httpBackend_; + })); + + describe('$onChanges()', () => { + it(`should define numberPage and currentPage based on index.filter properties`, () => { + let controller = $componentController('vnPaging', {$scope, $httpBackend}); + controller.index = {filter: {size: 'something', page: 'something else'}}; + controller.$onChanges({index: 'simpleChange', currentValue: 'current value', previousValue: 'previous value'}); + + expect(controller.numPerPage).toBe(controller.index.filter.size); + expect(controller.currentPage).toEqual(controller.index.filter.page); + }); + + it(`should define numItems based on changes.total.currentValue`, () => { + let controller = $componentController('vnPaging', {$scope, $httpBackend}); + controller.index = {filter: {size: 'something', page: 'something else'}}; + controller.$onChanges({total: {currentValue: 'current value'}}); + + expect(controller.numItems).toEqual('current value'); + }); + }); + + describe('onModelUpdated()', () => { + it(`should define controllers numItems as the result of page times size plus one`, () => { + let controller = $componentController('vnPaging', {$scope, $httpBackend}); + controller.numPerPage = 2; + controller.index = { + filter: {size: 10, page: 10}, + model: ['one mother..', 'another model..', 'last model..'] + }; + controller.onModelUpdated(); + + expect(controller.numItems).toBe(101); + }); + }); + + describe('onPageChange()', () => { + it(`should call accept() since pageChange property is undefined`, () => { + let myIndex = {accept: () => {}, filter: {page: 0}}; + let controller = $componentController('vnPaging', {$scope, $httpBackend}, {index: myIndex}); + spyOn(controller.index, 'accept'); + controller.onPageChange(100); + + expect(controller.index.accept).toHaveBeenCalledWith(); + }); + + it(`should call pageChange() since pageChange property isn't undefined`, () => { + let myIndex = {accept: () => {}, filter: {page: 0}}; + let controller = $componentController('vnPaging', {$scope, $httpBackend}, {index: myIndex}); + controller.pageChange = true; + spyOn(controller, 'pageChange'); + controller.onPageChange(100); + + expect(controller.pageChange).toHaveBeenCalledWith(); + }); + }); +}); + diff --git a/client/core/src/watcher/watcher.js b/client/core/src/watcher/watcher.js index 3ee1b5d4e..bf085db4e 100644 --- a/client/core/src/watcher/watcher.js +++ b/client/core/src/watcher/watcher.js @@ -24,6 +24,7 @@ export default class Watcher extends Component { transition => this.callback(transition)); this.copyData(); } + $onInit() { if (this.get && this.url) { this.fetchData(); @@ -31,14 +32,17 @@ export default class Watcher extends Component { throw new Error('Error: Parameter url ommitted'); } } + $onChanges(changes) { if (this.data) { this.copyData(); } } + $onDestroy() { this.deregisterCallback(); } + fetchData() { let id = this.data[this.idField]; // return new Promise((resolve, reject) => { @@ -120,6 +124,7 @@ export default class Watcher extends Component { ); }); } + writeData(json, resolve) { Object.keys(this.data).forEach( key => { @@ -130,21 +135,25 @@ export default class Watcher extends Component { this.copyData(); resolve(json); } + noChanges(resolve) { this.vnApp.showMessage( this.$translate.instant('No changes to save') ); resolve(); } + invalidForm(resolve) { this.vnApp.showMessage( this.$translate.instant('Some fields are invalid') ); resolve(); } + copyData() { this.orgData = this.copyObject(this.data); } + copyObject(data) { let copy = {}; if (data) { @@ -157,6 +166,7 @@ export default class Watcher extends Component { } return copy; } + callback(transition) { let dataChanged = this.dataChanged(); if (!this.state && dataChanged) { @@ -167,10 +177,12 @@ export default class Watcher extends Component { return true; } + dataChanged() { let newData = this.copyObject(this.data); return !isEqual(newData, this.orgData); } + onConfirmResponse(response) { if (response === 'ACCEPT') { this.data = this.copyObject(this.orgData); @@ -180,6 +192,7 @@ export default class Watcher extends Component { } } } + Watcher.$inject = ['$element', '$scope', '$state', '$transitions', '$http', 'vnApp', '$translate']; module.component('vnWatcher', {