import './index.js'; describe('Ticket', () => { describe('Component vnTicketSale', () => { let $componentController; let controller; let $httpBackend; let $state; let $scope; beforeEach(() => { angular.mock.module('ticket'); }); beforeEach(angular.mock.inject((_$componentController_, _$state_, _$httpBackend_, $rootScope) => { $componentController = _$componentController_; $httpBackend = _$httpBackend_; $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); $httpBackend.when('GET', '/api/Tickets/1/getSales').respond({}); $scope = $rootScope.$new(); $state = _$state_; $state.params.id = 1; $scope.model = {data: [ { id: 1, quantity: 5, price: 23.5, discount: 0, checked: true }, { id: 4, quantity: 20, price: 5.5, discount: 0, checked: false } ], refresh: () => {}}; $scope.addTurn = {show: () => {}}; controller = $componentController('vnTicketSale', {$scope: $scope}, {$state: $state}); controller.ticket = {id: 1, clientFk: 1, shipped: 1, client: {salesPersonFk: 1}}; spyOn(window, 'open'); })); describe('createClaim()', () => { it('should perfrom a query and call windows open', () => { controller.sales = controller.$scope.model.data; let res = {id: 1}; $httpBackend.expectPOST(`claim/api/Claims/createFromSales`).respond(res); controller.createClaim(); $httpBackend.flush(); let urlMock = null; expect(window.open).toHaveBeenCalledWith(urlMock, '_blank'); }); }); describe('getSales()', () => { it('should make a query and call getTaxes()', () => { let data = [ { id: 1, quantity: 5, price: 23.5, discount: 0, checked: true }, { id: 4, quantity: 20, price: 5.5, discount: 0, checked: false } ]; spyOn(controller, 'getTaxes'); controller.onDataChange(); expect(controller.sales).toEqual(data); expect(controller.getTaxes).toHaveBeenCalledWith(); }); }); describe('$onChanges()', () => { it('should call getSales and getVAT', () => { spyOn(controller, 'getSubTotal'); spyOn(controller, 'getVAT'); controller.getTaxes(); expect(controller.getSubTotal).toHaveBeenCalledWith(); expect(controller.getVAT).toHaveBeenCalledWith(); }); }); describe('getSubTotal()', () => { it('should calculate SubTotal', () => { controller.sales = [{quantity: 2, price: 10, discount: 10}]; controller.getSubTotal(); expect(controller.subTotal).toEqual(18); }); }); describe('getVAT()', () => { it('should make a query, set vat and calculate total', () => { controller.subTotal = 10; $httpBackend.expectGET(`/ticket/api/Tickets/1/getVAT`).respond(); controller.getVAT(); $httpBackend.flush(); expect(controller.total).toEqual(10); }); }); describe('isChecked() setter/getter', () => { it('should set isChecked value to true when one of the instances has the value checked to true', () => { let lines = [ {checked: false}, {checked: true} ]; controller.sales = lines; expect(controller.isChecked).toBeTruthy(); }); }); describe('getCheckedLines()', () => { it('should make an array of the instances with the property checked true()', () => { let expectedResult = [{id: 1, instance: 0}]; controller.sales = controller.$scope.model.data; 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(`/ticket/api/States?filter=${filter}`).respond(res); $httpBackend.expectGET(`/ticket/api/States?filter=${filter}`); controller.onStateOkClick(); $httpBackend.flush(); expect(controller.onStateChange).toHaveBeenCalledWith(3); }); }); describe('showAddTurnDialog()', () => { it('should call contrtoller.$scope.addTurn.show()', () => { controller.$scope.addTurn = {show: () => {}}; spyOn(controller.$scope.addTurn, 'show'); controller.showAddTurnDialog(); expect(controller.$scope.addTurn.show).toHaveBeenCalledWith(); }); }); describe('addTurn(day)', () => { it('should make a query and call $.addTurn.hide() and vnApp.showSuccess()', () => { controller.$scope.addTurn = {hide: () => {}}; spyOn(controller.$scope.addTurn, 'hide'); $httpBackend.expectPATCH(`/ticket/api/TicketWeeklies`).respond(); controller.addTurn(1); $httpBackend.flush(); expect(controller.$scope.addTurn.hide).toHaveBeenCalledWith(); }); }); describe('onStateChange()', () => { it('should perform a post and then call a function', () => { $httpBackend.expectPOST(`/ticket/api/TicketTrackings/changeState`).respond(); controller.card = {reload: () => {}}; controller.onStateChange(3); $httpBackend.flush(); }); }); describe('onRemoveLinesClick()', () => { it('should call getCheckedLines, call removeInstances, and make a query', () => { spyOn(controller, 'getCheckedLines'); spyOn(controller, 'removeInstances'); $httpBackend.expectPOST(`/ticket/api/Sales/removes`).respond(); controller.onRemoveLinesClick('ACCEPT'); $httpBackend.flush(); expect(controller.getCheckedLines).toHaveBeenCalledWith(); expect(controller.removeInstances).toHaveBeenCalledWith(undefined); }); }); 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 ', () => { spyOn(controller, 'getCheckedLines'); $httpBackend.expectPOST(`/ticket/api/Sales/reserve`).respond(); controller.setReserved(true); $httpBackend.flush(); expect(controller.getCheckedLines).toHaveBeenCalledWith(); }); }); }); });