diff --git a/modules/zone/front/descriptor/index.js b/modules/zone/front/descriptor/index.js index 288267bfe..5e365e0ac 100644 --- a/modules/zone/front/descriptor/index.js +++ b/modules/zone/front/descriptor/index.js @@ -33,6 +33,7 @@ class Controller extends Descriptor { this.vnApp.showSuccess(this.$t('Zone deleted')); }); } + onCloneAccept() { return this.$http.post(`Zones/${this.id}/clone`). then(res => this.$state.go('zone.card.basicData', {id: res.data.id})); diff --git a/modules/zone/front/descriptor/index.spec.js b/modules/zone/front/descriptor/index.spec.js new file mode 100644 index 000000000..23c6e3a3a --- /dev/null +++ b/modules/zone/front/descriptor/index.spec.js @@ -0,0 +1,74 @@ +import './index.js'; + +describe('Zone descriptor', () => { + let $httpBackend; + let controller; + let $element; + + beforeEach(ngModule('zone')); + + beforeEach(angular.mock.inject(($componentController, _$httpBackend_) => { + $httpBackend = _$httpBackend_; + $element = angular.element(' {}, + show: () => {} + }; + })); + + describe('onDelete()', () => { + it('should make an HTTP POST query and then call the deleteZone show() method', () => { + jest.spyOn(controller.$.deleteZone, 'show'); + + const expectedData = [{id: 16}]; + $httpBackend.when('GET', 'Tickets').respond(expectedData); + controller.onDelete(); + $httpBackend.flush(); + + expect(controller.$.deleteZone.show).toHaveBeenCalledWith(); + }); + + it('should make an HTTP POST query and then call the deleteZone() method', () => { + jest.spyOn(controller, 'deleteZone').mockReturnThis(); + + const expectedData = []; + $httpBackend.when('GET', 'Tickets').respond(expectedData); + controller.onDelete(); + $httpBackend.flush(); + + expect(controller.deleteZone).toHaveBeenCalledWith(); + }); + }); + + describe('deleteZone()', () => { + it('should make an HTTP POST query and then call the showMessage() method', () => { + jest.spyOn(controller.$state, 'go').mockReturnThis(); + jest.spyOn(controller.vnApp, 'showSuccess'); + + const stateName = 'zone.index'; + $httpBackend.when('POST', 'Zones/1/deleteZone').respond(200); + controller.deleteZone(); + $httpBackend.flush(); + + expect(controller.$state.go).toHaveBeenCalledWith(stateName); + expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Zone deleted'); + }); + }); + + describe('onCloneAccept()', () => { + it('should make an HTTP POST query and then call the state go() method', () => { + jest.spyOn(controller.$state, 'go').mockReturnThis(); + + const stateName = 'zone.card.basicData'; + const expectedData = {id: 1}; + $httpBackend.when('POST', 'Zones/1/clone').respond(expectedData); + controller.onCloneAccept(); + $httpBackend.flush(); + + expect(controller.$state.go).toHaveBeenCalledWith(stateName, expectedData); + }); + }); +}); diff --git a/modules/zone/front/summary/index.spec.js b/modules/zone/front/summary/index.spec.js index 0956fa8c6..49d6614ff 100644 --- a/modules/zone/front/summary/index.spec.js +++ b/modules/zone/front/summary/index.spec.js @@ -51,4 +51,26 @@ describe('component vnZoneSummary', () => { expect(controller.summary).toBeDefined(); }); }); + + describe('getWarehouses()', () => { + it('should make an HTTP get query and then store data on the controller', () => { + controller._zone = {id: 1}; + const params = { + filter: { + include: { + relation: 'warehouse', + fields: ['name'] + } + } + }; + + const serializedParams = $httpParamSerializer(params); + const query = `Zones/1/warehouses?${serializedParams}`; + $httpBackend.expect('GET', query).respond([{id: 1}]); + controller.getWarehouses(); + $httpBackend.flush(); + + expect(controller.zoneWarehouses.length).toEqual(1); + }); + }); }); diff --git a/modules/zone/front/warehouses/index.js b/modules/zone/front/warehouses/index.js index 9191a1f49..f428561fd 100644 --- a/modules/zone/front/warehouses/index.js +++ b/modules/zone/front/warehouses/index.js @@ -2,13 +2,14 @@ import ngModule from '../module'; import Section from 'salix/components/section'; class Controller extends Section { - constructor($element, $) { - super($element, $); - - this.path = `Zones/${this.$params.id}/warehouses`; + $onInit() { this.refresh(); } + get path() { + return `Zones/${this.$params.id}/warehouses`; + } + refresh() { let filter = {include: 'warehouse'}; this.$http.get(this.path, {params: {filter}}) diff --git a/modules/zone/front/warehouses/index.spec.js b/modules/zone/front/warehouses/index.spec.js new file mode 100644 index 000000000..aa9cef2e1 --- /dev/null +++ b/modules/zone/front/warehouses/index.spec.js @@ -0,0 +1,60 @@ +import './index.js'; + +describe('Zone warehouses', () => { + let $httpBackend; + let $httpParamSerializer; + let controller; + let $element; + + beforeEach(ngModule('zone')); + + beforeEach(angular.mock.inject(($componentController, _$httpBackend_, _$httpParamSerializer_) => { + $httpBackend = _$httpBackend_; + $httpParamSerializer = _$httpParamSerializer_; + $element = angular.element(' { + it('should make an HTTP GET query and then set the data', () => { + const params = {filter: {include: 'warehouse'}}; + const serializedParams = $httpParamSerializer(params); + const path = `Zones/1/warehouses?${serializedParams}`; + $httpBackend.expect('GET', path).respond([{id: 1, name: 'Warehouse one'}]); + controller.refresh(); + $httpBackend.flush(); + + expect(controller.$.data).toBeDefined(); + }); + }); + + describe('onSave()', () => { + it('should make an HTTP POST query and then call the refresh() method', () => { + jest.spyOn(controller, 'refresh').mockReturnThis(); + + $httpBackend.expect('POST', `Zones/1/warehouses`).respond(200); + controller.onSave('accept'); + $httpBackend.flush(); + + expect(controller.selected).toBeNull(); + expect(controller.isNew).toBeNull(); + expect(controller.$.dialog.hide).toHaveBeenCalledWith(); + expect(controller.refresh).toHaveBeenCalledWith(); + }); + }); + + describe('delete()', () => { + it('should make an HTTP DELETE query and then set deleteRow property to null value', () => { + controller.deleteRow = {id: 1}; + controller.$.data = [{id: 1}]; + $httpBackend.expect('DELETE', `Zones/1/warehouses/1`).respond(200); + controller.delete(); + $httpBackend.flush(); + + expect(controller.deleteRow).toBeNull(); + }); + }); +});