client unit test for paging plus rly small refactors

This commit is contained in:
Carlos 2017-10-04 13:27:39 +02:00
parent 6963051974
commit 92e1754571
4 changed files with 100 additions and 2 deletions

View File

@ -13,19 +13,24 @@ export default class MultiCheck {
this.type = {}; this.type = {};
this.showDropDown = false; this.showDropDown = false;
} }
get models() { get models() {
return this._models; return this._models;
} }
set models(value) { set models(value) {
this._models = value; this._models = value;
} }
get checkAll() { get checkAll() {
return this._checkAll; return this._checkAll;
} }
set checkAll(value) { set checkAll(value) {
this._checkAll = value; this._checkAll = value;
this.switchChecks(); this.switchChecks();
} }
switchChecks() { switchChecks() {
if (this.models) if (this.models)
this.models.forEach( this.models.forEach(
@ -47,10 +52,12 @@ export default class MultiCheck {
} }
); );
} }
$onChanges() { $onChanges() {
this.type = {}; this.type = {};
this.checkAll = 0; this.checkAll = 0;
} }
$doCheck() { $doCheck() {
if (this.type && this.type.id) { if (this.type && this.type.id) {
switch (this.type.id) { switch (this.type.id) {
@ -68,6 +75,7 @@ export default class MultiCheck {
} }
} }
} }
MultiCheck.$inject = []; MultiCheck.$inject = [];
module.component('vnMultiCheck', { module.component('vnMultiCheck', {

View File

@ -5,6 +5,7 @@ export default class Paging {
get numPages() { get numPages() {
return Math.ceil(this.numItems / this.numPerPage); return Math.ceil(this.numItems / this.numPerPage);
} }
constructor($http, $scope) { constructor($http, $scope) {
this.$http = $http; this.$http = $http;
this.$scope = $scope; this.$scope = $scope;
@ -13,6 +14,7 @@ export default class Paging {
this.numItems = 0; this.numItems = 0;
$scope.$watch('$ctrl.index.model.length', () => this.onModelUpdated()); $scope.$watch('$ctrl.index.model.length', () => this.onModelUpdated());
} }
$onChanges(changes) { $onChanges(changes) {
if (!this.index) return; if (!this.index) return;
this.numPerPage = this.index.filter.size; this.numPerPage = this.index.filter.size;
@ -20,14 +22,15 @@ export default class Paging {
if (changes.total) if (changes.total)
this.numItems = changes.total.currentValue; this.numItems = changes.total.currentValue;
} }
onModelUpdated() { onModelUpdated() {
let index = this.index; let index = this.index;
let filter = index.filter; let filter = index.filter;
if (filter.page >= this.numPages if (filter.page >= this.numPages && index.model.length >= this.numPerPage)
&& index.model.length >= this.numPerPage)
this.numItems = filter.page * filter.size + 1; this.numItems = filter.page * filter.size + 1;
} }
onPageChange(page) { onPageChange(page) {
this.index.filter.page = page; this.index.filter.page = page;
if (typeof this.pageChange === 'undefined') { if (typeof this.pageChange === 'undefined') {
@ -37,6 +40,7 @@ export default class Paging {
} }
} }
} }
Paging.$inject = ['$http', '$scope']; Paging.$inject = ['$http', '$scope'];
export const NAME = 'vnPaging'; export const NAME = 'vnPaging';
@ -49,4 +53,5 @@ export const COMPONENT = {
}, },
controller: Paging controller: Paging
}; };
module.component(NAME, COMPONENT); module.component(NAME, COMPONENT);

View File

@ -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();
});
});
});

View File

@ -24,6 +24,7 @@ export default class Watcher extends Component {
transition => this.callback(transition)); transition => this.callback(transition));
this.copyData(); this.copyData();
} }
$onInit() { $onInit() {
if (this.get && this.url) { if (this.get && this.url) {
this.fetchData(); this.fetchData();
@ -31,14 +32,17 @@ export default class Watcher extends Component {
throw new Error('Error: Parameter url ommitted'); throw new Error('Error: Parameter url ommitted');
} }
} }
$onChanges(changes) { $onChanges(changes) {
if (this.data) { if (this.data) {
this.copyData(); this.copyData();
} }
} }
$onDestroy() { $onDestroy() {
this.deregisterCallback(); this.deregisterCallback();
} }
fetchData() { fetchData() {
let id = this.data[this.idField]; let id = this.data[this.idField];
// return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
@ -120,6 +124,7 @@ export default class Watcher extends Component {
); );
}); });
} }
writeData(json, resolve) { writeData(json, resolve) {
Object.keys(this.data).forEach( Object.keys(this.data).forEach(
key => { key => {
@ -130,21 +135,25 @@ export default class Watcher extends Component {
this.copyData(); this.copyData();
resolve(json); resolve(json);
} }
noChanges(resolve) { noChanges(resolve) {
this.vnApp.showMessage( this.vnApp.showMessage(
this.$translate.instant('No changes to save') this.$translate.instant('No changes to save')
); );
resolve(); resolve();
} }
invalidForm(resolve) { invalidForm(resolve) {
this.vnApp.showMessage( this.vnApp.showMessage(
this.$translate.instant('Some fields are invalid') this.$translate.instant('Some fields are invalid')
); );
resolve(); resolve();
} }
copyData() { copyData() {
this.orgData = this.copyObject(this.data); this.orgData = this.copyObject(this.data);
} }
copyObject(data) { copyObject(data) {
let copy = {}; let copy = {};
if (data) { if (data) {
@ -157,6 +166,7 @@ export default class Watcher extends Component {
} }
return copy; return copy;
} }
callback(transition) { callback(transition) {
let dataChanged = this.dataChanged(); let dataChanged = this.dataChanged();
if (!this.state && dataChanged) { if (!this.state && dataChanged) {
@ -167,10 +177,12 @@ export default class Watcher extends Component {
return true; return true;
} }
dataChanged() { dataChanged() {
let newData = this.copyObject(this.data); let newData = this.copyObject(this.data);
return !isEqual(newData, this.orgData); return !isEqual(newData, this.orgData);
} }
onConfirmResponse(response) { onConfirmResponse(response) {
if (response === 'ACCEPT') { if (response === 'ACCEPT') {
this.data = this.copyObject(this.orgData); 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']; Watcher.$inject = ['$element', '$scope', '$state', '$transitions', '$http', 'vnApp', '$translate'];
module.component('vnWatcher', { module.component('vnWatcher', {