import './index';

describe('Client', () => {
    describe('Component vnClientBillingData', () => {
        let $httpBackend;
        let $scope;
        let controller;
        let vnApp;

        beforeEach(ngModule('client'));

        beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _vnApp_) => {
            let $element = angular.element('<vn-client-billing-data></vn-client-billing-data>');
            $httpBackend = _$httpBackend_;
            vnApp = _vnApp_;
            $scope = $rootScope.$new();
            $scope.watcher = {};
            jest.spyOn(vnApp, 'showError');
            controller = $componentController('vnClientBillingData', {$element, $scope});
            controller.client = {id: 1101, name: 'Client name', payMethodFk: 4};
            $scope.watcher.orgData = {id: 1101, name: 'Client name', payMethodFk: 4};
        }));

        describe('hasPaymethodChanges()', () => {
            it(`should return true if there are changes on payMethod data`, () => {
                controller.client.payMethodFk = 5;

                expect(controller.hasPaymethodChanges()).toBeTruthy();
            });

            it(`should return false if there are no changes on payMethod data`, () => {
                controller.client.payMethodFk = 4;

                expect(controller.hasPaymethodChanges()).toBeFalsy();
            });
        });

        describe('onAccept()', () => {
            it('should assign the response id to the client bankEntityFk', () => {
                const expectedResponse = {id: 999};
                controller.onAccept(expectedResponse);

                expect(controller.client.bankEntityFk).toEqual(expectedResponse.id);
            });
        });

        describe('autofillBic()', () => {
            it(`Should do nothing if there is not client`, () => {
                controller.client = undefined;

                controller.autofillBic();

                expect(controller.client).toBeUndefined();
            });

            it(`Should do nothing if the iban is not spanish`, () => {
                controller.client.iban = 'FR9121000418450200051332';

                controller.autofillBic();

                expect(controller.client.bankEntityFk).toBeUndefined();
            });

            it(`Should set the bankEntityId in the client`, () => {
                controller.client.iban = 'ES9121000418450200051332';

                $httpBackend.whenRoute('GET', `BankEntities`).respond([{id: 123}]);
                controller.autofillBic();
                $httpBackend.flush();

                expect(controller.client.bankEntityFk).toEqual(123);
            });

            it(`Should set clients bankEntityFk to null if no bank entity founds`, () => {
                controller.client.iban = 'ES9121000418450200051332';

                $httpBackend.whenRoute('GET', `BankEntities`).respond([]);
                controller.autofillBic();
                $httpBackend.flush();

                expect(controller.client.bankEntityFk).toBeNull();
            });
        });

        describe('ibanCountry()', () => {
            it('should return a country code from iban', () => {
                controller.client.iban = 'ES123';
                let countryCode = controller.ibanCountry;

                expect(countryCode).toEqual('ES');
            });
        });
    });
});