import './index';

describe('component vnUserDescriptor', () => {
    let controller;
    let $httpBackend;

    let user = {id: 1, name: 'foo'};

    beforeEach(ngModule('account'));

    beforeEach(inject(($componentController, _$httpBackend_) => {
        $httpBackend = _$httpBackend_;
        $httpBackend.whenGET('UserAccounts/1/exists').respond({exists: true});

        controller = $componentController('vnUserDescriptor', {$element: null}, {user});
        jest.spyOn(controller, 'emit');
        jest.spyOn(controller.vnApp, 'showSuccess');
    }));

    describe('onDelete()', () => {
        it('should delete entity and go to index', () => {
            controller.$state.go = jest.fn();

            $httpBackend.expectDELETE('Accounts/1').respond();
            controller.onDelete();
            $httpBackend.flush();

            expect(controller.$state.go).toHaveBeenCalledWith('account.index');
            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
        });
    });

    describe('onChangeRoleAccept()', () => {
        it('should call backend method to change role', () => {
            $httpBackend.expectPATCH('Accounts/1').respond();
            controller.onChangeRoleAccept();
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            expect(controller.emit).toHaveBeenCalledWith('change');
        });
    });

    describe('onPassChange()', () => {
        it('should throw an error when password is empty', () => {
            expect(() => {
                controller.onPassChange();
            }).toThrowErrorMatchingSnapshot();
        });

        it('should throw an error when repeat password not matches new password', () => {
            controller.newPassword = 'foo';
            controller.repeatPassword = 'bar';

            expect(() => {
                controller.onPassChange();
            }).toThrowErrorMatchingSnapshot();
        });

        it('should make a request when password checks passes', () => {
            controller.newPassword = 'foo';
            controller.repeatPassword = 'foo';

            $httpBackend.expectPATCH('Accounts/1/setPassword').respond();
            controller.onPassChange();
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            expect(controller.emit).toHaveBeenCalledWith('change');
        });
    });

    describe('onEnableAccount()', () => {
        it('should make request to enable account', () => {
            $httpBackend.expectPOST('UserAccounts', {id: 1}).respond();
            controller.onEnableAccount();
            $httpBackend.flush();

            expect(controller.hasAccount).toBeTruthy();
            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            expect(controller.emit).toHaveBeenCalledWith('change');
        });
    });

    describe('onDisableAccount()', () => {
        it('should make request to disable account', () => {
            $httpBackend.expectDELETE('UserAccounts/1').respond();
            controller.onDisableAccount();
            $httpBackend.flush();

            expect(controller.hasAccount).toBeFalsy();
            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            expect(controller.emit).toHaveBeenCalledWith('change');
        });
    });

    describe('onSetActive()', () => {
        it('should make request to activate/deactivate the user', () => {
            $httpBackend.expectPATCH('Accounts/1', {active: true}).respond();
            controller.onSetActive(true);
            $httpBackend.flush();

            expect(controller.user.active).toBeTruthy();
            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            expect(controller.emit).toHaveBeenCalledWith('change');
        });
    });
});