import './index';

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

        beforeEach(ngModule('ticket'));

        beforeEach(inject(($componentController, _$state_, _$httpBackend_, $rootScope) => {
            $httpBackend = _$httpBackend_;
            $scope = $rootScope.$new();
            $scope.model = {data: [{id: 1}, {id: 2}], accept: () => {
                return {
                    then: () => {}
                };
            }};
            $state = _$state_;
            $state.params.id = 101;
            const $element = angular.element('<vn-ticket-volume></vn-ticket-volume>');
            controller = $componentController('vnTicketVolume', {$element, $scope});
        }));

        describe('sales() setter', () => {
            it('should set sales property on controller an then call applyVolumes() method', () => {
                jest.spyOn(controller, 'applyVolumes');

                controller.sales = [{id: 1}];

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

        describe('volumes() setter', () => {
            it('should set volumes property on controller an then call applyVolumes() method', () => {
                jest.spyOn(controller, 'applyVolumes');

                controller.volumes = [{id: 1}];

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

        describe('applyVolumes()', () => {
            it(`should not apply volumes to the sales if sales property is not defined on controller`, () => {
                controller.sales = [{id: 1, name: 'Sale one'}, {id: 2, name: 'Sale two'}];

                expect(controller.sales[0].volume).toBeUndefined();
                expect(controller.sales[1].volume).toBeUndefined();
            });

            it(`should not apply volumes to the sales if sales property is not defined on controller`, () => {
                controller.volumes = [{saleFk: 1, m3: 0.012}, {saleFk: 2, m3: 0.015}];

                expect(controller.sales).toBeUndefined();
            });

            it(`should apply volumes to the sales if sales and volumes properties are defined on controller`, () => {
                controller.sales = [{id: 1, name: 'Sale one'}, {id: 2, name: 'Sale two'}];
                controller.volumes = [{saleFk: 1, volume: 0.012}, {saleFk: 2, volume: 0.015}];

                expect(controller.sales[0].saleVolume.volume).toEqual(0.012);
                expect(controller.sales[1].saleVolume.volume).toEqual(0.015);
            });
        });

        /*
        it('should join the sale volumes to its respective sale', () => {
            controller.ticket = {id: 1};
            let response = {volumes: [
                {saleFk: 1, m3: 0.008},
                {saleFk: 2, m3: 0.003}
            ]};

            $httpBackend.expectGET(`tickets/1/getVolume`).respond(response);
            controller.onDataChange();
            $httpBackend.flush();

            expect($scope.model.data[0].volume.m3).toBe(0.008);
            expect($scope.model.data[1].volume.m3).toBe(0.003);
        });
        */
    });
});