import '../index.js'; describe('Ticket', () => { describe('Component vnTicketSale', () => { let controller; let $element; let $scope; let $httpBackend; let ticket = { id: 1, clientFk: 1, 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; $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('getCheckedLines()', () => { 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.getCheckedLines()).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', () => { 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(); }); }); describe('updateConcept()', () => { it('should make a POST query saving sale concept', () => { const data = {concept: 'My new weapon'}; const sale = sales[0]; sale.concept = 'My new weapon'; $httpBackend.when('PATCH', `/api/Sales/1`, data).respond(); $httpBackend.expect('PATCH', `/api/Sales/1`, data).respond(); controller.updateConcept(sale); $httpBackend.flush(); }); }); describe('addSale()', () => { it('should make a POST query adding a new sale', () => { 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(); }); }); }); });