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 = 1101;
            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('applyVolumes()', () => {
            const ticket = 1;
            const response =
            {
                saleVolume: [
                    {saleFk: 1, volume: 0.012},
                    {saleFk: 2, volume: 0.015}
                ],
                packingTypeVolume: [
                    {code: 'V', volume: 1},
                    {code: 'H', volume: 2}
                ]
            };

            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`, () => {
                const expectedResultOne = response.saleVolume[0].volume;
                const expectedResultTwo = response.saleVolume[1].volume;
                $httpBackend.expectGET(`Tickets/${ticket}/getVolume`).respond(response);
                controller.sales = [
                    {id: 1, name: 'Sale one', ticketFk: ticket},
                    {id: 2, name: 'Sale two'}
                ];
                $httpBackend.flush();

                expect(controller.sales[0].saleVolume.volume).toEqual(expectedResultOne);
                expect(controller.sales[1].saleVolume.volume).toEqual(expectedResultTwo);
            });

            it(`should apply packing volumes to the sales if sales and volumes properties are defined on controller`, () => {
                const expectedResultOne = response.packingTypeVolume[0].code;
                const expectedResultTwo = response.packingTypeVolume[1].code;
                $httpBackend.expectGET(`Tickets/${ticket}/getVolume`).respond(response);
                controller.sales = [
                    {id: 1, name: 'Sale one', ticketFk: ticket},
                    {id: 2, name: 'Sale two'}
                ];
                $httpBackend.flush();

                expect(controller.packingTypeVolume[0].code).toEqual(expectedResultOne);
                expect(controller.packingTypeVolume[1].code).toEqual(expectedResultTwo);
            });
        });
    });
});