import './index'; describe('InvoiceOut', () => { describe('Component vnInvoiceOutGlobalInvoicing', () => { let controller; let $httpBackend; let $httpParamSerializer; beforeEach(ngModule('invoiceOut')); beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { $httpBackend = _$httpBackend_; $httpParamSerializer = _$httpParamSerializer_; let $scope = $rootScope.$new(); const $element = angular.element(''); const $transclude = { $$boundTransclude: { $$slots: [] } }; controller = $componentController('vnInvoiceOutGlobalInvoicing', {$element, $scope, $transclude}); })); describe('getMinClientId()', () => { it('should set the invoice fromClientId property', () => { const filter = { order: 'id ASC', limit: 1 }; const serializedParams = $httpParamSerializer({filter}); $httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1101}); controller.getMinClientId(); $httpBackend.flush(); expect(controller.invoice.fromClientId).toEqual(1101); }); }); describe('getMaxClientId()', () => { it('should set the invoice toClientId property', () => { const filter = { order: 'id DESC', limit: 1 }; const serializedParams = $httpParamSerializer({filter}); $httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1112}); controller.getMaxClientId(); $httpBackend.flush(); expect(controller.invoice.toClientId).toEqual(1112); }); }); describe('responseHandler()', () => { it('should throw an error when invoiceDate or maxShipped properties are not filled in', () => { jest.spyOn(controller.vnApp, 'showError'); controller.invoice = { fromClientId: 1101, toClientId: 1101 }; controller.responseHandler('accept'); const expectedError = 'Invoice date and the max date should be filled'; expect(controller.vnApp.showError).toHaveBeenCalledWith(expectedError); }); it('should throw an error when fromClientId or toClientId properties are not filled in', () => { jest.spyOn(controller.vnApp, 'showError'); controller.invoice = { invoiceDate: new Date(), maxShipped: new Date() }; controller.responseHandler('accept'); expect(controller.vnApp.showError).toHaveBeenCalledWith(`Choose a valid clients range`); }); it('should make an http POST query and then call to the showSuccess() method', () => { jest.spyOn(controller.vnApp, 'showSuccess'); controller.invoice = { invoiceDate: new Date(), maxShipped: new Date(), fromClientId: 1101, toClientId: 1101 }; $httpBackend.expect('POST', `InvoiceOuts/globalInvoicing`).respond({id: 1}); controller.responseHandler('accept'); $httpBackend.flush(); expect(controller.vnApp.showSuccess).toHaveBeenCalled(); }); }); }); });