import '../index.js'; import watcher from 'core/mocks/watcher'; import crudModel from 'core/mocks/crud-model'; describe('Ticket', () => { describe('Component vnTicketSale', () => { let controller; let $scope; let $state; let $httpBackend; const ticket = { id: 1, clientFk: 101, shipped: 1, created: new Date(), client: {salesPersonFk: 1}, address: {mobile: 111111111} }; const sales = [ { id: 1, concept: 'Item 1', quantity: 5, price: 23.5, discount: 0, }, { id: 4, concept: 'Item 2', quantity: 20, price: 5.5, discount: 0, } ]; beforeEach(ngModule('ticket')); beforeEach(angular.mock.inject(($componentController, $rootScope, _$state_, _$httpBackend_) => { $state = _$state_; $scope = $rootScope.$new(); $scope.watcher = watcher; $scope.sms = {open: () => {}}; $scope.ticket = ticket; $scope.model = crudModel; $httpBackend = _$httpBackend_; Object.defineProperties($state.params, { id: { value: ticket.id, writable: true }, go: { value: () => {}, writable: false } }); controller = $componentController('vnTicketSale', {$scope, $state}); controller.card = {reload: () => {}}; controller.ticket = ticket; controller.sales = sales; })); describe('createClaim()', () => { it('should perform a query and call windows open', () => { jest.spyOn(controller.$state, 'go'); const claim = {id: 1}; const sales = [{id: 1}, {id: 2}]; $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); $httpBackend.when('POST', `Claims/createFromSales`, {claim: claim, sales: sales}).respond(claim); $httpBackend.expect('POST', `Claims/createFromSales`).respond(claim); controller.createClaim(); $httpBackend.flush(); expect(controller.subtotal).toEqual(227.5); expect(controller.VAT).toEqual(10.5); expect(controller.total).toEqual(238); expect(controller.$state.go).toHaveBeenCalledWith('claim.card.basicData', {id: 1}); }); }); describe('isChecked() getter', () => { it('should set isChecked value to true when one of the instances has the value checked to true', () => { controller.sales[0].checked = true; expect(controller.isChecked).toBeTruthy(); }); }); describe('checkedLines()', () => { it('should make an array of the instances with the property checked true()', () => { let sale = controller.sales[0]; sale.checked = true; let expectedResult = [sale]; $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); let result = controller.checkedLines(); $httpBackend.flush(); expect(result).toEqual(expectedResult); }); }); describe('onStateOkClick()', () => { it('should perform a get and then call a function', () => { let filter = {where: {code: 'OK'}, fields: ['id']}; filter = encodeURIComponent(JSON.stringify(filter)); let res = [{id: 3}]; jest.spyOn(controller, 'onStateChange').mockReturnThis(); $httpBackend.whenGET(`States?filter=${filter}`).respond(res); $httpBackend.expectGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.expectGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); controller.onStateOkClick(); $httpBackend.flush(); expect(controller.onStateChange).toHaveBeenCalledWith(3); }); }); describe('onStateChange()', () => { it('should perform a post and then call a function', () => { $httpBackend.expectPOST(`TicketTrackings/changeState`).respond(); $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); controller.onStateChange(3); $httpBackend.flush(); }); }); describe('onRemoveLinesClick()', () => { it('should call getCheckedLines, call removeInstances, and make a query', () => { controller.sales[0].checked = true; $httpBackend.whenPOST(`Sales/removes`).respond(); $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); controller.onRemoveLinesClick('accept'); $httpBackend.flush(); expect(controller.sales.length).toEqual(1); }); }); 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 sale = controller.sales[0]; sale.checked = true; let expectedRequest = { sales: [sale], ticketFk: ticket.id, reserved: false }; $httpBackend.expectPOST(`Sales/reserve`, expectedRequest).respond(); $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); controller.unmarkAsReserved(false); $httpBackend.flush(); }); }); describe('showSMSDialog()', () => { it('should open an SMS dialog with specified data', () => { jest.spyOn(controller.$scope.sms, 'open'); controller.sales[0].checked = true; controller.showSMSDialog(); expect(controller.$scope.sms.open).toHaveBeenCalledWith(); expect(controller.newSMS.destination).toEqual(111111111); expect(controller.newSMS.message).not.toEqual(''); }); }); describe('updateQuantity()', () => { it('should make a POST query saving sale quantity', () => { jest.spyOn(controller.$scope.watcher, 'updateOriginalData'); const data = {quantity: 10}; const sale = sales[0]; sale.quantity = 10; $httpBackend.when('POST', `Sales/4/updateQuantity`, data).respond(); $httpBackend.expect('POST', `Sales/4/updateQuantity`, data).respond(); $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); controller.updateQuantity(sale); $httpBackend.flush(); expect(controller.$scope.watcher.updateOriginalData).toHaveBeenCalledWith(); }); }); describe('updateConcept()', () => { it('should make a POST query saving sale concept', () => { jest.spyOn(controller.$scope.watcher, 'updateOriginalData'); const data = {newConcept: 'My new weapon'}; const sale = sales[0]; sale.concept = 'My new weapon'; $httpBackend.when('POST', `Sales/4/updateConcept`, data).respond(); $httpBackend.expect('POST', `Sales/4/updateConcept`, data).respond(); $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); controller.updateConcept(sale); $httpBackend.flush(); expect(controller.$scope.watcher.updateOriginalData).toHaveBeenCalledWith(); }); }); describe('addSale()', () => { it('should make a POST query adding a new sale', () => { jest.spyOn(controller.$scope.watcher, 'updateOriginalData'); const newSale = {itemFk: 4, quantity: 10}; const params = {itemId: 4, quantity: 10}; const expectedResult = { id: 30, quantity: 10, discount: 0, price: 0, itemFk: 4, item: { subName: 'Item subName', image: '30.png' } }; $httpBackend.when('POST', `tickets/1/addSale`, params).respond(expectedResult); $httpBackend.expect('POST', `tickets/1/addSale`, params).respond(expectedResult); $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); controller.addSale(newSale); $httpBackend.flush(); expect(controller.$scope.watcher.updateOriginalData).toHaveBeenCalledWith(); }); }); describe('transferSales()', () => { it('should transfer sales to a ticket', () => { jest.spyOn(controller, 'goToTicket'); controller.transfer = { sales: [{id: 1, itemFk: 1}, {id: 2, itemFk: 4}] }; const expectedResponse = {id: 13}; const params = { ticketId: 13, sales: controller.transfer.sales }; $httpBackend.when('POST', `tickets/1/transferSales`, params).respond(expectedResponse); $httpBackend.expect('POST', `tickets/1/transferSales`, params).respond(expectedResponse); $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); controller.transferSales(13); $httpBackend.flush(); expect(controller.goToTicket).toHaveBeenCalledWith(13); }); }); describe('setTransferParams()', () => { it('should define the transfer object on the controller and its default properties', () => { let sale = controller.sales[0]; sale.checked = true; const expectedResponse = [sale]; $httpBackend.when('GET', `clients/101/lastActiveTickets?ticketId=1`).respond(expectedResponse); $httpBackend.expect('GET', `clients/101/lastActiveTickets?ticketId=1`).respond(expectedResponse); $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); controller.setTransferParams(); $httpBackend.flush(); const lastActiveTickets = controller.transfer.lastActiveTickets; expect(controller.transfer).toBeDefined(); expect(lastActiveTickets).toBeDefined(); expect(lastActiveTickets[0].id).toEqual(4); }); }); describe('newOrderFromTicket()', () => { it('should make an HTTP post query and then open the new order on a new tab', () => { const params = {ticketFk: 1}; const expectedResponse = {id: 123}; window.open = jasmine.createSpy('open'); controller.$state.href = jasmine.createSpy('href') .and.returnValue('/somePath'); $httpBackend.when('POST', `Orders/newFromTicket`, params).respond(expectedResponse); $httpBackend.expect('POST', `Orders/newFromTicket`, params).respond(expectedResponse); $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); controller.newOrderFromTicket(); $httpBackend.flush(); expect(window.open).toHaveBeenCalledWith('/somePath', '_blank'); }); }); describe('hasOneSaleSelected()', () => { it('should return true if just one sale is selected', () => { controller.sales[0].checked = true; expect(controller.hasOneSaleSelected()).toBeTruthy(); }); }); describe('calculateSalePrice()', () => { it('should make an HTTP post query ', () => { controller.sales[0].checked = true; $httpBackend.when('POST', `Sales/4/recalculatePrice`).respond(200); $httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`Tickets/1/isEditable`).respond(); $httpBackend.whenGET(`Tickets/1/isLocked`).respond(); controller.calculateSalePrice(); $httpBackend.flush(); }); }); }); });