#507 unitarios front tax plus refactor

This commit is contained in:
Carlos Jimenez 2018-08-09 13:13:50 +02:00
parent 55c13604e6
commit 8f46a455d6
2 changed files with 79 additions and 6 deletions

View File

@ -6,7 +6,13 @@ export default class Controller {
this.$stateParams = $stateParams; this.$stateParams = $stateParams;
this._ = $translate; this._ = $translate;
this.vnApp = vnApp; this.vnApp = vnApp;
}
$onInit() {
this.getTaxes();
}
getTaxes() {
let filter = { let filter = {
fields: ['id', 'countryFk', 'taxClassFk'], fields: ['id', 'countryFk', 'taxClassFk'],
include: [{ include: [{
@ -16,8 +22,8 @@ export default class Controller {
}; };
let urlFilter = encodeURIComponent(JSON.stringify(filter)); let urlFilter = encodeURIComponent(JSON.stringify(filter));
let url = `/item/api/Items/${$stateParams.id}/taxes?filter=${urlFilter}`; let url = `/item/api/Items/${this.$stateParams.id}/taxes?filter=${urlFilter}`;
$http.get(url).then(json => { this.$http.get(url).then(json => {
this.taxes = json.data; this.taxes = json.data;
}); });
} }
@ -38,8 +44,5 @@ Controller.$inject = ['$stateParams', '$http', '$translate', 'vnApp'];
ngModule.component('vnItemTax', { ngModule.component('vnItemTax', {
template: require('./index.html'), template: require('./index.html'),
controller: Controller, controller: Controller
bindings: {
item: '<'
}
}); });

View File

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