import './index'; import watcher from 'core/mocks/watcher'; describe('Client', () => { describe('Component vnClientFiscalData', () => { let $httpBackend; let $scope; let $element; let controller; beforeEach(ngModule('client')); beforeEach(inject(($componentController, $rootScope, _$httpBackend_) => { $httpBackend = _$httpBackend_; $scope = $rootScope.$new(); $scope.watcher = watcher; $scope.watcher.orgData = {id: 1101, isEqualizated: false, isTaxDataChecked: false}; $element = angular.element(''); controller = $componentController('vnClientFiscalData', {$element, $scope}); controller.card = {reload: () => {}}; controller.client = { id: 1101, email: 'batman@gothamcity.com', phone: '1111111111', isEqualizated: false, isTaxDataChecked: false }; controller._province = {}; controller._town = {}; controller._postcode = {}; })); describe('onSubmit()', () => { it('should call the save() method directly', () => { jest.spyOn(controller, 'save'); controller.onSubmit(); expect(controller.save).toHaveBeenCalledWith(); }); it('should call the checkExistingClient() if the isTaxDataChecked property is checked', () => { jest.spyOn(controller, 'save'); jest.spyOn(controller, 'checkExistingClient'); controller.client.isTaxDataChecked = true; controller.onSubmit(); expect(controller.save).not.toHaveBeenCalledWith(); expect(controller.checkExistingClient).toHaveBeenCalledWith(); }); it('should enable the hasToInvoice property any time the form activates the client with isActive', () => { $scope.watcher.orgData.isActive = false; controller.client.isActive = true; controller.client.hasToInvoice = false; controller.onSubmit(); expect(controller.client.hasToInvoice).toBe(true); }); }); 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 filter = encodeURIComponent(JSON.stringify(filterObj)); $httpBackend.expect('GET', `Clients/findOne?filter=${filter}`).respond(404); controller.checkExistingClient(); $httpBackend.flush(); }); it(`should show a save confirmation and then set the despiteOfClient property`, () => { controller.$.confirmDuplicatedClient = {show: () => {}}; jest.spyOn(controller.$.confirmDuplicatedClient, 'show'); const filterObj = { where: { and: [ {or: [{email: controller.client.email}, {phone: controller.client.phone}]}, {id: {neq: controller.client.id}} ] } }; const expectedClient = {id: 1102}; 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(1102); }); }); describe('checkEtChanges()', () => { it(`should show a propagation confirmation if isEqualizated property is changed and invoice by address is checked`, () => { controller.$.propagateIsEqualizated = {show: () => {}}; jest.spyOn(controller.$.propagateIsEqualizated, 'show'); 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`, () => { jest.spyOn(controller, 'onAcceptEt'); 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.expectPATCH(`Clients/${controller.client.id}/addressesPropagateRe`, {isEqualizated: controller.client.isEqualizated}).respond('done'); controller.onAcceptEt(); $httpBackend.flush(); }); }); describe('province() setter', () => { it(`should set countryFk property`, () => { controller.client.countryFk = null; controller.province = { id: 1, name: 'New york', country: { id: 2, name: 'USA' } }; expect(controller.client.countryFk).toEqual(2); }); }); describe('town() setter', () => { it(`should set provinceFk property`, () => { controller.town = { provinceFk: 1, code: 46001, province: { id: 1, name: 'New york', country: { id: 2, name: 'USA' } }, postcodes: [] }; expect(controller.client.provinceFk).toEqual(1); }); it(`should set provinceFk property and fill the postalCode if there's just one`, () => { controller.town = { provinceFk: 1, code: 46001, province: { id: 1, name: 'New york', country: { id: 2, name: 'USA' } }, postcodes: [{code: '46001'}] }; expect(controller.client.provinceFk).toEqual(1); expect(controller.client.postcode).toEqual('46001'); }); }); describe('postcode() setter', () => { it(`should set the town, provinceFk and contryFk properties`, () => { controller.postcode = { townFk: 1, code: 46001, town: { id: 1, name: 'New York', province: { id: 1, name: 'New york', country: { id: 2, name: 'USA' } } } }; expect(controller.client.city).toEqual('New York'); expect(controller.client.provinceFk).toEqual(1); expect(controller.client.countryFk).toEqual(2); }); }); }); });