import './index';

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

    beforeEach(ngModule('zone'));

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

    describe('zone setter', () => {
        it('should set the zone and then call both getSummary() and getWarehouses()', () => {
            jest.spyOn(controller, 'getSummary');
            jest.spyOn(controller, 'getWarehouses');

            controller.zone = {id: 1};

            expect(controller.getSummary).toHaveBeenCalledWith();
            expect(controller.getWarehouses).toHaveBeenCalledWith();
        });
    });

    describe('getSummary()', () => {
        it('should perform a get and then store data on the controller', () => {
            controller._zone = {id: 1};
            let params = {
                filter: {
                    include: {
                        relation: 'agencyMode',
                        fields: ['name']
                    },
                    where: {
                        id: controller._zone.id
                    }
                }
            };
            const serializedParams = $httpParamSerializer(params);
            const query = `Zones/findOne?${serializedParams}`;
            $httpBackend.expectGET(query).respond({id: 1});
            controller.getSummary();
            $httpBackend.flush();

            expect(controller.summary).toBeDefined();
        });
    });

    describe('getWarehouses()', () => {
        it('should make an HTTP get query and then store data on the controller', () => {
            controller._zone = {id: 1};
            const params = {
                filter: {
                    include: {
                        relation: 'warehouse',
                        fields: ['name']
                    }
                }
            };

            const serializedParams = $httpParamSerializer(params);
            const query = `Zones/1/warehouses?${serializedParams}`;
            $httpBackend.expect('GET', query).respond([{id: 1}]);
            controller.getWarehouses();
            $httpBackend.flush();

            expect(controller.zoneWarehouses.length).toEqual(1);
        });
    });
});