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

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

    beforeEach(ngModule('ticket')
    );

    beforeEach(inject(($componentController, _$httpBackend_) => {
        $httpBackend = _$httpBackend_;
        const $element = angular.element('<vn-ticket-advance></vn-ticket-advance>');
        controller = $componentController('vnTicketAdvance', {$element});
        controller.$.model = crudModel;
        controller.$.model.data = [{
            id: 1,
            checked: true,
            state: 'OK'
        }, {
            id: 2,
            checked: true,
            state: 'Libre'
        }];
    }));

    describe('compareDate()', () => {
        it('should return warning when the date is the present', () => {
            let today = new Date();
            let result = controller.compareDate(today);

            expect(result).toEqual('warning');
        });

        it('should return sucess when the date is in the future', () => {
            let futureDate = new Date();
            futureDate = futureDate.setDate(futureDate.getDate() + 10);
            let result = controller.compareDate(futureDate);

            expect(result).toEqual('success');
        });

        it('should return undefined when the date is in the past', () => {
            let pastDate = new Date();
            pastDate = pastDate.setDate(pastDate.getDate() - 10);
            let result = controller.compareDate(pastDate);

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

    describe('checked()', () => {
        it('should return an array of checked tickets', () => {
            const result = controller.checked;
            const firstRow = result[0];
            const secondRow = result[1];

            expect(result.length).toEqual(2);
            expect(firstRow.id).toEqual(1);
            expect(secondRow.id).toEqual(2);
        });
    });

    describe('stateColor()', () => {
        it('should return success to the OK tickets', () => {
            const ok = controller.stateColor(controller.$.model.data[0].state);
            const notOk = controller.stateColor(controller.$.model.data[1].state);

            expect(ok).toEqual('success');
            expect(notOk).not.toEqual('success');
        });

        it('should return success to the FREE tickets', () => {
            const notFree = controller.stateColor(controller.$.model.data[0].state);
            const free = controller.stateColor(controller.$.model.data[1].state);

            expect(free).toEqual('notice');
            expect(notFree).not.toEqual('notice');
        });
    });

    describe('dateRange()', () => {
        it('should return two dates with the hours at the start and end of the given date', () => {
            const now = new Date();

            const today = now.getDate();

            const dateRange = controller.dateRange(now);
            const start = dateRange[0].toString();
            const end = dateRange[1].toString();

            expect(start).toContain(today);
            expect(start).toContain('00:00:00');

            expect(end).toContain(today);
            expect(end).toContain('23:59:59');
        });
    });

    describe('moveTicketsAdvance()', () => {
        it('should make an HTTP Post query', () => {
            jest.spyOn(controller.$.model, 'refresh');
            jest.spyOn(controller.vnApp, 'showSuccess');

            $httpBackend.expectPOST(`Tickets/merge`).respond();
            controller.moveTicketsAdvance();
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            expect(controller.$.model.refresh).toHaveBeenCalledWith();
        });
    });
});