client unit test for paging plus rly small refactors
This commit is contained in:
parent
6963051974
commit
92e1754571
|
@ -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', {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -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', {
|
||||
|
|
Loading…
Reference in New Issue