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('makeInvoice()', () => { 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.makeInvoice(); 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: Date.vnNew(), maxShipped: Date.vnNew() }; controller.makeInvoice(); 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 minShipped = Date.vnNew(); minShipped.setFullYear(minShipped.getFullYear() - 1); minShipped.setMonth(1); minShipped.setDate(1); minShipped.setHours(0, 0, 0, 0); controller.invoice = { invoiceDate: Date.vnNew(), maxShipped: Date.vnNew(), fromClientId: 1101, toClientId: 1101, companyFk: 442, minShipped: minShipped }; const response = { clientsAndAddresses: [{clientId: 1101, addressId: 121}], invoice: controller.invoice }; const address = {id: 121}; $httpBackend.expect('POST', `InvoiceOuts/clientsToInvoice`).respond(response); $httpBackend.expect('GET', `Addresses/${response.clientsAndAddresses[0].addressId}`).respond(address); $httpBackend.expect('POST', `InvoiceOuts/invoiceClient`).respond({id: 1}); controller.makeInvoice(); $httpBackend.flush(); expect(controller.vnApp.showSuccess).toHaveBeenCalled(); }); }); }); });