import './index.js';
import crudModel from 'core/mocks/crud-model';

describe('ticket', () => {
    describe('Component vnTicketComponents', () => {
        let controller;
        let $httpBackend;

        beforeEach(ngModule('ticket'));

        beforeEach(inject(($componentController, $rootScope, $state, _$httpBackend_) => {
            $state.params.id = '1';
            let $scope = $rootScope.$new();
            $httpBackend = _$httpBackend_;
            $scope.model = crudModel;
            $scope.model.data = [{
                quantity: 1,
                components: [
                    {
                        value: 5,
                        component: {
                            componentType: {
                                isBase: 1
                            }
                        }
                    },
                    {
                        value: 5,
                        component: {
                            componentType: {
                                isBase: 0
                            }
                        }
                    },
                    {
                        value: 5,
                        component: {
                            componentType: {
                                isBase: 0
                            }
                        }
                    }
                ]
            },
            {
                quantity: 5,
                components: [
                    {
                        value: 1,
                        component: {
                            componentType: {
                                isBase: 1
                            }
                        }
                    },
                    {
                        value: 1,
                        component: {
                            componentType: {
                                isBase: 0
                            }
                        }
                    },
                    {
                        value: 1,
                        component: {
                            componentType: {
                                isBase: 0
                            }
                        }
                    },
                ]
            }];
            const $element = angular.element('<vn-ticket-components></vn-ticket-components>');
            controller = $componentController('vnTicketComponents', {$element, $scope});
        }));

        describe('base()', () => {
            it(`should return the sum from all componenets base in each sale`, () => {
                let result = controller.base();

                expect(result).toEqual(10);
            });
        });

        describe('ticket setter', () => {
            it('should set the ticket data and then call getTheoricalCost() and getComponentsSum()', () => {
                jest.spyOn(controller, 'getTheoricalCost');
                jest.spyOn(controller, 'getComponentsSum');
                controller._ticket = undefined;
                controller.ticket = {
                    id: 7,
                    zone: {
                        isVolumetric: false
                    }
                };

                expect(controller.ticket).toBeDefined();
                expect(controller.getTheoricalCost).toHaveBeenCalledWith();
                expect(controller.getComponentsSum).toHaveBeenCalledWith();
            });
        });

        describe('getTotal()', () => {
            it('should return the total sum of a ticket', () => {
                let result = controller.getTotal();

                expect(result).toEqual(30);
            });
        });

        describe('getTheoricalCost()', () => {
            it('should make a request to get the theorical cost of a ticket', () => {
                controller._ticket = {
                    id: 7
                };
                $httpBackend.expect('GET', `Tickets/${controller._ticket.id}/freightCost`).respond('My freight cost');
                controller.getTheoricalCost();
                $httpBackend.flush();

                expect(controller.theoricalCost).toBe('My freight cost');
            });
        });

        describe('getComponentsSum()', () => {
            it('should make a request to get the component list', () => {
                controller._ticket = {
                    id: 7
                };

                $httpBackend.expect('GET', `Tickets/${controller._ticket.id}/getComponentsSum`).respond('My component list');
                controller.getComponentsSum();
                $httpBackend.flush();

                expect(controller.componentsList).toBe('My component list');
            });
        });
    });
});