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

124 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-08-02 11:35:38 +00:00
import './index';
describe('InvoiceOut', () => {
2021-08-10 11:57:03 +00:00
describe('Component vnInvoiceOutGlobalInvoicing', () => {
2021-08-02 11:35:38 +00:00
let controller;
let $httpBackend;
2021-08-10 11:57:03 +00:00
let $httpParamSerializer;
2021-08-02 11:35:38 +00:00
beforeEach(ngModule('invoiceOut'));
2021-08-10 11:57:03 +00:00
beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
2021-08-02 11:35:38 +00:00
$httpBackend = _$httpBackend_;
2021-08-10 11:57:03 +00:00
$httpParamSerializer = _$httpParamSerializer_;
2021-08-02 11:35:38 +00:00
let $scope = $rootScope.$new();
2021-08-10 11:57:03 +00:00
const $element = angular.element('<vn-invoice-out-global-invoicing></vn-invoice-out-global-invoicing>');
2021-08-02 11:35:38 +00:00
const $transclude = {
$$boundTransclude: {
$$slots: []
}
};
2021-08-10 11:57:03 +00:00
controller = $componentController('vnInvoiceOutGlobalInvoicing', {$element, $scope, $transclude});
2022-10-17 11:02:19 +00:00
controller.$.invoiceButton = {disabled: false};
2021-08-02 11:35:38 +00:00
}));
2021-08-10 11:57:03 +00:00
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);
});
});
2021-08-02 11:35:38 +00:00
describe('responseHandler()', () => {
2021-08-10 11:57:03 +00:00
it('should throw an error when invoiceDate or maxShipped properties are not filled in', () => {
2021-08-02 11:35:38 +00:00
jest.spyOn(controller.vnApp, 'showError');
controller.invoice = {
2021-08-10 11:57:03 +00:00
fromClientId: 1101,
toClientId: 1101
2021-08-02 11:35:38 +00:00
};
controller.responseHandler('accept');
const expectedError = 'Invoice date and the max date should be filled';
expect(controller.vnApp.showError).toHaveBeenCalledWith(expectedError);
2021-08-02 11:35:38 +00:00
});
2021-08-10 11:57:03 +00:00
it('should throw an error when fromClientId or toClientId properties are not filled in', () => {
2021-08-02 11:35:38 +00:00
jest.spyOn(controller.vnApp, 'showError');
controller.invoice = {
2021-08-10 11:57:03 +00:00
invoiceDate: new Date(),
maxShipped: new Date()
2021-08-02 11:35:38 +00:00
};
controller.responseHandler('accept');
2021-08-10 11:57:03 +00:00
expect(controller.vnApp.showError).toHaveBeenCalledWith(`Choose a valid clients range`);
2021-08-02 11:35:38 +00:00
});
2021-08-10 11:57:03 +00:00
it('should make an http POST query and then call to the showSuccess() method', () => {
2021-08-02 11:35:38 +00:00
jest.spyOn(controller.vnApp, 'showSuccess');
2022-10-17 11:02:19 +00:00
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);
2021-08-02 11:35:38 +00:00
controller.invoice = {
2021-08-10 11:57:03 +00:00
invoiceDate: new Date(),
maxShipped: new Date(),
fromClientId: 1101,
2022-10-17 11:02:19 +00:00
toClientId: 1101,
companyFk: 442,
minShipped: minShipped
};
const response = {
clientsAndAddresses: [{clientId: 1101, addressId: 121}],
invoice: controller.invoice
};
2021-08-02 11:35:38 +00:00
2022-10-17 11:02:19 +00:00
const serializedParams = $httpParamSerializer({filter});
$httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1112});
$httpBackend.expect('POST', `InvoiceOuts/clientToInvoice`).respond(response);
2021-08-10 11:57:03 +00:00
$httpBackend.expect('POST', `InvoiceOuts/globalInvoicing`).respond({id: 1});
2021-08-02 11:35:38 +00:00
controller.responseHandler('accept');
$httpBackend.flush();
expect(controller.vnApp.showSuccess).toHaveBeenCalled();
});
});
});
});