salix/modules/client/front/balance/create/index.spec.js

163 lines
6.4 KiB
JavaScript

import './index';
describe('Client', () => {
describe('Component vnClientBalancCreate', () => {
let controller;
let $httpBackend;
let $httpParamSerializer;
beforeEach(ngModule('client'));
beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
$httpBackend = _$httpBackend_;
$httpParamSerializer = _$httpParamSerializer_;
let $scope = $rootScope.$new();
const $element = angular.element('<vn-client-balance-create></vn-client-balance-create>');
const $transclude = {
$$boundTransclude: {
$$slots: []
}
};
controller = $componentController('vnClientBalanceCreate', {$element, $scope, $transclude});
controller.receipt = {
clientFk: 1101,
companyFk: 442
};
controller.bankSelection = {accountingType: {code: 'myCode'}};
}));
describe('bankSelection() setter', () => {
it('should set the receipt description property', () => {
controller.originalDescription = 'Albaran: 1, 2';
controller.bankSelection = {
id: 1,
bank: 'Cash',
accountingType: {
id: 2,
receiptDescription: 'Cash'
}
};
expect(controller.receipt.description).toEqual('Cash, Albaran: 1, 2');
});
});
describe('amountToReturn() setter', () => {
it('should set the amount to return with a maximum of two decimals', () => {
controller.amountToReturn = 200.1451789;
expect(controller.amountToReturn).toEqual(200.15);
});
});
describe('getAmountPaid()', () => {
it('should make an http GET query and then set the receipt amountPaid property', () => {
controller.$params = {id: 1101};
const receipt = controller.receipt;
const filter = {
where: {
clientFk: 1101,
companyFk: 442
}
};
const serializedParams = $httpParamSerializer({filter});
$httpBackend.expect('GET', `ClientRisks?${serializedParams}`).respond([{amount: 20}]);
controller.getAmountPaid();
$httpBackend.flush();
expect(receipt.amountPaid).toEqual(20);
});
});
describe('responseHandler()', () => {
it('should make an http POST query and then call to the parent responseHandler() method', () => {
jest.spyOn(controller.vnApp, 'showSuccess');
jest.spyOn(controller.vnReport, 'show');
controller.$params = {id: 1101};
$httpBackend.expect('POST', `Clients/1101/createReceipt`).respond({id: 1});
controller.responseHandler('accept');
$httpBackend.flush();
expect(controller.vnApp.showSuccess).toHaveBeenCalled();
expect(controller.vnReport.show).not.toHaveBeenCalled();
});
it('should make an http POST query and then call to the report show() method', () => {
const receiptId = 1;
jest.spyOn(controller.vnApp, 'showSuccess');
jest.spyOn(controller.vnReport, 'show');
window.open = jest.fn();
controller.$params = {id: 1101};
controller.viewReceipt = true;
$httpBackend.expect('POST', `Clients/1101/createReceipt`).respond({id: receiptId});
controller.responseHandler('accept');
$httpBackend.flush();
expect(controller.vnApp.showSuccess).toHaveBeenCalled();
expect(controller.vnReport.show).toHaveBeenCalledWith(`Receipts/${receiptId}/receipt-pdf`);
});
});
describe('deliveredAmount() setter', () => {
it('should set the deliveredAmount property', () => {
controller.amountPaid = 999;
controller.deliveredAmount = 1000;
expect(controller.amountToReturn).toEqual(1);
});
});
describe('getClientOrSupplierReference', () => {
it('should update receipt description when compensation account is provided', async() => {
jest.spyOn(controller, '$t');
const mockValue = '4100000001';
const mockResponse = {
clientId: '1',
clientName: 'Client mocked'
};
$httpBackend
.expectGET(`Clients/getClientOrSupplierReference?bankAccount=${mockValue}`)
.respond(mockResponse);
controller.getClientOrSupplierReference(mockValue);
$httpBackend.flush();
expect(controller.receipt.compensationAccount).toBe(mockValue);
expect(controller.receipt.description).toBe('Client Compensation Reference');
expect(controller.$t).toHaveBeenCalledWith('Client Compensation Reference', {
clientId: mockResponse.clientId,
clientName: mockResponse.clientName
});
});
it('should update receipt description to empty string if compensation account is not provided', async() => {
controller.getClientOrSupplierReference('');
expect(controller.receipt.compensationAccount).toBe('');
expect(controller.receipt.description).toBe('');
});
});
describe('bankSearchFunc()', () => {
it('should return the filter by id property for an input of a number', () => {
const bankId = 1;
const result = controller.bankSearchFunc(bankId);
expect(result).toEqual({id: bankId});
});
it('should return the filter by bank property for an input of an string', () => {
const bankName = 'Bank of America';
const result = controller.bankSearchFunc(bankName);
expect(result).toEqual({bank: {like: '%' + bankName + '%'}});
});
});
});
});