import './index';

describe('Client', () => {
    describe('Component vnClientAddressEdit', () => {
        let $scope;
        let controller;
        let $httpBackend;
        let $element;
        let $state;

        beforeEach(ngModule('client'));

        beforeEach(inject(($componentController, $rootScope, _$state_, _$httpBackend_) => {
            $scope = $rootScope.$new();
            $httpBackend = _$httpBackend_;
            $state = _$state_;
            $state.params.addressId = '1';
            $element = angular.element('<vn-client-address-edit></vn-client-address-edit>');
            controller = $componentController('vnClientAddressEdit', {$element, $scope});
            controller.address = {id: 1, customsAgentFk: null};
            controller.$.watcher = {
                setDirty: () => {},
                setPristine: () => {},
                realSubmit: () => {},
                check: () => {},
                notifySaved: () => {}
            };
            controller.$.model = {
                remove: () => {},
                save: () => {}
            };
            controller.card = {
                reload: () => {}
            };
        }));

        describe('removeObservation()', () => {
            it('should call $.watcher.setDirty() and $.model.remove(index)', () => {
                jest.spyOn(controller.$.watcher, 'setDirty');
                jest.spyOn(controller.$.model, 'remove');
                controller.removeObservation(1);

                expect(controller.$.model.remove).toHaveBeenCalledWith(1);
                expect(controller.$.watcher.setDirty).toHaveBeenCalledWith();
            });
        });

        describe('cancel()', () => {
            it('should call goToIndex()', () => {
                jest.spyOn(controller, 'goToIndex');
                controller.cancel();

                expect(controller.goToIndex).toHaveBeenCalledWith();
            });
        });

        describe('goToIndex()', () => {
            it('should call $state.go("client.card.address.index")', () => {
                jest.spyOn(controller.$state, 'go');
                controller.goToIndex();

                expect(controller.$state.go).toHaveBeenCalledWith('client.card.address.index');
            });
        });

        describe('onCustomAgentAccept()', () => {
            it(`should now create a new customs agent and then set the customsAgentFk property on the address`, () => {
                const expectedResult = {id: 1, fiscalName: 'Customs agent one'};
                $httpBackend.when('POST', 'CustomsAgents').respond(200, expectedResult);
                controller.onCustomAgentAccept();
                $httpBackend.flush();

                expect(controller.address.customsAgentFk).toEqual(1);
            });
        });
    });
});