From 83d2dd2d808aee9113153b2a07e7be8adf21d6de Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 17 Jan 2022 13:17:06 +0100 Subject: [PATCH] refacto(ticket_volume): tests and front js --- .../ticket/back/methods/ticket/getVolume.js | 8 ++-- .../methods/ticket/specs/getVolume.spec.js | 13 +++-- modules/ticket/front/volume/index.js | 19 ++++---- modules/ticket/front/volume/index.spec.js | 47 +++++++------------ 4 files changed, 40 insertions(+), 47 deletions(-) diff --git a/modules/ticket/back/methods/ticket/getVolume.js b/modules/ticket/back/methods/ticket/getVolume.js index 57493a5e3..6029020ba 100644 --- a/modules/ticket/back/methods/ticket/getVolume.js +++ b/modules/ticket/back/methods/ticket/getVolume.js @@ -11,11 +11,11 @@ module.exports = Self => { }], returns: [{ arg: 'saleVolume', - type: ['Object'] + type: ['object'] }, { arg: 'packingTypeVolume', - type: ['Object'] + type: ['object'] }], http: { path: `/:id/getVolume`, @@ -43,7 +43,7 @@ module.exports = Self => { ON i.code = s.itemPackingTypeFk WHERE s.ticketFk = ? GROUP BY s.itemPackingTypeFk`, [ticketFk], myOptions); - - return [saleVolume, packingTypeVolume] + + return [saleVolume, packingTypeVolume]; }; }; diff --git a/modules/ticket/back/methods/ticket/specs/getVolume.spec.js b/modules/ticket/back/methods/ticket/specs/getVolume.spec.js index af171c8eb..cba3948db 100644 --- a/modules/ticket/back/methods/ticket/specs/getVolume.spec.js +++ b/modules/ticket/back/methods/ticket/specs/getVolume.spec.js @@ -8,12 +8,15 @@ describe('ticket getVolume()', () => { const options = {transaction: tx}; const ticketId = 1; - const result = await models.Ticket.getVolume(ticketId, options); - const saleVolume = result[0]; - const packingTypeVolume = result[1]; + const expectedSaleVolume = 1.09; + const expectedPackingTypeVolume = 0.028; - expect(saleVolume).toBeDefined; - expect(packingTypeVolume).toBeDefined; + const result = await models.Ticket.getVolume(ticketId, options); + const [saleVolume] = result[0]; + const [packingTypeVolume] = result[1]; + + expect(saleVolume.volume).toEqual(expectedSaleVolume); + expect(packingTypeVolume.volume).toEqual(expectedPackingTypeVolume); await tx.rollback(); } catch (e) { diff --git a/modules/ticket/front/volume/index.js b/modules/ticket/front/volume/index.js index 33fe66bdb..81491bca0 100644 --- a/modules/ticket/front/volume/index.js +++ b/modules/ticket/front/volume/index.js @@ -24,15 +24,18 @@ class Controller extends Section { } applyVolumes() { - const ticket = this.sales[0].ticketFk + const ticket = this.sales[0].ticketFk; this.$http.get(`Tickets/${ticket}/getVolume`).then(res => { - this.sales.forEach(sale => { - res.data.saleVolume.forEach(volume => { - if (sale.id === volume.saleFk) - sale.saleVolume = volume; - }); - }); - this.packingTypeVolume = res.data.packingTypeVolume + const saleVolume = res.data.saleVolume; + + const volumes = new Map(); + for (const volume of saleVolume) + volumes.set(volume.saleFk, volume); + + for (const sale of this.sales) + sale.saleVolume = volumes.get(sale.id); + + this.packingTypeVolume = res.data.packingTypeVolume; }); } } diff --git a/modules/ticket/front/volume/index.spec.js b/modules/ticket/front/volume/index.spec.js index 578c6109c..6ab968503 100644 --- a/modules/ticket/front/volume/index.spec.js +++ b/modules/ticket/front/volume/index.spec.js @@ -35,18 +35,18 @@ describe('ticket', () => { describe('applyVolumes()', () => { const ticket = 1; - const respondGet = + const response = { saleVolume: [ - {saleFk: 1, volume: 0.012}, + {saleFk: 1, volume: 0.012}, {saleFk: 2, volume: 0.015} ], packingTypeVolume: [ - {code: 'V', volume: 1}, - {code: 'H',volume: 2}, + {code: 'V', volume: 1}, + {code: 'H', volume: 2} ] }; - + it(`should not apply volumes to the sales if sales property is not defined on controller`, () => { controller.sales = [{id: 1, name: 'Sale one'}, {id: 2, name: 'Sale two'}]; @@ -61,45 +61,32 @@ describe('ticket', () => { }); it(`should apply volumes to the sales if sales and volumes properties are defined on controller`, () => { - $httpBackend.expectGET(`Tickets/${ticket}/getVolume`).respond(respondGet); + const expectedResultOne = response.saleVolume[0].volume; + const expectedResultTwo = response.saleVolume[1].volume; + $httpBackend.expectGET(`Tickets/${ticket}/getVolume`).respond(response); controller.sales = [ - {id: 1, name: 'Sale one', ticketFk: ticket}, + {id: 1, name: 'Sale one', ticketFk: ticket}, {id: 2, name: 'Sale two'} ]; $httpBackend.flush(); - expect(controller.sales[0].saleVolume.volume).toEqual(0.012); - expect(controller.sales[1].saleVolume.volume).toEqual(0.015); + expect(controller.sales[0].saleVolume.volume).toEqual(expectedResultOne); + expect(controller.sales[1].saleVolume.volume).toEqual(expectedResultTwo); }); it(`should apply packing volumes to the sales if sales and volumes properties are defined on controller`, () => { - $httpBackend.expectGET(`Tickets/${ticket}/getVolume`).respond(respondGet); + const expectedResultOne = response.packingTypeVolume[0].code; + const expectedResultTwo = response.packingTypeVolume[1].code; + $httpBackend.expectGET(`Tickets/${ticket}/getVolume`).respond(response); controller.sales = [ - {id: 1, name: 'Sale one', ticketFk: ticket}, + {id: 1, name: 'Sale one', ticketFk: ticket}, {id: 2, name: 'Sale two'} ]; $httpBackend.flush(); - expect(controller.packingTypeVolume[0]).toBeDefined; - expect(controller.packingTypeVolume[1]).toBeDefined + expect(controller.packingTypeVolume[0].code).toEqual(expectedResultOne); + expect(controller.packingTypeVolume[1].code).toEqual(expectedResultTwo); }); }); - - /* - it('should join the sale volumes to its respective sale', () => { - controller.ticket = {id: 1}; - let response = {volumes: [ - {saleFk: 1, m3: 0.008}, - {saleFk: 2, m3: 0.003} - ]}; - - $httpBackend.expectGET(`tickets/1/getVolume`).respond(response); - controller.onDataChange(); - $httpBackend.flush(); - - expect($scope.model.data[0].volume.m3).toBe(0.008); - expect($scope.model.data[1].volume.m3).toBe(0.003); - }); - */ }); });