import './index';

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

    beforeEach(ngModule('invoiceIn'));

    beforeEach(inject(($componentController, _$httpBackend_) => {
        $httpBackend = _$httpBackend_;
        const $element = angular.element('<vn-invoice-in-descriptor></vn-invoice-in-descriptor>');

        controller = $componentController('vnInvoiceInDescriptor', {$element});
        controller.invoiceIn = {id: 1};
    }));

    describe('loadData()', () => {
        it(`should perform a get query to store the invoice in data into the controller`, () => {
            const invoiceIn = {
                id: 1,
                invoiceInDueDay: [
                    {amount: 1},
                    {amount: 2}
                ]
            };
            const expectedAmount = invoiceIn.invoiceInDueDay[0].amount + invoiceIn.invoiceInDueDay[1].amount;

            $httpBackend.when('GET', `InvoiceIns/${controller.invoiceIn.id}`).respond(invoiceIn);
            controller.loadData();
            $httpBackend.flush();

            expect(controller.invoiceIn.id).toEqual(invoiceIn.id);
            expect(controller.invoiceIn.amount).toEqual(expectedAmount);
        });
    });

    describe('onAcceptToBook()', () => {
        it(`should perform a post query to book the invoice`, () => {
            jest.spyOn(controller.vnApp, 'showSuccess');
            controller.$state.reload = jest.fn();

            const id = 1;

            $httpBackend.expectPOST(`InvoiceIns/${id}/toBook`).respond();
            controller.onAcceptToBook();
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('InvoiceIn booked');
        });
    });

    describe('checkToBook()', () => {
        it(`should show a warning before book`, () => {
            controller.$.confirmToBookAnyway = {show: () => {}};
            jest.spyOn(controller.$.confirmToBookAnyway, 'show');

            const invoceInId = 1;
            const data = {
                totalDueDay: 'an amount',
                totalTaxableBase: 'distinct amount'
            };

            $httpBackend.expectGET(`InvoiceIns/${invoceInId}/getTotals`).respond(data);
            $httpBackend.expectGET(`InvoiceInDueDays/count`).respond();

            controller.checkToBook();
            $httpBackend.flush();

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

        it(`should call onAcceptToBook`, () => {
            controller.onAcceptToBook = jest.fn();

            const invoceInId = 1;
            const data = {
                totalDueDay: 'same amount',
                totalTaxableBase: 'same amount'
            };

            $httpBackend.expectGET(`InvoiceIns/${invoceInId}/getTotals`).respond(data);
            $httpBackend.expectGET(`InvoiceInDueDays/count`).respond();

            controller.checkToBook();
            $httpBackend.flush();

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