import './index';

describe('Client', () => {
    describe('Component vnClientCreditInsuranceIndex', () => {
        let controller;
        let $httpBackend;

        beforeEach(ngModule('client'));

        beforeEach(angular.mock.inject(($componentController, _$httpBackend_) => {
            $httpBackend = _$httpBackend_;
            controller = $componentController('vnClientCreditInsuranceIndex');
            controller.client = {id: 101};
        }));

        describe('_getClassifications()', () => {
            it('should perform a GET query to define the classifications property in the controller', () => {
                let res = ['some classifications'];
                let query = '/client/api/CreditClassifications?filter=%7B%22order%22%3A%22finished%20ASC%2C%20started%20DESC%22%2C%22include%22%3A%5B%7B%22relation%22%3A%22insurances%22%2C%22scope%22%3A%7B%22fields%22%3A%5B%22id%22%2C%22credit%22%2C%22created%22%2C%22grade%22%5D%2C%22order%22%3A%22created%20DESC%22%2C%22limit%22%3A2%7D%7D%5D%2C%22where%22%3A%7B%7D%7D';

                $httpBackend.whenGET(query).respond(res);
                $httpBackend.expectGET(query);
                controller._getClassifications();
                $httpBackend.flush();

                expect(controller.classifications).toEqual(['some classifications']);
            });
        });

        describe('canCreateNew()', () => {
            it(`should return false if doesn't have classifications`, () => {
                let result = controller.canCreateNew();

                expect(result).toBeFalsy();
            });

            it(`should return false if finds a classification without due date`, () => {
                controller.classifications = [
                    {finished: Date.now()},
                    {finished: null}
                ];

                let result = controller.canCreateNew();

                expect(result).toBeFalsy();
            });

            it(`should return true if all classifications are defined with due date`, () => {
                controller.classifications = [
                    {finished: Date.now()},
                    {finished: Date.now()}
                ];

                let result = controller.canCreateNew();

                expect(result).toBeTruthy();
            });
        });

        describe('closeContract()', () => {
            it('should define the classificationId property of the controller and then call the show method()', () => {
                controller.$scope.closeContract = {show: () => {}};
                spyOn(controller.$scope.closeContract, 'show');

                expect(controller.classificationId).toBeFalsy();
                controller.closeContract({id: 1});

                expect(controller.classificationId).toEqual(1);
                expect(controller.$scope.closeContract.show).toHaveBeenCalledWith();
            });
        });

        describe('returnDialog()', () => {
            it('should call the returnDialog method and perform a PATCH query, then call _getClassifications method', () => {
                spyOn(controller, '_getClassifications');
                controller.classificationId = 1;
                $httpBackend.when('PATCH', `/client/api/CreditClassifications/1`).respond(200);
                $httpBackend.expect('PATCH', `/client/api/CreditClassifications/1`);
                controller.returnDialog('ACCEPT');
                $httpBackend.flush();

                expect(controller._getClassifications).toHaveBeenCalledWith(101);
            });
        });
    });
});