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

describe('Travel Component vnTravelCreate', () => {
    let $scope;
    let $state;
    let controller;
    let $httpBackend;

    beforeEach(ngModule('travel'));

    beforeEach(inject(($componentController, $rootScope, _$state_, _$httpBackend_) => {
        $httpBackend = _$httpBackend_;
        $scope = $rootScope.$new();
        $state = _$state_;
        $scope.watcher = watcher;
        const $element = angular.element('<vn-travel-create></vn-travel-create>');
        controller = $componentController('vnTravelCreate', {$element, $scope});
    }));

    describe('onSubmit()', () => {
        it(`should call submit() on the watcher then expect a callback`, () => {
            jest.spyOn($state, 'go');

            controller.onSubmit();

            expect(controller.$state.go).toHaveBeenCalledWith('travel.card.basicData', {id: 1234});
        });
    });

    describe('$onChanges()', () => {
        it('should update the travel data when $params.q is defined', () => {
            controller.$params = {q: '{"ref": 1,"agencyModeFk": 1}'};

            const params = {q: '{"ref": 1, "agencyModeFk": 1}'};
            const json = JSON.parse(params.q);

            controller.$onChanges();

            expect(controller.travel).toEqual(json);
        });
    });

    describe('onShippedChange()', () => {
        it(`should do nothing if there's no agencyModeFk in the travel.`, () => {
            controller.travel = {};
            controller.onShippedChange();

            expect(controller.travel.landed).toBeUndefined();
            expect(controller.travel.warehouseInFk).toBeUndefined();
            expect(controller.travel.warehouseOutFk).toBeUndefined();
        });

        it(`should do nothing if there's no response data.`, () => {
            controller.travel = {agencyModeFk: 4};
            const tomorrow = Date.vnNew();

            const query = `travels/getAverageDays?agencyModeFk=${controller.travel.agencyModeFk}`;
            $httpBackend.expectGET(query).respond(undefined);
            controller.onShippedChange(tomorrow);
            $httpBackend.flush();

            expect(controller.travel.warehouseInFk).toBeUndefined();
            expect(controller.travel.warehouseOutFk).toBeUndefined();
            expect(controller.travel.dayDuration).toBeUndefined();
        });

        it(`should fill the fields when it's selected a date and agency.`, () => {
            controller.travel = {agencyModeFk: 1};
            const tomorrow = Date.vnNew();
            tomorrow.setDate(tomorrow.getDate() + 9);
            const expectedResponse = {
                id: 8,
                dayDuration: 9,
                warehouseInFk: 5,
                warehouseOutFk: 1
            };

            const query = `travels/getAverageDays?agencyModeFk=${controller.travel.agencyModeFk}`;
            $httpBackend.expectGET(query).respond(expectedResponse);
            controller.onShippedChange(tomorrow);
            $httpBackend.flush();

            expect(controller.travel.warehouseInFk).toEqual(expectedResponse.warehouseInFk);
            expect(controller.travel.warehouseOutFk).toEqual(expectedResponse.warehouseOutFk);
        });
    });
});