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

describe('claim', () => {
    describe('Component vnClaimAction', () => {
        let controller;
        let $httpBackend;
        let $state;

        beforeEach(ngModule('claim'));

        beforeEach(inject(($componentController, _$state_, _$httpBackend_) => {
            $httpBackend = _$httpBackend_;
            $state = _$state_;
            $state.params.id = 1;

            controller = $componentController('vnClaimAction', {$element: null});
            controller.claim = {ticketFk: 1};
            controller.$.model = {refresh: () => {}};
            controller.$.addSales = {
                hide: () => {},
                show: () => {}
            };
            controller.$.lastTicketsModel = crudModel;
            controller.$.lastTicketsPopover = {
                hide: () => {},
                show: () => {}
            };
            controller.card = {reload: () => {}};
            $httpBackend.expectGET(`ClaimStates/findOne`).respond({});
        }));

        describe('getResolvedState()', () => {
            it('should return the resolved state id', () => {
                $httpBackend.expectGET(`ClaimStates/findOne`).respond({id: 1});
                controller.getResolvedState();
                $httpBackend.flush();

                expect(controller.resolvedStateId).toEqual(1);
            });
        });

        describe('calculateTotals()', () => {
            it('should calculate the total price of the items claimed', () => {
                controller.salesClaimed = [
                    {quantity: 5, price: 2, discount: 0},
                    {quantity: 10, price: 2, discount: 0},
                    {quantity: 10, price: 2, discount: 0}
                ];
                controller.calculateTotals();

                expect(controller.claimedTotal).toEqual(50);
            });
        });

        describe('importToNewRefundTicket()', () => {
            it('should perform a post query and add lines from a new ticket', () => {
                jest.spyOn(controller.$.model, 'refresh');
                jest.spyOn(controller.vnApp, 'showSuccess');

                $httpBackend.expect('POST', `ClaimBeginnings/1/importToNewRefundTicket`).respond({});
                controller.importToNewRefundTicket();
                $httpBackend.flush();

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

        describe('regularize()', () => {
            it('should perform a post query and reload the claim card', () => {
                jest.spyOn(controller.card, 'reload');
                jest.spyOn(controller.vnApp, 'showSuccess');

                $httpBackend.expect('POST', `Claims/1/regularizeClaim`).respond({});
                controller.regularize();
                $httpBackend.flush();

                expect(controller.card.reload).toHaveBeenCalledWith();
                expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            });
        });

        describe('save()', () => {
            it('should perform a patch query and show a success message', () => {
                jest.spyOn(controller.vnApp, 'showSuccess');

                const data = {hasToPickUp: true};
                $httpBackend.expect('PATCH', `Claims/1/updateClaimAction`, data).respond({});
                controller.save(data);
                $httpBackend.flush();

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

        describe('onUpdateGreugeAccept()', () => {
            const greugeTypeId = 7;
            const freightPickUpPrice = 11;

            it('should make a query and get the greugeTypeId and greuge config', () => {
                $httpBackend.expectRoute('GET', `GreugeTypes/findOne`).respond({id: greugeTypeId});
                $httpBackend.expectGET(`GreugeConfigs/findOne`).respond({freightPickUpPrice});
                controller.onUpdateGreugeAccept();
                $httpBackend.flush();

                expect(controller.greugeTypeFreightId).toEqual(greugeTypeId);
                expect(controller.freightPickUpPrice).toEqual(freightPickUpPrice);
            });

            it('should perform a insert into greuges', done => {
                jest.spyOn(controller, 'getGreugeTypeId').mockReturnValue(new Promise(resolve => {
                    return resolve({id: greugeTypeId});
                }));
                jest.spyOn(controller, 'getGreugeConfig').mockReturnValue(new Promise(resolve => {
                    return resolve({freightPickUpPrice});
                }));
                jest.spyOn(controller, 'updateGreuge').mockReturnValue(new Promise(resolve => {
                    return resolve(true);
                }));

                controller.claim.clientFk = 1101;
                controller.claim.id = 11;

                controller.onUpdateGreugeAccept().then(() => {
                    expect(controller.updateGreuge).toHaveBeenCalledWith(jasmine.any(Object));
                    done();
                }).catch(done.fail);
            });
        });

        describe('updateGreuge()', () => {
            it('should make a query and then call to showSuccess() and showMessage() methods', () => {
                jest.spyOn(controller.vnApp, 'showSuccess');
                jest.spyOn(controller.vnApp, 'showMessage');

                const freightPickUpPrice = 11;
                const greugeTypeId = 7;
                const expectedData = {
                    clientFk: 1101,
                    description: `claim: ${controller.claim.id}`,
                    amount: freightPickUpPrice,
                    greugeTypeFk: greugeTypeId,
                    ticketFk: controller.claim.ticketFk
                };
                $httpBackend.expect('POST', `Greuges`, expectedData).respond(200);
                controller.updateGreuge(expectedData);
                $httpBackend.flush();

                expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
                expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Greuge added');
            });
        });

        describe('onResponse()', () => {
            it('should perform a post query and show a success message', () => {
                jest.spyOn(controller.vnApp, 'showSuccess');

                $httpBackend.expect('POST', `Claims/updateClaimDestination`).respond({});
                controller.onResponse();
                $httpBackend.flush();

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