From 78ee6f1aff229280b3aad7b76569744fa61b9403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Wed, 1 Jul 2020 12:53:56 +0200 Subject: [PATCH] Updated unit tests --- gulpfile.js | 4 - modules/ticket/front/sale/index.html | 2 +- modules/ticket/front/sale/index.js | 6 +- .../front/sale/{specs => }/index.spec.js | 244 +++++++++++++++++- .../front/sale/specs/editDiscount.spec.js | 87 ------- 5 files changed, 247 insertions(+), 96 deletions(-) rename modules/ticket/front/sale/{specs => }/index.spec.js (67%) delete mode 100644 modules/ticket/front/sale/specs/editDiscount.spec.js diff --git a/gulpfile.js b/gulpfile.js index b946b48de..096c44584 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -83,8 +83,6 @@ async function backTestOnce(done) { port: container.dbConf.port }); - log('[Debug] dataSources', dataSources.vn); - let bootOptions = {dataSources}; let app = require(`./loopback/server/server`); @@ -92,8 +90,6 @@ async function backTestOnce(done) { try { app.boot(bootOptions); - log('[Debug] back started'); - await new Promise((resolve, reject) => { const jasmine = require('gulp-jasmine'); diff --git a/modules/ticket/front/sale/index.html b/modules/ticket/front/sale/index.html index c50e39ea5..a907cfbf7 100644 --- a/modules/ticket/front/sale/index.html +++ b/modules/ticket/front/sale/index.html @@ -141,7 +141,7 @@ + on-change="$ctrl.updateConcept(sale)"> diff --git a/modules/ticket/front/sale/index.js b/modules/ticket/front/sale/index.js index 30414f144..21456a74e 100644 --- a/modules/ticket/front/sale/index.js +++ b/modules/ticket/front/sale/index.js @@ -426,7 +426,7 @@ class Controller extends Section { /* * Changes a sale concept */ - changeConcept(sale) { + updateConcept(sale) { const data = {newConcept: sale.concept}; this.$http.post(`Sales/${sale.id}/updateConcept`, data).then(() => { this.vnApp.showSuccess(this.$t('Data saved!')); @@ -463,12 +463,12 @@ class Controller extends Section { } isTicketEditable() { - this.$http.get(`Tickets/${this.$state.params.id}/isEditable`) + this.$http.get(`Tickets/${this.$params.id}/isEditable`) .then(res => this.isEditable = res.data); } isTicketLocked() { - this.$http.get(`Tickets/${this.$state.params.id}/isLocked`) + this.$http.get(`Tickets/${this.$params.id}/isLocked`) .then(res => this.isLocked = res.data); } diff --git a/modules/ticket/front/sale/specs/index.spec.js b/modules/ticket/front/sale/index.spec.js similarity index 67% rename from modules/ticket/front/sale/specs/index.spec.js rename to modules/ticket/front/sale/index.spec.js index 7848a36b9..8fbda6e70 100644 --- a/modules/ticket/front/sale/specs/index.spec.js +++ b/modules/ticket/front/sale/index.spec.js @@ -1,4 +1,4 @@ -import '../index.js'; +import './index.js'; import watcher from 'core/mocks/watcher'; import crudModel from 'core/mocks/crud-model'; @@ -476,5 +476,247 @@ describe('Ticket', () => { expect(controller.resetChanges).toHaveBeenCalledWith(); }); }); + + describe('getNewPrice()', () => { + it('should return the total price simulation from a price change', () => { + const selectedSale = controller.sales[0]; + selectedSale.checked = true; + controller.edit = { + price: 2, + sale: selectedSale + }; + + const expectedAmount = 10; + const result = controller.getNewPrice(); + + expect(result).toEqual(expectedAmount); + }); + + it('should return the total price simulation from a discount change', () => { + const selectedSale = controller.sales[0]; + selectedSale.checked = true; + controller.edit = { + discount: 10, + sale: selectedSale + }; + + const expectedAmount = 105.75; + const result = controller.getNewPrice(); + + expect(result).toEqual(expectedAmount); + }); + }); + + describe('hasReserves()', () => { + it('should return true for any sale marked as reserved', () => { + const selectedSale = controller.sales[0]; + selectedSale.reserved = true; + + const result = controller.hasReserves(); + + expect(result).toBeTruthy(); + }); + }); + + describe('unmarkAsReserved()', () => { + it('should call setReserved with false', () => { + jest.spyOn(controller, 'setReserved'); + + controller.unmarkAsReserved(false); + + expect(controller.setReserved).toHaveBeenCalledWith(false); + }); + }); + + describe('markAsReserved()', () => { + it('should call setReserved with true', () => { + jest.spyOn(controller, 'setReserved'); + + controller.markAsReserved(true); + + expect(controller.setReserved).toHaveBeenCalledWith(true); + }); + }); + + describe('setReserved()', () => { + it('should call getCheckedLines, $.index.accept and make a query ', () => { + const selectedSale = controller.sales[0]; + selectedSale.checked = true; + const expectedParams = { + sales: [selectedSale], + ticketFk: 1, + reserved: false + }; + + $httpBackend.expectPOST(`Sales/reserve`, expectedParams).respond(); + controller.unmarkAsReserved(false); + $httpBackend.flush(); + }); + }); + + describe('newOrderFromTicket()', () => { + it('should make an HTTP post query and then open the new order on a new tab', () => { + const expectedParams = {ticketFk: 1}; + const expectedResponse = {id: 123}; + + window.open = jasmine.createSpy('open'); + controller.$state.href = jasmine.createSpy('href') + .and.returnValue('/somePath'); + + $httpBackend.expect('POST', `Orders/newFromTicket`, expectedParams).respond(expectedResponse); + controller.newOrderFromTicket(); + $httpBackend.flush(); + + expect(window.open).toHaveBeenCalledWith('/somePath', '_blank'); + }); + }); + + describe('showSMSDialog()', () => { + it('should open an SMS dialog with specified data', () => { + jest.spyOn(controller.$.sms, 'open'); + + const selectedSale = controller.sales[0]; + selectedSale.checked = true; + controller.showSMSDialog(); + + expect(controller.$.sms.open).toHaveBeenCalledWith(); + expect(controller.newSMS.destination).toEqual(111111111); + expect(controller.newSMS.message).not.toEqual(''); + }); + }); + + describe('changeQuantity()', () => { + it('should not call addSale() or updateQuantity() methods', () => { + jest.spyOn(controller, 'addSale'); + jest.spyOn(controller, 'updateQuantity'); + + const sale = {itemFk: 4}; + controller.changeQuantity(sale); + + expect(controller.addSale).not.toHaveBeenCalled(); + expect(controller.updateQuantity).not.toHaveBeenCalled(); + }); + + it('should call addSale() method', () => { + jest.spyOn(controller, 'addSale'); + + const sale = {itemFk: 4, quantity: 5}; + controller.changeQuantity(sale); + + expect(controller.addSale).toHaveBeenCalledWith(sale); + }); + + it('should call updateQuantity() method', () => { + jest.spyOn(controller, 'addSale'); + jest.spyOn(controller, 'updateQuantity'); + + const sale = {id: 1, itemFk: 4, quantity: 5}; + controller.changeQuantity(sale); + + expect(controller.addSale).not.toHaveBeenCalled(); + expect(controller.updateQuantity).toHaveBeenCalledWith(sale); + }); + }); + + describe('updateQuantity()', () => { + it('should make a POST query saving sale quantity', () => { + jest.spyOn(controller, 'resetChanges').mockReturnThis(); + + const selectedSale = controller.sales[0]; + selectedSale.checked = true; + selectedSale.quantity = 10; + + const expectedParams = {quantity: 10}; + $httpBackend.expect('POST', `Sales/1/updateQuantity`, expectedParams).respond(); + controller.updateQuantity(selectedSale); + $httpBackend.flush(); + + expect(controller.resetChanges).toHaveBeenCalledWith(); + }); + }); + + describe('updateConcept()', () => { + it('should make a POST query saving sale concept', () => { + jest.spyOn(controller, 'resetChanges').mockReturnThis(); + + const selectedSale = controller.sales[0]; + selectedSale.checked = true; + selectedSale.concept = 'My new weapon'; + + const expectedParams = {newConcept: 'My new weapon'}; + $httpBackend.expect('POST', `Sales/1/updateConcept`, expectedParams).respond(); + controller.updateConcept(selectedSale); + $httpBackend.flush(); + + expect(controller.resetChanges).toHaveBeenCalledWith(); + }); + }); + + describe('addSale()', () => { + it('should make a POST query adding a new sale', () => { + jest.spyOn(controller.vnApp, 'showSuccess').mockReturnThis(); + jest.spyOn(controller, 'resetChanges').mockReturnThis(); + + const newSale = {itemFk: 4, quantity: 10}; + const expectedParams = {itemId: 4, quantity: 10}; + const expectedResult = { + id: 30, + quantity: 10, + discount: 0, + price: 0, + itemFk: 4, + item: { + subName: 'Item subName', + image: '30.png' + } + }; + + $httpBackend.expect('POST', `tickets/1/addSale`, expectedParams).respond(expectedResult); + controller.addSale(newSale); + $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); + expect(controller.resetChanges).toHaveBeenCalledWith(); + }); + }); + + describe('isTicketEditable()', () => { + it('should make a HTTP GET query and set the isEditable property on the controller', () => { + $httpBackend.expect('GET', `Tickets/1/isEditable`).respond(200, true); + controller.isTicketEditable(); + $httpBackend.flush(); + + expect(controller.isEditable).toBeDefined(); + expect(controller.isEditable).toBeTruthy(); + }); + }); + + describe('isTicketLocked()', () => { + it('should make a HTTP GET query and set the isLocked property on the controller', () => { + $httpBackend.expect('GET', `Tickets/1/isLocked`).respond(200, false); + controller.isTicketLocked(); + $httpBackend.flush(); + + expect(controller.isLocked).toBeDefined(); + expect(controller.isLocked).toBeFalsy(); + }); + }); + + describe('calculateSalePrice()', () => { + it('should make an HTTP post query ', () => { + jest.spyOn(controller.vnApp, 'showSuccess').mockReturnThis(); + jest.spyOn(controller.$.model, 'refresh').mockReturnThis(); + + const selectedSale = controller.sales[0]; + selectedSale.checked = true; + + $httpBackend.expect('POST', `Sales/1/recalculatePrice`).respond(200); + controller.calculateSalePrice(); + $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); + expect(controller.$.model.refresh).toHaveBeenCalledWith(); + }); + }); }); }); diff --git a/modules/ticket/front/sale/specs/editDiscount.spec.js b/modules/ticket/front/sale/specs/editDiscount.spec.js deleted file mode 100644 index 8eccfe74f..000000000 --- a/modules/ticket/front/sale/specs/editDiscount.spec.js +++ /dev/null @@ -1,87 +0,0 @@ -import '../editDiscount.js'; - -describe('Ticket', () => { - describe('Component vnTicketSaleEditDiscount', () => { - let controller; - let $httpBackend; - let $state; - let $scope; - - beforeEach(ngModule('ticket')); - - beforeEach(angular.mock.inject(($componentController, _$state_, _$httpBackend_, $rootScope) => { - $httpBackend = _$httpBackend_; - $scope = $rootScope.$new(); - $scope.index = {model: {instances: [{id: 1}, {id: 2}]}, accept: () => { - return { - then: () => {} - }; - }}; - $state = _$state_; - $state.params.id = 11; - const $element = angular.element(''); - controller = $componentController('vnTicketSaleEditDiscount', {$element, $scope}); - controller._edit = [{id: 3}]; - controller.onHide = () => {}; - })); - - describe('edit() setter', () => { - it('should set _edit value and call setNewDiscount', () => { - jest.spyOn(controller, 'setNewDiscount'); - controller.edit = {id: 1}; - - expect(controller.edit).toEqual({id: 1}); - expect(controller.setNewDiscount).toHaveBeenCalledWith(); - }); - }); - - describe('bulk() setter', () => { - it('should set _bulk value and call setNewDiscount', () => { - jest.spyOn(controller, 'setNewDiscount'); - controller.bulk = true; - - expect(controller.bulk).toEqual(true); - expect(controller.setNewDiscount).toHaveBeenCalledWith(); - }); - }); - - describe('setNewDiscount()', () => { - it('should set NewDiscount to edit[0].discount value if it doesnt exists', () => { - controller.edit = [{discount: 1}]; - controller.setNewDiscount(); - - expect(controller.newDiscount).toEqual(1); - }); - }); - - describe('updateDiscount()', () => { - it('should make a query if the discount value has been modified or the bulk value is true', () => { - controller.bulk = true; - controller.newDiscount = 15; - - $httpBackend.expectPOST(`Tickets/11/updateDiscount`).respond(); - controller.updateDiscount(); - - $httpBackend.flush(); - }); - - it(`should throw if there's no changes on discount and it isn't bulk`, () => { - controller.bulk = false; - controller.newDiscount = 15; - controller.edit = [{discount: 15}]; - jest.spyOn(controller.vnApp, 'showError'); - controller.updateDiscount(); - - expect(controller.vnApp.showError).toHaveBeenCalledWith('There are no changes to save'); - }); - }); - - describe('clearDiscount()', () => { - it('should set newDiscount to null', () => { - controller.clearDiscount(); - - expect(controller.newDiscount).toEqual(null); - }); - }); - }); -});