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

describe('Supplier', () => {
    describe('Component vnSupplierAddressCreate', () => {
        let $scope;
        let controller;
        let $element;
        let $state;

        beforeEach(ngModule('supplier'));

        beforeEach(inject(($componentController, $rootScope, _$state_) => {
            $scope = $rootScope.$new();
            $state = _$state_;
            $state.params.id = '1234';
            $element = angular.element('<vn-supplier-address-create></vn-supplier-address-create>');
            controller = $componentController('vnSupplierAddressCreate', {$element, $scope});
            controller.$.watcher = watcher;
            controller.$.watcher.submit = () => {
                return {
                    then: callback => {
                        callback({data: {id: 124}});
                    }
                };
            };
            controller.supplier = {id: 1};
        }));

        describe('onSubmit()', () => {
            it('should perform a PATCH and then redirect to the main section', () => {
                jest.spyOn(controller.$state, 'go');
                controller.onSubmit();

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

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

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

            it(`should set provinceFk 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.provinceFk).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.provinceFk).toEqual(1);
            });
        });
    });
});