import './index';
import watcher from 'core/mocks/watcher';

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

        beforeEach(ngModule('client'));

        beforeEach(angular.mock.inject(($componentController, $rootScope, _$state_, _$httpBackend_) => {
            $scope = $rootScope.$new();
            $httpBackend = _$httpBackend_;
            $state = _$state_;
            $state.params.id = '1234';
            $element = angular.element('<vn-client-address-create></vn-client-address-create>');
            controller = $componentController('vnClientAddressCreate', {$element, $scope});
            controller.$.watcher = watcher;
            controller.$.watcher.submit = () => {
                return {
                    then: callback => {
                        callback({data: {id: 124}});
                    }
                };
            };
            controller.client = {id: 101, defaultAddressFk: 121};
        }));

        it('should define and set address property', () => {
            expect(controller.address.isActive).toBe(true);
        });

        describe('onSubmit()', () => {
            it('should perform a PATCH and not set value to defaultAddressFk property', () => {
                jest.spyOn(controller.$state, 'go');
                controller.address.isDefaultAddress = false;
                controller.onSubmit();

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

            it('should perform a PATCH and set a value to defaultAddressFk property', () => {
                jest.spyOn(controller.$state, 'go');
                controller.address.isDefaultAddress = true;
                controller.onSubmit();

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

        describe('town() setter', () => {
            it(`should set provinceId property`, () => {
                controller.town = {
                    provinceFk: 1,
                    code: 46001,
                    province: {
                        id: 1,
                        name: 'New york',
                        country: {
                            id: 2,
                            name: 'USA'
                        }
                    },
                    postcodes: []
                };

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

            it(`should set provinceId property and fill the postalCode if there's just one`, () => {
                controller.town = {
                    provinceFk: 1,
                    code: 46001,
                    province: {
                        id: 1,
                        name: 'New york',
                        country: {
                            id: 2,
                            name: 'USA'
                        }
                    },
                    postcodes: [{code: '46001'}]
                };

                expect(controller.address.provinceId).toEqual(1);
                expect(controller.address.postalCode).toEqual('46001');
            });
        });

        describe('postcode() setter', () => {
            it(`should set the town and province properties`, () => {
                controller.postcode = {
                    townFk: 1,
                    code: 46001,
                    town: {
                        id: 1,
                        name: 'New York',
                        province: {
                            id: 1,
                            name: 'New york',
                            country: {
                                id: 2,
                                name: 'USA'
                            }
                        }
                    }
                };

                expect(controller.address.city).toEqual('New York');
                expect(controller.address.provinceId).toEqual(1);
            });
        });

        describe('onCustomAgentAccept()', () => {
            it(`should 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.customsAgentId).toEqual(1);
            });
        });
    });
});