salix/modules/invoiceOut/front/global-invoicing/index.spec.js

122 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-12-29 10:47:12 +00:00
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('<vn-invoice-out-global-invoicing></vn-invoice-out-global-invoicing>');
const $transclude = {
$$boundTransclude: {
$$slots: []
}
};
controller = $componentController('vnInvoiceOutGlobalInvoicing', {$element, $scope, $transclude});
controller.$.invoiceButton = {disabled: false};
}));
2022-12-27 11:21:04 +00:00
2022-12-29 10:47:12 +00:00
describe('getMinClientId()', () => {
it('should set the invoice fromClientId property', () => {
const filter = {
order: 'id ASC',
limit: 1
};
2022-12-27 11:21:04 +00:00
2022-12-29 10:47:12 +00:00
const serializedParams = $httpParamSerializer({filter});
$httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1101});
2022-12-27 11:21:04 +00:00
2022-12-29 10:47:12 +00:00
controller.getMinClientId();
$httpBackend.flush();
2022-12-27 11:21:04 +00:00
2022-12-29 10:47:12 +00:00
expect(controller.invoice.fromClientId).toEqual(1101);
});
2022-12-27 11:21:04 +00:00
});
2022-12-29 10:47:12 +00:00
describe('getMaxClientId()', () => {
it('should set the invoice toClientId property', () => {
const filter = {
order: 'id DESC',
limit: 1
};
2022-12-27 11:21:04 +00:00
2022-12-29 10:47:12 +00:00
const serializedParams = $httpParamSerializer({filter});
$httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1112});
2022-12-27 11:21:04 +00:00
2022-12-29 10:47:12 +00:00
controller.getMaxClientId();
$httpBackend.flush();
2022-12-27 11:21:04 +00:00
2022-12-29 10:47:12 +00:00
expect(controller.invoice.toClientId).toEqual(1112);
});
2022-12-27 11:21:04 +00:00
});
2022-12-29 10:47:12 +00:00
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 = {
2023-01-16 14:18:24 +00:00
invoiceDate: Date.vnNew(),
maxShipped: Date.vnNew()
2022-12-29 10:47:12 +00:00
};
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');
2023-01-16 14:18:24 +00:00
const minShipped = Date.vnNew();
2022-12-29 10:47:12 +00:00
minShipped.setFullYear(minShipped.getFullYear() - 1);
minShipped.setMonth(1);
minShipped.setDate(1);
minShipped.setHours(0, 0, 0, 0);
controller.invoice = {
2023-01-16 14:18:24 +00:00
invoiceDate: Date.vnNew(),
maxShipped: Date.vnNew(),
2022-12-29 10:47:12 +00:00
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();
});
2022-12-27 11:21:04 +00:00
});
});
});