From 7f96eae312cb898a004e0da9933b3d762274a95a Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Wed, 13 Feb 2019 07:57:08 +0100 Subject: [PATCH] $applyAsync() with relocate() on catalog. Replaced $$postDigest() #1072 --- modules/agency/front/location/index.js | 2 +- modules/item/front/diary/index.js | 2 +- modules/item/front/diary/index.spec.js | 12 +++--- modules/order/front/filter/index.js | 2 +- modules/order/front/filter/index.spec.js | 48 +++++++++++++-------- modules/order/front/prices-popover/index.js | 5 ++- 6 files changed, 44 insertions(+), 27 deletions(-) diff --git a/modules/agency/front/location/index.js b/modules/agency/front/location/index.js index cdb89829a..92e893297 100644 --- a/modules/agency/front/location/index.js +++ b/modules/agency/front/location/index.js @@ -10,7 +10,7 @@ class Controller { } onSearch() { - this.$scope.$$postDigest(() => { + this.$scope.$applyAsync(() => { this.$scope.treeview.refresh(); }); } diff --git a/modules/item/front/diary/index.js b/modules/item/front/diary/index.js index 6745ad94c..c2e5e6bff 100644 --- a/modules/item/front/diary/index.js +++ b/modules/item/front/diary/index.js @@ -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) diff --git a/modules/item/front/diary/index.spec.js b/modules/item/front/diary/index.spec.js index 454a8b25f..9286f2863 100644 --- a/modules/item/front/diary/index.spec.js +++ b/modules/item/front/diary/index.spec.js @@ -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); diff --git a/modules/order/front/filter/index.js b/modules/order/front/filter/index.js index 9f1013b35..c2bfb8e4b 100644 --- a/modules/order/front/filter/index.js +++ b/modules/order/front/filter/index.js @@ -28,7 +28,7 @@ class Controller { this._order = value; - this.$scope.$$postDigest(() => { + this.$scope.$applyAsync(() => { let category; let type; diff --git a/modules/order/front/filter/index.spec.js b/modules/order/front/filter/index.spec.js index 3d9888c57..c31e87500 100644 --- a/modules/order/front/filter/index.spec.js +++ b/modules/order/front/filter/index.spec.js @@ -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); }); diff --git a/modules/order/front/prices-popover/index.js b/modules/order/front/prices-popover/index.js index c44ff7109..e89f04774 100644 --- a/modules/order/front/prices-popover/index.js +++ b/modules/order/front/prices-popover/index.js @@ -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 = {};