From 25b5cfe99f255a60eb9e4f7f9a6c5a82bf76b461 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Tue, 14 Aug 2018 14:32:27 +0200 Subject: [PATCH] #549 threeLastActive.js Backend unit tests --- .../methods/ticket/specs/getTaxes.spec.js | 9 ++++----- .../methods/ticket/specs/getTotal.spec.js | 18 ++++++++---------- .../ticket/specs/threeLastActive.spec.js | 18 ++++++++++++++++++ .../common/methods/ticket/threeLastActive.js | 6 +++--- 4 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 services/loopback/common/methods/ticket/specs/threeLastActive.spec.js diff --git a/services/loopback/common/methods/ticket/specs/getTaxes.spec.js b/services/loopback/common/methods/ticket/specs/getTaxes.spec.js index a4d9214f9d..5bb6ecf46d 100644 --- a/services/loopback/common/methods/ticket/specs/getTaxes.spec.js +++ b/services/loopback/common/methods/ticket/specs/getTaxes.spec.js @@ -1,10 +1,9 @@ const app = require(`${servicesDir}/ticket/server/server`); describe('ticket getTaxes()', () => { - it('should call the getTaxes method', async() => { - await app.models.Ticket.getTaxes(1) - .then(response => { - expect(response[0].tax).toEqual(20.95); - }); + it('should return the tax of a given ticket', async() => { + let result = await app.models.Ticket.getTaxes(1); + + expect(result[0].tax).toEqual(20.95); }); }); diff --git a/services/loopback/common/methods/ticket/specs/getTotal.spec.js b/services/loopback/common/methods/ticket/specs/getTotal.spec.js index 50aa5abd83..062256df11 100644 --- a/services/loopback/common/methods/ticket/specs/getTotal.spec.js +++ b/services/loopback/common/methods/ticket/specs/getTotal.spec.js @@ -1,17 +1,15 @@ const app = require(`${servicesDir}/ticket/server/server`); describe('ticket getTotal()', () => { - it('should call the getTotal method and return the response', async() => { - await app.models.Ticket.getTotal(1) - .then(response => { - expect(response).toEqual(448.25); - }); + it('should return the total of a ticket', async() => { + let result = await app.models.Ticket.getTotal(1); + + expect(result).toEqual(448.25); }); - it(`should call the getTotal method and return zero if doesn't have lines`, async() => { - await app.models.Ticket.getTotal(13) - .then(response => { - expect(response).toEqual(0); - }); + it(`should return zero if the ticket doesn't have lines`, async() => { + let result = await app.models.Ticket.getTotal(13); + + expect(result).toEqual(0); }); }); diff --git a/services/loopback/common/methods/ticket/specs/threeLastActive.spec.js b/services/loopback/common/methods/ticket/specs/threeLastActive.spec.js new file mode 100644 index 0000000000..008e24bea0 --- /dev/null +++ b/services/loopback/common/methods/ticket/specs/threeLastActive.spec.js @@ -0,0 +1,18 @@ +const app = require(`${servicesDir}/ticket/server/server`); + +describe('ticket threeLastActive()', () => { + it('should return the last three active tickets', async() => { + let expectedResult = [ + {id: 12, shipped: '2018-09-14 00:00:00', agencyName: 'inhouse pickup', warehouseName: 'Warehouse One'}, + {id: 13, shipped: '2018-10-14 00:00:00', agencyName: 'Super-Man delivery', warehouseName: 'Warehouse Two'}, + {id: 14, shipped: '2018-11-14 00:00:00', agencyName: 'Super-Man delivery', warehouseName: 'Warehouse Two'} + ]; + expectedResult = JSON.stringify(expectedResult); + + let params = {clientFk: 101, ticketFk: 1}; + let result = await app.models.Ticket.threeLastActive(params); + result = JSON.stringify(result); + + expect(result).toEqual(expectedResult); + }); +}); diff --git a/services/loopback/common/methods/ticket/threeLastActive.js b/services/loopback/common/methods/ticket/threeLastActive.js index d57f5824a6..3539efb4f6 100644 --- a/services/loopback/common/methods/ticket/threeLastActive.js +++ b/services/loopback/common/methods/ticket/threeLastActive.js @@ -18,7 +18,7 @@ module.exports = Self => { } }); - Self.threeLastActive = async filter => { + Self.threeLastActive = async params => { let query = ` SELECT t.id,t.shipped,a.name AS agencyName,w.name AS warehouseName FROM vn.ticket t @@ -28,7 +28,7 @@ module.exports = Self => { WHERE t.shipped > CURDATE() AND t.clientFk = ? AND ts.alertLevel = 0 AND t.id <> ? ORDER BY t.shipped LIMIT 3`; - let result = await Self.rawSql(query, [filter.clientFk, filter.ticketFk]); - return result; + let tickets = await Self.rawSql(query, [params.clientFk, params.ticketFk]); + return tickets; }; };