diff --git a/client/item/src/tax/index.js b/client/item/src/tax/index.js index 5e5e108f1..bf3be6510 100644 --- a/client/item/src/tax/index.js +++ b/client/item/src/tax/index.js @@ -6,7 +6,13 @@ export default class Controller { this.$stateParams = $stateParams; this._ = $translate; this.vnApp = vnApp; + } + $onInit() { + this.getTaxes(); + } + + getTaxes() { let filter = { fields: ['id', 'countryFk', 'taxClassFk'], include: [{ @@ -16,8 +22,8 @@ export default class Controller { }; let urlFilter = encodeURIComponent(JSON.stringify(filter)); - let url = `/item/api/Items/${$stateParams.id}/taxes?filter=${urlFilter}`; - $http.get(url).then(json => { + let url = `/item/api/Items/${this.$stateParams.id}/taxes?filter=${urlFilter}`; + this.$http.get(url).then(json => { this.taxes = json.data; }); } @@ -38,8 +44,5 @@ Controller.$inject = ['$stateParams', '$http', '$translate', 'vnApp']; ngModule.component('vnItemTax', { template: require('./index.html'), - controller: Controller, - bindings: { - item: '<' - } + controller: Controller }); diff --git a/client/item/src/tax/index.spec.js b/client/item/src/tax/index.spec.js new file mode 100644 index 000000000..6691211d5 --- /dev/null +++ b/client/item/src/tax/index.spec.js @@ -0,0 +1,70 @@ +import './index.js'; + +describe('Item', () => { + describe('Component vnItemTax', () => { + let $componentController; + let $stateParams; + let controller; + let $httpBackend; + let vnApp; + + beforeEach(() => { + angular.mock.module('item'); + }); + + beforeEach(angular.mock.inject((_$componentController_, _$httpBackend_, _$stateParams_, _vnApp_) => { + $componentController = _$componentController_; + $stateParams = _$stateParams_; + $stateParams.id = 1; + $httpBackend = _$httpBackend_; + $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); + vnApp = _vnApp_; + spyOn(vnApp, 'showSuccess'); + controller = $componentController('vnItemTax', {$state: $stateParams}); + })); + + describe('getTaxes()', () => { + it("should perform a query to set the array of taxes into the controller", () => { + let filter = { + fields: ['id', 'countryFk', 'taxClassFk'], + include: [{ + relation: 'country', + scope: {fields: ['country']} + }] + }; + let response = [{id: 1, taxClassFk: 1}]; + filter = encodeURIComponent(JSON.stringify(filter)); + $httpBackend.when('GET', `/item/api/Items/1/taxes?filter=${filter}`).respond(response); + $httpBackend.expect('GET', `/item/api/Items/1/taxes?filter=${filter}`); + controller.$onInit(); + $httpBackend.flush(); + + expect(controller.taxes).toEqual(response); + }); + }); + + describe('submit()', () => { + it("should perform a post to update taxes", () => { + let filter = { + fields: ['id', 'countryFk', 'taxClassFk'], + include: [{ + relation: 'country', + scope: {fields: ['country']} + }] + }; + let response = [{id: 1, taxClassFk: 1}]; + filter = encodeURIComponent(JSON.stringify(filter)); + $httpBackend.when('GET', `/item/api/Items/1/taxes?filter=${filter}`).respond(response); + controller.$onInit(); + $httpBackend.flush(); + + $httpBackend.when('POST', `/item/api/Items/1/updateTaxes`).respond('ok'); + $httpBackend.expect('POST', `/item/api/Items/1/updateTaxes`); + controller.submit(); + $httpBackend.flush(); + + expect(vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); + }); + }); + }); +});