salix/modules/client/front/fiscal-data/index.spec.js

134 lines
5.6 KiB
JavaScript
Raw Normal View History

2018-05-23 12:26:51 +00:00
import './index';
2020-02-04 10:25:15 +00:00
import watcher from 'core/mocks/watcher';
describe('Client', () => {
describe('Component vnClientFiscalData', () => {
let $httpBackend;
let $scope;
2020-02-04 10:25:15 +00:00
let $element;
let controller;
beforeEach(ngModule('client'));
beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => {
$httpBackend = _$httpBackend_;
$scope = $rootScope.$new();
2020-02-04 10:25:15 +00:00
$scope.watcher = watcher;
$scope.watcher.orgData = {id: 101, isEqualizated: false, isTaxDataChecked: false};
$element = angular.element('<vn-client-fiscal-data></client-fiscal-data>');
controller = $componentController('vnClientFiscalData', {$element, $scope});
controller.card = {reload: () => {}};
2020-02-04 10:25:15 +00:00
controller.client = {
id: 101,
email: 'batman@gothamcity.com',
phone: '1111111111',
isEqualizated: false,
isTaxDataChecked: false
};
}));
2020-02-04 10:25:15 +00:00
describe('onSubmit()', () => {
it('should call the save() method directly', () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'save');
2020-02-04 10:25:15 +00:00
controller.onSubmit();
expect(controller.save).toHaveBeenCalledWith();
});
it('should call the checkExistingClient() if the isTaxDataChecked property is checked', () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'save');
jest.spyOn(controller, 'checkExistingClient');
2020-02-04 10:25:15 +00:00
controller.client.isTaxDataChecked = true;
controller.onSubmit();
expect(controller.save).not.toHaveBeenCalledWith();
expect(controller.checkExistingClient).toHaveBeenCalledWith();
});
});
describe('checkExistingClient()', () => {
it(`should make a HTTP GET query filtering by email, phone and mobile`, () => {
controller.client.mobile = 222222222;
const filterObj = {
where: {
and: [
{or: [
{email: controller.client.email},
{phone: controller.client.phone},
{mobile: controller.client.mobile}
]},
{id: {neq: controller.client.id}}
]
}
};
const expectedClient = {id: 102};
const filter = encodeURIComponent(JSON.stringify(filterObj));
$httpBackend.expect('GET', `Clients/findOne?filter=${filter}`).respond(expectedClient);
controller.checkExistingClient();
$httpBackend.flush();
});
it(`should show a save confirmation and then set the despiteOfClient property`, () => {
2020-02-04 10:25:15 +00:00
controller.$.confirmDuplicatedClient = {show: () => {}};
2020-02-26 12:22:52 +00:00
jest.spyOn(controller.$.confirmDuplicatedClient, 'show');
2020-02-04 10:25:15 +00:00
const filterObj = {
where: {
and: [
{or: [{email: controller.client.email}, {phone: controller.client.phone}]},
{id: {neq: controller.client.id}}
]
}
};
const expectedClient = {id: 102};
const filter = encodeURIComponent(JSON.stringify(filterObj));
$httpBackend.expect('GET', `Clients/findOne?filter=${filter}`).respond(expectedClient);
controller.checkExistingClient();
$httpBackend.flush();
expect(controller.$.confirmDuplicatedClient.show).toHaveBeenCalledWith();
expect(controller.client.despiteOfClient).toEqual(102);
});
});
describe('checkEtChanges()', () => {
it(`should show a propagation confirmation if isEqualizated property is changed and invoice by address is checked`, () => {
controller.$.propagateIsEqualizated = {show: () => {}};
2020-02-26 12:22:52 +00:00
jest.spyOn(controller.$.propagateIsEqualizated, 'show');
2020-02-04 10:25:15 +00:00
const orgData = $scope.watcher.orgData;
orgData.hasToInvoiceByAddress = true;
controller.client.isEqualizated = true;
controller.checkEtChanges(orgData);
expect(controller.$.propagateIsEqualizated.show).toHaveBeenCalledWith();
});
it(`should call to the onAcceptEt() method if isEqualizated property is changed and invoice by address isn't checked`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'onAcceptEt');
2020-02-04 10:25:15 +00:00
const orgData = $scope.watcher.orgData;
orgData.hasToInvoiceByAddress = false;
controller.client.isEqualizated = true;
controller.checkEtChanges(orgData);
expect(controller.onAcceptEt).toHaveBeenCalledWith();
});
});
describe('onAcceptEt()', () => {
it('should request to patch the propagation of tax status', () => {
controller.client = {id: 123, isEqualizated: false};
$httpBackend.when('PATCH', `Clients/${controller.client.id}/addressesPropagateRe`, {isEqualizated: controller.client.isEqualizated}).respond('done');
$httpBackend.expectPATCH(`Clients/${controller.client.id}/addressesPropagateRe`, {isEqualizated: controller.client.isEqualizated});
2020-02-04 10:25:15 +00:00
controller.onAcceptEt();
$httpBackend.flush();
});
});
});
});