import './index';

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

    beforeEach(ngModule('client'));

    beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_, _vnApp_) => {
        $scope = $rootScope.$new();
        $httpBackend = _$httpBackend_;
        $httpParamSerializer = _$httpParamSerializer_;
        vnApp = _vnApp_;
        jest.spyOn(vnApp, 'showError');
        const $element = angular.element('<vn-client-web-access></vn-client-web-access>');
        controller = $componentController('vnClientWebAccess', {$element, $scope});
    }));

    describe('$onChanges()', () => {
        it(`should pass client's account data to account then call isCustomer function`, () => {
            jest.spyOn(controller, 'isCustomer');
            controller.client = {client: 'Bruce Wayne', account: 'Wayne Industries'};
            controller.account = {};
            controller.$onChanges();

            expect(controller.account).toBe('Wayne Industries');
            expect(controller.isCustomer).toHaveBeenCalledWith();
        });
    });

    describe('isCustomer()', () => {
        it('should return true if the password can be modified', () => {
            controller.client = {id: '1234'};
            const filter = {where: {id: controller.client.id}};
            const serializedParams = $httpParamSerializer({filter});

            $httpBackend.expectGET(`VnUsers/preview?${serializedParams}`).respond('foo');
            $httpBackend.expectGET(`Clients/${controller.client.id}/hasCustomerRole`).respond(true);
            controller.isCustomer();
            $httpBackend.flush();

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

        it(`should return a false if the password can't be modified`, () => {
            controller.client = {id: '1234'};
            const filter = {where: {id: controller.client.id}};
            const serializedParams = $httpParamSerializer({filter});

            $httpBackend.expectGET(`VnUsers/preview?${serializedParams}`).respond('foo');
            $httpBackend.expectGET(`Clients/${controller.client.id}/hasCustomerRole`).respond(false);
            controller.isCustomer();
            $httpBackend.flush();

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

    describe('checkConditions()', () => {
        it('should perform a query to check if the client is valid', () => {
            controller.client = {id: '1234'};
            const filter = {where: {id: controller.client.id}};
            const serializedParams = $httpParamSerializer({filter});

            expect(controller.canEnableCheckBox).toBeTruthy();

            $httpBackend.expectGET(`VnUsers/preview?${serializedParams}`).respond('foo');
            $httpBackend.expectGET(`Clients/${controller.client.id}/isValidClient`).respond(false);
            controller.checkConditions();
            $httpBackend.flush();

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

    describe('onPassOpen()', () => {
        it('should set passwords to empty values', () => {
            controller.newPassword = 'm24x8';
            controller.repeatPassword = 'm24x8';
            controller.onPassOpen();

            expect(controller.newPassword).toBe('');
            expect(controller.repeatPassword).toBe('');
        });
    });

    describe('onPassChange()', () => {
        it('should request to update the password', () => {
            controller.client = {id: '1234'};
            controller.newPassword = 'm24x8';
            controller.repeatPassword = 'm24x8';
            controller.canChangePassword = true;
            const filter = {where: {id: controller.client.id}};
            const serializedParams = $httpParamSerializer({filter});

            $httpBackend.expectGET(`VnUsers/preview?${serializedParams}`).respond('foo');
            const query = `Clients/${controller.client.id}/setPassword`;
            $httpBackend.expectPATCH(query, {newPassword: controller.newPassword}).respond('done');
            controller.onPassChange();
            $httpBackend.flush();
        });

        describe(`when password is empty`, () => {
            it(`should throw 'You must enter a new password' error`, () => {
                controller.client = {id: '1234'};
                controller.newPassword = '';
                controller.canChangePassword = true;
                controller.onPassChange();

                expect(vnApp.showError).toHaveBeenCalledWith(`You must enter a new password`);
            });
        });

        describe(`when passwords don't match`, () => {
            it(`should throw Passwords don't match error`, () => {
                controller.client = {id: '1234'};
                controller.newPassword = 'm24x8';
                controller.canChangePassword = true;
                controller.repeatPassword = 'notMatchingPassword';
                controller.onPassChange();

                expect(vnApp.showError).toHaveBeenCalledWith(`Passwords don't match`);
            });
        });
    });
});