import './index';

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

    beforeEach(ngModule('account'));

    beforeEach(inject(($componentController, _$httpBackend_) => {
        $httpBackend = _$httpBackend_;
        controller = $componentController('vnUserAliases', {$element: null});
        jest.spyOn(controller.vnApp, 'showSuccess');
    }));

    describe('refresh()', () => {
        it('should refresh the controller data', () => {
            $httpBackend.expectGET('MailAliasAccounts').respond('foo');
            controller.refresh();
            $httpBackend.flush();

            expect(controller.$.data).toBe('foo');
        });
    });

    describe('onAddSave()', () => {
        it('should add the new row', () => {
            controller.addData = {account: 1};

            $httpBackend.expectPOST('MailAliasAccounts').respond();
            $httpBackend.expectGET('MailAliasAccounts').respond('foo');
            controller.onAddSave();
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
        });
    });

    describe('onRemove()', () => {
        it('shoud remove the passed row remote and locally', () => {
            controller.$.data = [
                {id: 1, alias: 'foo'},
                {id: 2, alias: 'bar'}
            ];

            $httpBackend.expectDELETE('MailAliasAccounts/1').respond();
            controller.onRemove(controller.$.data[0]);
            $httpBackend.flush();

            expect(controller.$.data).toEqual([{id: 2, alias: 'bar'}]);
            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
        });
    });
});