$applyAsync() with relocate() on catalog. Replaced $$postDigest() #1072

This commit is contained in:
Joan Sanchez 2019-02-13 07:57:08 +01:00
parent 8fa7abe191
commit 7f96eae312
6 changed files with 44 additions and 27 deletions

View File

@ -10,7 +10,7 @@ class Controller {
}
onSearch() {
this.$scope.$$postDigest(() => {
this.$scope.$applyAsync(() => {
this.$scope.treeview.refresh();
});
}

View File

@ -22,7 +22,7 @@ class Controller {
where: {itemFk: this.$stateParams.id}
};
this.$scope.$$postDigest(() => {
this.$scope.$applyAsync(() => {
if (this.$stateParams.warehouseFk)
this.warehouseFk = this.$stateParams.warehouseFk;
else if (value)

View File

@ -69,23 +69,23 @@ describe('Item', () => {
describe('set item()', () => {
it(`should set warehouseFk property based on itemType warehouseFk`, () => {
spyOn(controller.$scope, '$$postDigest').and.callThrough();
spyOn(controller.$scope, '$applyAsync').and.callThrough();
controller.item = {id: 1, itemType: {warehouseFk: 1}};
expect(controller.$scope.$$postDigest).toHaveBeenCalledWith(jasmine.any(Function));
$scope.$digest();
expect(controller.$scope.$applyAsync).toHaveBeenCalledWith(jasmine.any(Function));
$scope.$apply();
expect(controller.warehouseFk).toEqual(1);
expect(controller.item.id).toEqual(1);
});
it(`should set warehouseFk property based on url query warehouseFk`, () => {
spyOn(controller.$scope, '$$postDigest').and.callThrough();
spyOn(controller.$scope, '$applyAsync').and.callThrough();
controller.$stateParams.warehouseFk = 4;
controller.item = {id: 1, itemType: {warehouseFk: 1}};
expect(controller.$scope.$$postDigest).toHaveBeenCalledWith(jasmine.any(Function));
$scope.$digest();
expect(controller.$scope.$applyAsync).toHaveBeenCalledWith(jasmine.any(Function));
$scope.$apply();
expect(controller.warehouseFk).toEqual(4);
expect(controller.item.id).toEqual(1);

View File

@ -28,7 +28,7 @@ class Controller {
this._order = value;
this.$scope.$$postDigest(() => {
this.$scope.$applyAsync(() => {
let category;
let type;

View File

@ -6,10 +6,12 @@ describe('Order', () => {
let $scope;
let $state;
let controller;
let $httpBackend;
beforeEach(ngModule('order'));
beforeEach(angular.mock.inject(($componentController, _$state_, $rootScope) => {
beforeEach(angular.mock.inject(($componentController, _$state_, _$httpBackend_, $rootScope) => {
$httpBackend = _$httpBackend_;
$scope = $rootScope.$new();
$scope.model = crudModel;
$scope.search = {};
@ -27,12 +29,14 @@ describe('Order', () => {
}));
describe('order() setter', () => {
it(`should call scope $$postDigest() method and apply filters from state params`, () => {
spyOn(controller.$scope, '$$postDigest').and.callThrough();
it(`should call scope $applyAsync() method and apply filters from state params`, () => {
$httpBackend.expect('GET', `/item/api/ItemCategories/1/itemTypes`).respond();
spyOn(controller.$scope, '$applyAsync').and.callThrough();
controller.order = {id: 4};
expect(controller.$scope.$$postDigest).toHaveBeenCalledWith(jasmine.any(Function));
$scope.$digest();
expect(controller.$scope.$applyAsync).toHaveBeenCalledWith(jasmine.any(Function));
$scope.$apply();
expect(controller.category).toEqual({id: 1, value: 'My Category'});
expect(controller.type).toEqual({id: 1, value: 'My type'});
@ -96,28 +100,38 @@ describe('Order', () => {
});
describe('applyFilters()', () => {
it(`should set type property to null, call updateStateParams() method and not call applyFilters()`, () => {
spyOn(controller.catalog.$scope.model, 'applyFilter');
controller.order = {id: 4};
$scope.$digest();
it(`should call model applyFilter() method with a new filter`, () => {
let model = controller.catalog.$scope.model;
spyOn(model, 'applyFilter');
controller._category = {id: 1, value: 'My Category'};
controller._type = {id: 1, value: 'My type'};
controller._order = {id: 4};
controller.applyFilters();
expect(controller.catalog.$scope.model.applyFilter).toHaveBeenCalledWith(
expect(model.applyFilter).toHaveBeenCalledWith(
{where: {categoryFk: 1, typeFk: 1}},
{orderFk: 4, orderBy: controller.catalog.getOrderBy(), tags: []});
});
});
describe('remove()', () => {
it(`should remove a filter from tags property and then call applyFilters()`, () => {
spyOn(controller, 'applyFilters');
controller.order = {id: 4};
it(`should remove a tag from tags property`, () => {
controller.tags = [{tagFk: 1, value: 'Blue'}, {tagFk: 2, value: '70'}];
$scope.$digest();
controller.remove(0);
expect(controller.tags.length).toEqual(1);
expect(controller.tags[0].tagFk).toEqual(2);
});
it(`should remove a tag from tags property and call applyFilters() if there's no more tags`, () => {
spyOn(controller, 'applyFilters');
controller._category = {id: 1, value: 'My Category'};
controller._type = {id: 1, value: 'My type'};
controller.tags = [{tagFk: 1, value: 'Blue'}];
controller.remove(0);
expect(controller.tags.length).toEqual(0);
expect(controller.applyFilters).toHaveBeenCalledWith();
});
});
@ -125,10 +139,10 @@ describe('Order', () => {
describe('updateStateParams()', () => {
it(`should call state go() method passing category and type state params`, () => {
spyOn(controller.$state, 'go');
controller.order = {id: 4};
$scope.$digest();
controller._category = {id: 1, value: 'My Category'};
controller._type = {id: 1, value: 'My type'};
let result = {category: '{"id":1,"value":"My Category"}', type: '{"id":1,"value":"My type"}'};
controller.updateStateParams();
expect(controller.$state.go).toHaveBeenCalledWith('my.current.state', result);
});

View File

@ -35,6 +35,9 @@ class Controller {
};
this.$http.get(`/item/api/ItemTags?filter=${JSON.stringify(filter)}`).then(response => {
this.tags = response.data;
this.$.$applyAsync(() => {
this.$.popover.relocate();
});
});
}
show(event, item) {
@ -42,8 +45,8 @@ class Controller {
this.prices = this.item.prices;
this.getTags();
this.$.popover.parent = event.target;
this.$.popover.relocate();
this.$.popover.show();
this.$.popover.relocate();
}
clear() {
this.item = {};