import './index.js';

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

    beforeEach(ngModule('zone'));

    beforeEach(inject(($componentController, _$httpBackend_) => {
        $httpBackend = _$httpBackend_;
        $element = angular.element('<vn-zone-descriptor></vn-zone-descriptor');
        controller = $componentController('vnZoneDescriptor', {$element});
        controller.zone = {id: 1};
        controller.id = 1;
        controller.$.deleteZone = {
            hide: () => {},
            show: () => {}
        };
    }));

    describe('onDelete()', () => {
        it('should make an HTTP POST query and then call the deleteZone show() method', () => {
            jest.spyOn(controller.$.deleteZone, 'show');

            const expectedData = [{id: 16}];
            $httpBackend.when('GET', 'Tickets').respond(expectedData);
            controller.onDelete();
            $httpBackend.flush();

            expect(controller.$.deleteZone.show).toHaveBeenCalledWith();
        });

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

            const expectedData = [];
            $httpBackend.when('GET', 'Tickets').respond(expectedData);
            controller.onDelete();
            $httpBackend.flush();

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

    describe('deleteZone()', () => {
        it('should make an HTTP POST query and then call the showMessage() method', () => {
            jest.spyOn(controller.$state, 'go').mockReturnThis();
            jest.spyOn(controller.vnApp, 'showSuccess');

            const stateName = 'zone.index';
            $httpBackend.when('POST', 'Zones/1/deleteZone').respond(200);
            controller.deleteZone();
            $httpBackend.flush();

            expect(controller.$state.go).toHaveBeenCalledWith(stateName);
            expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Zone deleted');
        });
    });

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

            const stateName = 'zone.card.basicData';
            const expectedData = {id: 1};
            $httpBackend.when('POST', 'Zones/1/clone').respond(expectedData);
            controller.onCloneAccept();
            $httpBackend.flush();

            expect(controller.$state.go).toHaveBeenCalledWith(stateName, expectedData);
        });
    });
});