import './index';

describe('Client', () => {
    describe('Component vnClientCreditCreate', () => {
        let controller;
        let $httpBackend;
        let $state;
        let $scope;
        let client;

        beforeEach(ngModule('client'));

        beforeEach(inject(($componentController, _$httpBackend_, $rootScope, _$state_) => {
            $scope = $rootScope.$new();
            $scope.confirmation = {show: () => {
                return {
                    then: () => {}
                };
            }};
            $scope.watcher = {
                submit: () => {
                    return {
                        then: callback => {
                            callback();
                        }
                    };
                }
            };
            client = {credit: 0};
            $state = _$state_;
            $state.params.id = 1101;
            $httpBackend = _$httpBackend_;
            const $element = angular.element('<vn-client-credit-create></vn-client-credit-create>');
            controller = $componentController('vnClientCreditCreate', {$element, $scope});
        }));

        describe('onSubmit()', () => {
            it('should perform a query to check (GET) if the client has an active recovery', () => {
                $httpBackend.whenGET(`Recoveries/1101/hasActiveRecovery`).respond(true);
                $httpBackend.expectGET(`Recoveries/1101/hasActiveRecovery`);
                controller.onSubmit();
                $httpBackend.flush();
            });

            it('should call show() method when the client have a recovery', () => {
                jest.spyOn(controller.$.confirmation, 'show');
                $httpBackend.whenGET(`Recoveries/1101/hasActiveRecovery`).respond(true);
                $httpBackend.expectGET(`Recoveries/1101/hasActiveRecovery`);
                controller.onSubmit();
                $httpBackend.flush();

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

            it('should call addCredit() method when the client doesnt have a recovery', () => {
                jest.spyOn(controller, 'addCredit');
                $httpBackend.whenGET(`Recoveries/1101/hasActiveRecovery`).respond(false);
                $httpBackend.expectGET(`Recoveries/1101/hasActiveRecovery`);
                controller.onSubmit();
                $httpBackend.flush();

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

        describe('cancel()', () => {
            it('should call goToIndex()', () => {
                jest.spyOn(controller, 'goToIndex');
                controller.cancel();

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

        describe('returnDialog()', () => {
            it('should call addCredit() when is called with accept', () => {
                jest.spyOn(controller, 'addCredit');
                controller.returnDialog();

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

        describe('addCredit()', () => {
            it('should call the function go() on $state to go to the credit list', () => {
                jest.spyOn($state, 'go');
                client.credit = 1;
                controller.addCredit();

                expect(controller.$state.go).toHaveBeenCalledWith('client.card.credit.index');
            });
        });
    });
});