import './index'; const UserError = require('vn-loopback/util/user-error'); 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('makeInvoice()', () => { it('should throw an error when invoiceDate or maxShipped properties are not filled in', () => { jest.spyOn(controller.vnApp, 'showError'); controller.clientsToInvoice = 'all'; let error; try { controller.makeInvoice(); } catch (e) { error = e.message; } const expectedError = 'Invoice date and the max date should be filled'; expect(error).toBe(expectedError); }); it('should throw an error when select one client and clientId is not filled in', () => { jest.spyOn(controller.vnApp, 'showError'); controller.clientsToInvoice = 'one'; let error; try { controller.makeInvoice(); } catch (e) { error = e.message; } const expectedError = 'Choose a valid client'; expect(error).toBe(expectedError); }); it('should make an http POST query and then call to the showSuccess() method', () => { jest.spyOn(controller.vnApp, 'showSuccess'); const date = Date.vnNew(); date.setDate(date.getDate() + 1); controller.invoiceDate = date; controller.maxShipped = date; controller.minInvoicingDate = Date.vnNew(); controller.clientsToInvoice = 'one'; controller.clientId = 1101; controller.companyFk = 442; controller.printerFk = 1; const response = [{ clientId: 1101, id: 121 }]; $httpBackend.expect('POST', `InvoiceOuts/clientsToInvoice`).respond(response); $httpBackend.expect('POST', `InvoiceOuts/invoiceClient`).respond(1); controller.makeInvoice(); $httpBackend.flush(); expect(controller.status).toEqual('done'); }); }); }); });