import './index.js';
describe('Component vnTicketIndex', () => {
    let controller;
    let $httpBackend;
    let $window;
    let tickets = [{
        id: 1,
        clientFk: 1,
        checked: false,
        totalWithVat: 10.5
    }, {
        id: 2,
        clientFk: 1,
        checked: true,
        totalWithVat: 20.5
    }, {
        id: 3,
        clientFk: 1,
        checked: true,
        totalWithVat: 30
    }];

    beforeEach(ngModule('ticket'));

    beforeEach(inject(($componentController, _$window_, _$httpBackend_) => {
        $httpBackend = _$httpBackend_;
        $window = _$window_;
        const $element = angular.element('<vn-ticket-index></vn-ticket-index>');
        controller = $componentController('vnTicketIndex', {$element});
    }));

    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('preview()', () => {
        it('should show the dialog summary', () => {
            controller.$.summary = {show: () => {}};
            jest.spyOn(controller.$.summary, 'show');

            let event = new MouseEvent('click', {
                view: $window,
                bubbles: true,
                cancelable: true
            });
            controller.preview(event, tickets[0]);

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

    describe('openBalanceDialog()', () => {
        it('should fill the object for the component balanceCreateDialog', () => {
            controller.$.balanceCreateDialog = {show: () => {}};
            jest.spyOn(controller.$.balanceCreateDialog, 'show').mockReturnThis();

            controller.$.model = {data: tickets};
            controller.openBalanceDialog();

            let description = controller.$.balanceCreateDialog.description;
            let amountPaid = controller.$.balanceCreateDialog.amountPaid;

            expect(description).toEqual('Albaran: 2, 3');
            expect(amountPaid).toEqual(50.5);
        });
    });

    describe('setDelivered()/openDeliveryNotes()', () => {
        it('should perform a post to setDelivered and open tabs with the delivery notes', () => {
            controller.$.model = {data: tickets, refresh: () => {}};

            $window.open = jest.fn();

            $httpBackend.expect('POST', 'TicketTrackings/setDelivered').respond('ok');
            controller.setDelivered();
            $httpBackend.flush();

            expect($window.open).toHaveBeenCalledWith(`api/Tickets/${tickets[1].id}/delivery-note-pdf`);
            expect($window.open).toHaveBeenCalledWith(`api/Tickets/${tickets[2].id}/delivery-note-pdf`);
        });
    });

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

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

    describe('totalChecked()', () => {
        it('should return the total number of checked tickets', () => {
            controller.$.model = {data: tickets};
            const result = controller.checked;

            expect(result.length).toEqual(2);
        });
    });
});