import './index.js';

describe('Zone warehouses', () => {
    let $httpBackend;
    let $httpParamSerializer;
    let controller;
    let $element;

    beforeEach(ngModule('zone'));

    beforeEach(inject(($componentController, _$httpBackend_, _$httpParamSerializer_) => {
        $httpBackend = _$httpBackend_;
        $httpParamSerializer = _$httpParamSerializer_;
        $element = angular.element('<vn-zone-warehouses></vn-zone-warehouses');
        controller = $componentController('vnZoneWarehouses', {$element});
        controller.zone = {id: 1};
        controller.$params = {id: 1};
        controller.$.dialog = {hide: jest.fn()};
    }));

    describe('refresh()', () => {
        it('should make an HTTP GET query and then set the data', () => {
            const params = {filter: {include: 'warehouse'}};
            const serializedParams = $httpParamSerializer(params);
            const path = `Zones/1/warehouses?${serializedParams}`;
            $httpBackend.expect('GET', path).respond([{id: 1, name: 'Warehouse one'}]);
            controller.refresh();
            $httpBackend.flush();

            expect(controller.$.data).toBeDefined();
        });
    });

    describe('onSave()', () => {
        it('should make an HTTP POST query and then call the refresh() method', () => {
            jest.spyOn(controller, 'refresh').mockReturnThis();

            $httpBackend.expect('POST', `Zones/1/warehouses`).respond(200);
            controller.onSave();
            $httpBackend.flush();

            expect(controller.selected).toBeNull();
            expect(controller.isNew).toBeNull();
            expect(controller.$.dialog.hide).toHaveBeenCalledWith();
            expect(controller.refresh).toHaveBeenCalledWith();
        });
    });

    describe('delete()', () => {
        it('should make an HTTP DELETE query and then set deleteRow property to null value', () => {
            controller.deleteRow = {id: 1};
            controller.$.data = [{id: 1}];
            $httpBackend.expect('DELETE', `Zones/1/warehouses/1`).respond(200);
            controller.delete();
            $httpBackend.flush();

            expect(controller.deleteRow).toBeNull();
        });
    });
});