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}); controller.$.invoiceButton = {disabled: false}; })); 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'); const filter = { order: 'id DESC', limit: 1 }; const minShipped = new Date(); minShipped.setFullYear(minShipped.getFullYear() - 1); minShipped.setMonth(1); minShipped.setDate(1); minShipped.setHours(0, 0, 0, 0); controller.invoice = { invoiceDate: new Date(), maxShipped: new Date(), fromClientId: 1101, toClientId: 1101, companyFk: 442, minShipped: minShipped }; const response = { clientsAndAddresses: [{clientId: 1101, addressId: 121}], invoice: controller.invoice }; const serializedParams = $httpParamSerializer({filter}); $httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1112}); $httpBackend.expect('POST', `InvoiceOuts/clientToInvoice`).respond(response); $httpBackend.expect('POST', `InvoiceOuts/globalInvoicing`).respond({id: 1}); controller.responseHandler('accept'); $httpBackend.flush(); expect(controller.vnApp.showSuccess).toHaveBeenCalled(); }); }); }); });