import './index';

describe('component vnTravelSummary', () => {
    let controller;
    let $httpBackend;
    let $scope;
    let $httpParamSerializer;

    beforeEach(angular.mock.module('travel', $translateProvider => {
        $translateProvider.translations('en', {});
    }));

    beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
        $httpBackend = _$httpBackend_;
        $httpParamSerializer = _$httpParamSerializer_;
        $scope = $rootScope.$new();
        const $element = angular.element(`<vn-travel-summary></vn-travel-summary>`);
        controller = $componentController('vnTravelSummary', {$element, $scope});
    }));

    describe('travel setter/getter', () => {
        it('should return the travel and then call both getTravel() and getEntries()', () => {
            jest.spyOn(controller, 'getTravel');
            jest.spyOn(controller, 'getEntries');
            jest.spyOn(controller, 'getThermographs');
            controller.travel = {id: 99};

            expect(controller._travel.id).toEqual(99);
            expect(controller.getTravel).toHaveBeenCalledWith();
            expect(controller.getEntries).toHaveBeenCalledWith();
            expect(controller.getThermographs).toHaveBeenCalledWith();
        });
    });

    describe('getTravel()', () => {
        it('should perform a get and then store data on the controller', () => {
            controller._travel = {id: 999};

            const query = `Travels/${controller._travel.id}/getTravel`;
            $httpBackend.expectGET(query).respond('I am the travelData');
            controller.getTravel();
            $httpBackend.flush();

            expect(controller.travelData).toEqual('I am the travelData');
        });
    });

    describe('getEntries()', () => {
        it('should call the getEntries method to get the entries data', () => {
            controller._travel = {id: 999};

            const query = `Travels/${controller._travel.id}/getEntries`;
            $httpBackend.expectGET(query).respond('I am the entries');
            controller.getEntries();
            $httpBackend.flush();

            expect(controller.entries).toEqual('I am the entries');
        });
    });

    describe('getThermographs()', () => {
        it('should call the getThermographs method to get the thermographs', () => {
            controller._travel = {id: 2};

            $httpBackend.expectGET(`TravelThermographs`).respond('I am the thermographs');
            controller.getThermographs();
            $httpBackend.flush();

            expect(controller.travelThermographs).toEqual('I am the thermographs');
        });
    });

    describe('total()', () => {
        it('should calculate the total amount of a given property for every row', () => {
            controller.entries = [
                {id: 1, freightValue: 1, packageValue: 2, cc: 0.01},
                {id: 2, freightValue: 1, packageValue: 2, cc: 0.01},
                {id: 3, freightValue: 1, packageValue: 2, cc: 0.01}
            ];

            expect(controller.total('freightValue')).toEqual(3);
            expect(controller.total('packageValue')).toEqual(6);
            expect(controller.total('cc')).toEqual(0.03);
        });
    });
});