From 8c631012b14a4d41caa650d8b11a45994e9bdd72 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Thu, 22 Mar 2018 10:57:55 +0100 Subject: [PATCH] ticket notes logic refactor plus unit tests --- client/ticket/routes.json | 4 +- .../ticket-observation.html} | 5 ++- .../ticket-observation.js} | 12 +++--- .../ticket-observation.spec.js} | 37 ++++++++++++++----- services/loopback/common/models/ticket.js | 1 + .../methods/notes/crudTicketObservation.js | 3 ++ .../methods/ticket/crudTicketObservations.js | 3 -- .../ticket/common/models/ticketObservation.js | 2 +- 8 files changed, 43 insertions(+), 24 deletions(-) rename client/ticket/src/{notes/ticket-observations.html => note/ticket-observation.html} (93%) rename client/ticket/src/{notes/ticket-observations.js => note/ticket-observation.js} (92%) rename client/ticket/src/{notes/ticket-observations.spec.js => note/ticket-observation.spec.js} (84%) create mode 100644 services/ticket/common/methods/notes/crudTicketObservation.js delete mode 100644 services/ticket/common/methods/ticket/crudTicketObservations.js diff --git a/client/ticket/routes.json b/client/ticket/routes.json index 5328c8fc9..b9ee8a7f1 100644 --- a/client/ticket/routes.json +++ b/client/ticket/routes.json @@ -48,8 +48,8 @@ }, { "url": "/observation", - "state": "ticket.card.observations", - "component": "vn-ticket-observations", + "state": "ticket.card.observation", + "component": "vn-ticket-observation", "params": { "ticket": "$ctrl.ticket" }, diff --git a/client/ticket/src/notes/ticket-observations.html b/client/ticket/src/note/ticket-observation.html similarity index 93% rename from client/ticket/src/notes/ticket-observations.html rename to client/ticket/src/note/ticket-observation.html index b48ec5a72..dc3b1c12c 100644 --- a/client/ticket/src/notes/ticket-observations.html +++ b/client/ticket/src/note/ticket-observation.html @@ -1,6 +1,6 @@ @@ -31,7 +31,8 @@ vn-two margin-large-right label="Description" - model="ticketObservation.description"> + model="ticketObservation.description" + rule="ticketObservation.description"> 0 || observationsObj.create.length > 0 || observationsObj.delete.length > 0; if (canSubmit) { - return this.$http.post(`/ticket/api/TicketObservations/crudTicketObservations`, observationsObj).then(() => { + return this.$http.post(`/ticket/api/TicketObservations/crudTicketObservation`, observationsObj).then(() => { this.getObservations(); this._unsetDirtyForm(); }); @@ -129,11 +129,11 @@ class TicketObservations { } } -TicketObservations.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp']; +Controller.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp']; -ngModule.component('vnTicketObservations', { - template: require('./ticket-observations.html'), - controller: TicketObservations, +ngModule.component('vnTicketObservation', { + template: require('./ticket-observation.html'), + controller: Controller, bindings: { ticket: '<' } diff --git a/client/ticket/src/notes/ticket-observations.spec.js b/client/ticket/src/note/ticket-observation.spec.js similarity index 84% rename from client/ticket/src/notes/ticket-observations.spec.js rename to client/ticket/src/note/ticket-observation.spec.js index 982cfb61b..ceed1d232 100644 --- a/client/ticket/src/notes/ticket-observations.spec.js +++ b/client/ticket/src/note/ticket-observation.spec.js @@ -1,7 +1,7 @@ -import './ticket-observations.js'; +import './ticket-observation.js'; describe('ticket', () => { - describe('Component vnTicketObservations', () => { + describe('Component vnTicketObservation', () => { let $componentController; let $state; let controller; @@ -15,7 +15,7 @@ describe('ticket', () => { $componentController = _$componentController_; $state = _$state_; $httpBackend = _$httpBackend_; - controller = $componentController('vnTicketObservations', {$state: $state}); + controller = $componentController('vnTicketObservation', {$state: $state}); })); describe('add / remove observation', () => { @@ -24,10 +24,10 @@ describe('ticket', () => { spyOn(controller, '_setIconAdd').and.callThrough(); controller.addObservation(); - expect(controller._setIconAdd).toHaveBeenCalledWith(); expect(controller.ticketObservations.length).toEqual(1); expect(controller.ticketObservations[0].id).toBe(undefined); expect(controller.ticketObservations[0].showAddIcon).toBeTruthy(); + expect(controller._setIconAdd).toHaveBeenCalledWith(); }); it('should remove an observation that occupies the position in the index given and call _setIconAdd()', () => { @@ -42,11 +42,11 @@ describe('ticket', () => { controller.removeObservation(index); - expect(controller._setIconAdd).toHaveBeenCalledWith(); expect(controller.ticketObservations.length).toEqual(2); expect(controller.ticketObservations[0].showAddIcon).toBeFalsy(); expect(controller.ticketObservations[1].showAddIcon).toBeTruthy(); expect(controller.ticketObservations[index]).toBe(undefined); + expect(controller._setIconAdd).toHaveBeenCalledWith(); }); }); @@ -70,16 +70,33 @@ describe('ticket', () => { describe('get Observations()', () => { it('should perform a GET query to receive the ticket observations', () => { - let res = [{id: 1, observationTypeFk: 1, description: 'one'}]; + let response = [{id: 1, observationTypeFk: 1, description: 'one'}]; + spyOn(controller, 'setOldObservations'); - $httpBackend.whenGET(`/ticket/api/TicketObservations?filter={"where":{},"include":["observationType"]}`).respond(res); + $httpBackend.whenGET(`/ticket/api/TicketObservations?filter={"where":{},"include":["observationType"]}`).respond(response); $httpBackend.expectGET(`/ticket/api/TicketObservations?filter={"where":{},"include":["observationType"]}`); controller.getObservations(); $httpBackend.flush(); + + expect(controller.setOldObservations).toHaveBeenCalledWith(jasmine.any(Object)); }); }); describe('submit()', () => { + it("should return an error message 'Some fields are invalid'", () => { + controller.$scope.form = {}; + controller.$scope.form.$invalid = true; + spyOn(controller.vnApp, 'showMessage').and.callThrough(); + controller.ticketObservations = [ + {id: 1, observationTypeFk: 1, description: 'one', itemFk: 1}, + {observationTypeFk: 1, description: 'one', itemFk: 1} + ]; + controller.oldObservations = {1: {id: 1, observationTypeFk: 1, description: 'one', itemFk: 1}}; + controller.submit(); + + expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Some fields are invalid'); + }); + it("should return an error message 'The observation type must be unique'", () => { controller.$scope.form = {}; spyOn(controller.vnApp, 'showMessage').and.callThrough(); @@ -100,7 +117,7 @@ describe('ticket', () => { controller.removedObservations = [1]; $httpBackend.whenGET(`/ticket/api/TicketObservations?filter={"where":{},"include":["observationType"]}`).respond([]); - $httpBackend.expectPOST(`/ticket/api/TicketObservations/crudTicketObservations`).respond('ok!'); + $httpBackend.expectPOST(`/ticket/api/TicketObservations/crudTicketObservation`).respond('ok!'); controller.submit(); $httpBackend.flush(); }); @@ -111,7 +128,7 @@ describe('ticket', () => { controller.oldObservations = {1: {id: 1, observationTypeFk: 1, description: 'one'}}; $httpBackend.whenGET(`/ticket/api/TicketObservations?filter={"where":{},"include":["observationType"]}`).respond([]); - $httpBackend.expectPOST(`/ticket/api/TicketObservations/crudTicketObservations`).respond('ok!'); + $httpBackend.expectPOST(`/ticket/api/TicketObservations/crudTicketObservation`).respond('ok!'); controller.submit(); $httpBackend.flush(); }); @@ -121,7 +138,7 @@ describe('ticket', () => { controller.ticketObservations = [{observationTypeFk: 2, description: 'two'}]; $httpBackend.whenGET(`/ticket/api/TicketObservations?filter={"where":{},"include":["observationType"]}`).respond([]); - $httpBackend.expectPOST(`/ticket/api/TicketObservations/crudTicketObservations`).respond('ok!'); + $httpBackend.expectPOST(`/ticket/api/TicketObservations/crudTicketObservation`).respond('ok!'); controller.submit(); $httpBackend.flush(); }); diff --git a/services/loopback/common/models/ticket.js b/services/loopback/common/models/ticket.js index 108ebc872..a9d930903 100644 --- a/services/loopback/common/models/ticket.js +++ b/services/loopback/common/models/ticket.js @@ -3,4 +3,5 @@ module.exports = function(Self) { require('../methods/ticket/change-time.js')(Self); require('../methods/ticket/change-worker.js')(Self); require('../methods/ticket/filter.js')(Self); + require('../methods/ticket/get-volume.js')(Self); }; diff --git a/services/ticket/common/methods/notes/crudTicketObservation.js b/services/ticket/common/methods/notes/crudTicketObservation.js new file mode 100644 index 000000000..04365eb12 --- /dev/null +++ b/services/ticket/common/methods/notes/crudTicketObservation.js @@ -0,0 +1,3 @@ +module.exports = Self => { + Self.installCrudModel('crudTicketObservation'); +}; diff --git a/services/ticket/common/methods/ticket/crudTicketObservations.js b/services/ticket/common/methods/ticket/crudTicketObservations.js deleted file mode 100644 index 33949e1f4..000000000 --- a/services/ticket/common/methods/ticket/crudTicketObservations.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = Self => { - Self.installCrudModel('crudTicketObservations'); -}; diff --git a/services/ticket/common/models/ticketObservation.js b/services/ticket/common/models/ticketObservation.js index bed837bd3..87e6f8748 100644 --- a/services/ticket/common/models/ticketObservation.js +++ b/services/ticket/common/models/ticketObservation.js @@ -1,3 +1,3 @@ module.exports = function(Self) { - require('../methods/ticket/crudTicketObservations.js')(Self); + require('../methods/notes/crudTicketObservation.js')(Self); };