import '../index.js'; import watcher from 'core/mocks/watcher'; describe('Ticket', () => { describe('Component vnTicketSale', () => { let controller; let $element; let $scope; let $httpBackend; let ticket = { id: 1, clientFk: 101, shipped: 1, created: new Date(), client: {salesPersonFk: 1}, address: {mobile: 111111111} }; let 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('item'); ngModule('ticket'); ngModule('client'); }); beforeEach(angular.mock.inject(($compile, $rootScope, $state, _$httpBackend_) => { $state.params.id = ticket.id; $scope = $rootScope.$new(); $scope.ticket = ticket; $scope.watcher = watcher; $httpBackend = _$httpBackend_; $httpBackend.whenGET(/api\/Tickets\/1\/getSales.*/).respond(sales); $httpBackend.whenGET(`/api/Tickets/1/getVAT`).respond(200, 10.5); $httpBackend.whenGET(`/api/Tickets/1/subtotal`).respond(200, 227.5); $httpBackend.whenGET(`/api/Tickets/1/isEditable`).respond(); $element = $compile('')($scope); controller = $element.controller('vnTicketSale'); controller.card = {reload: () => {}}; $httpBackend.flush(); })); afterEach(() => { $scope.$destroy(); $element.remove(); }); describe('createClaim()', () => { it('should perform a query and call windows open', () => { spyOn(controller.$state, 'go'); const claim = {id: 1}; const sales = [{id: 1}, {id: 2}]; $httpBackend.when('POST', `/api/Claims/createFromSales`, {claim, sales}).respond(claim); $httpBackend.expect('POST', `/api/Claims/createFromSales`).respond(claim); controller.createClaim(); $httpBackend.flush(); expect(controller.$state.go).toHaveBeenCalledWith('claim.card.basicData', {id: 1}); }); }); describe('total/VAT/subtotal properties', () => { it('should fill total, VAT and subTotal', () => { expect(controller.subtotal).toEqual(227.5); expect(controller.VAT).toEqual(10.5); expect(controller.total).toEqual(238); }); }); 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[1]; sale.checked = true; let expectedResult = [sale]; expect(controller.checkedLines()).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}]; spyOn(controller, 'onStateChange'); $httpBackend.whenGET(`/api/States?filter=${filter}`).respond(res); controller.onStateOkClick(); $httpBackend.flush(); expect(controller.onStateChange).toHaveBeenCalledWith(3); }); }); describe('onStateChange()', () => { it('should perform a post and then call a function', () => { $httpBackend.expectPOST(`/api/TicketTrackings/changeState`).respond(); controller.onStateChange(3); $httpBackend.flush(); }); }); describe('onRemoveLinesClick()', () => { it('should call getCheckedLines, call removeInstances, and make a query', () => { controller.sales[1].checked = true; $httpBackend.whenPOST(`/api/Sales/removes`).respond(); controller.onRemoveLinesClick('ACCEPT'); $httpBackend.flush(); expect(controller.sales.length).toEqual(1); }); }); describe('unmarkAsReserved()', () => { it('should call setReserved with false', () => { spyOn(controller, 'setReserved'); controller.unmarkAsReserved(false); expect(controller.setReserved).toHaveBeenCalledWith(false); }); }); describe('markAsReserved()', () => { it('should call setReserved with true', () => { 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[1]; sale.checked = true; let expectedRequest = { sales: [sale], ticketFk: ticket.id, reserved: false }; $httpBackend.expectPOST(`/api/Sales/reserve`, expectedRequest).respond(); controller.unmarkAsReserved(false); $httpBackend.flush(); }); }); describe('showSMSDialog()', () => { it('should open an SMS dialog with specified data', () => { spyOn(controller.$scope.sms, 'open'); controller.sales[1].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', () => { spyOn(controller.$scope.watcher, 'updateOriginalData'); const data = {quantity: 10}; const sale = sales[0]; sale.quantity = 10; $httpBackend.when('POST', `/api/Sales/1/updateQuantity`, data).respond(); $httpBackend.expect('POST', `/api/Sales/1/updateQuantity`, data).respond(); controller.updateQuantity(sale); $httpBackend.flush(); expect(controller.$scope.watcher.updateOriginalData).toHaveBeenCalledWith(); }); }); describe('updateConcept()', () => { it('should make a POST query saving sale concept', () => { spyOn(controller.$scope.watcher, 'updateOriginalData'); const data = {newConcept: 'My new weapon'}; const sale = sales[0]; sale.concept = 'My new weapon'; $httpBackend.when('POST', `/api/Sales/1/updateConcept`, data).respond(); $httpBackend.expect('POST', `/api/Sales/1/updateConcept`, data).respond(); controller.updateConcept(sale); $httpBackend.flush(); expect(controller.$scope.watcher.updateOriginalData).toHaveBeenCalledWith(); }); }); describe('addSale()', () => { it('should make a POST query adding a new sale', () => { 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', `/api/tickets/1/addSale`, params).respond(expectedResult); $httpBackend.expect('POST', `/api/tickets/1/addSale`, params).respond(expectedResult); controller.addSale(newSale); $httpBackend.flush(); expect(controller.$scope.watcher.updateOriginalData).toHaveBeenCalledWith(); }); }); describe('transferSales()', () => { it('should transfer sales to a ticket', () => { 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', `/api/tickets/1/transferSales`, params).respond(expectedResponse); $httpBackend.expect('POST', `/api/tickets/1/transferSales`, params).respond(expectedResponse); 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[1]; sale.checked = true; const expectedResponse = [sale]; $httpBackend.when('GET', `/api/clients/101/lastActiveTickets?ticketId=1`).respond(expectedResponse); $httpBackend.expect('GET', `/api/clients/101/lastActiveTickets?ticketId=1`).respond(expectedResponse); controller.setTransferParams(); $httpBackend.flush(); const lastActiveTickets = controller.transfer.lastActiveTickets; expect(controller.transfer).toBeDefined(); expect(lastActiveTickets).toBeDefined(); expect(lastActiveTickets[0].id).toEqual(4); }); }); }); });