import './index.js'; import crudModel from 'core/mocks/crud-model'; describe('Item', () => { describe('Component vnItemRequest', () => { let $scope; let controller; let $httpBackend; beforeEach(ngModule('item')); beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => { $httpBackend = _$httpBackend_; $scope = $rootScope.$new(); $scope.model = crudModel; $scope.denyReason = {hide: () => {}}; controller = $componentController('vnItemRequest', {$scope: $scope}); })); describe('getState()', () => { it(`should return an string depending to the isOK value`, () => { let isOk = null; let result = controller.getState(isOk); expect(result).toEqual('Nueva'); isOk = 1; result = controller.getState(isOk); expect(result).toEqual('Aceptada'); isOk = 0; result = controller.getState(isOk); expect(result).toEqual('Denegada'); }); }); describe('confirmRequest()', () => { it(`should do nothing if the request does't have itemFk or saleQuantity`, () => { let request = {}; spyOn(controller.vnApp, 'showSuccess'); controller.confirmRequest(request); expect(controller.vnApp.showSuccess).not.toHaveBeenCalledWith(); }); it('should perform a query and call vnApp.showSuccess() and refresh if the conditions are met', () => { spyOn(controller.vnApp, 'showSuccess'); let model = controller.$.model; spyOn(model, 'refresh'); let request = {itemFk: 1, saleQuantity: 1, id: 1}; $httpBackend.when('POST', `/api/TicketRequests/${request.id}/confirm`).respond(); $httpBackend.expect('POST', `/api/TicketRequests/${request.id}/confirm`).respond(); controller.confirmRequest(request); $httpBackend.flush(); expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); expect($scope.model.refresh).toHaveBeenCalledWith(); }); }); describe('changeQuantity()', () => { it(`should call confirmRequest() if there's no sale id in the request`, () => { let request = {}; spyOn(controller, 'confirmRequest'); controller.changeQuantity(request); expect(controller.confirmRequest).toHaveBeenCalledWith(jasmine.any(Object)); }); it(`should perform a query and call vnApp.showSuccess() if the conditions are met`, () => { let request = {saleFk: 1, saleQuantity: 1}; spyOn(controller.vnApp, 'showSuccess'); $httpBackend.when('PATCH', `/api/Sales/${request.saleFk}/`).respond(); $httpBackend.expect('PATCH', `/api/Sales/${request.saleFk}/`).respond(); controller.changeQuantity(request); $httpBackend.flush(); expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); }); }); describe('compareDate()', () => { it(`should return "success" if receives a future date`, () => { let date = '3019-02-18T11:00:00.000Z'; let result = controller.compareDate(date); expect(result).toEqual('success'); }); it(`should return "warning" if date is today`, () => { let date = new Date(); let result = controller.compareDate(date); expect(result).toEqual('warning'); }); }); describe('denyRequest()', () => { it(`should perform a query and call vnApp.showSuccess(), refresh(), hide() and set denyObservation to null in the controller`, () => { spyOn(controller.vnApp, 'showSuccess'); let model = controller.$.model; spyOn(model, 'refresh'); spyOn(controller.$.denyReason, 'hide'); controller.denyRequestId = 1; $httpBackend.when('POST', `/api/TicketRequests/${controller.denyRequestId}/deny`).respond(); $httpBackend.expect('POST', `/api/TicketRequests/${controller.denyRequestId}/deny`).respond(); controller.denyRequest(); $httpBackend.flush(); expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); expect($scope.model.refresh).toHaveBeenCalledWith(); expect($scope.denyReason.hide).toHaveBeenCalledWith(); }); }); }); });