import './index';

describe('vnInvoiceOutDescriptorMenu', () => {
    let controller;
    let $httpBackend;
    let $httpParamSerializer;
    const invoiceOut = {
        id: 1,
        client: {id: 1101},
        ref: 'T1111111'
    };

    beforeEach(ngModule('invoiceOut'));

    beforeEach(inject(($componentController, _$httpParamSerializer_, _$httpBackend_) => {
        $httpBackend = _$httpBackend_;
        $httpParamSerializer = _$httpParamSerializer_;
        controller = $componentController('vnInvoiceOutDescriptorMenu', {$element: null});
        controller.invoiceOut = {
            id: 1,
            ref: 'T1111111',
            client: {id: 1101}
        };
    }));

    describe('createPdfInvoice()', () => {
        it('should make a query to the createPdf() endpoint and show a success snackbar', () => {
            jest.spyOn(controller.vnApp, 'showSuccess');

            $httpBackend.whenGET(`InvoiceOuts/${invoiceOut.id}`).respond();
            $httpBackend.expectPOST(`InvoiceOuts/${invoiceOut.id}/createPdf`).respond();
            controller.createPdfInvoice();
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
        });
    });

    describe('showCsvInvoice()', () => {
        it('should make a query to the csv invoice download endpoint and show a message snackbar', () => {
            jest.spyOn(window, 'open').mockReturnThis();

            const expectedParams = {
                recipientId: invoiceOut.client.id
            };
            const serializedParams = $httpParamSerializer(expectedParams);
            const expectedPath = `api/InvoiceOuts/${invoiceOut.ref}/invoice-csv?${serializedParams}`;
            controller.showCsvInvoice();

            expect(window.open).toHaveBeenCalledWith(expectedPath);
        });
    });

    describe('deleteInvoiceOut()', () => {
        it(`should make a query and call showSuccess()`, () => {
            controller.$state.reload = jest.fn();
            jest.spyOn(controller.vnApp, 'showSuccess');

            $httpBackend.expectPOST(`InvoiceOuts/${invoiceOut.id}/delete`).respond();
            controller.deleteInvoiceOut();
            $httpBackend.flush();

            expect(controller.$state.reload).toHaveBeenCalled();
            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
        });

        it(`should make a query and call showSuccess() after state.go if the state wasn't in invoiceOut module`, () => {
            jest.spyOn(controller.$state, 'go').mockReturnValue('ok');
            jest.spyOn(controller.vnApp, 'showSuccess');
            controller.$state.current.name = 'invoiceOut.card.something';

            $httpBackend.expectPOST(`InvoiceOuts/${invoiceOut.id}/delete`).respond();
            controller.deleteInvoiceOut();
            $httpBackend.flush();

            expect(controller.$state.go).toHaveBeenCalledWith('invoiceOut.index');
            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
        });
    });

    describe('sendPdfInvoice()', () => {
        it('should make a query to the email invoice endpoint and show a message snackbar', () => {
            jest.spyOn(controller.vnApp, 'showMessage');

            const $data = {email: 'brucebanner@gothamcity.com'};

            $httpBackend.expectPOST(`InvoiceOuts/${invoiceOut.ref}/invoice-email`).respond();
            controller.sendPdfInvoice($data);
            $httpBackend.flush();

            expect(controller.vnApp.showMessage).toHaveBeenCalled();
        });
    });

    describe('sendCsvInvoice()', () => {
        it('should make a query to the csv invoice send endpoint and show a message snackbar', () => {
            jest.spyOn(controller.vnApp, 'showMessage');

            const $data = {email: 'brucebanner@gothamcity.com'};

            $httpBackend.expectPOST(`InvoiceOuts/${invoiceOut.ref}/invoice-csv-email`).respond();
            controller.sendCsvInvoice($data);
            $httpBackend.flush();

            expect(controller.vnApp.showMessage).toHaveBeenCalled();
        });
    });

    describe('refundInvoiceOut()', () => {
        it('should make a query and show a success message', () => {
            jest.spyOn(controller.vnApp, 'showSuccess');
            const params = {ref: controller.invoiceOut.ref};

            $httpBackend.expectPOST(`InvoiceOuts/refund`, params).respond([{id: 1}, {id: 2}]);
            controller.refundInvoiceOut();
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalled();
        });
    });
});