import './index';

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

        beforeEach(ngModule('client'));

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

        describe('$onChanges()', () => {
            it('should perform a GET query and define summary property', () => {
                let res = {name: 'Superman', classifications: []};

                spyOn(controller, 'sumRisk');
                $httpBackend.when('GET', `/client/api/Clients/101/summary`).respond(200, res);
                $httpBackend.expect('GET', `/client/api/Clients/101/summary`);

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

                expect(controller.summary).toBeDefined();
                expect(controller.summary.name).toEqual('Superman');
            });
        });

        describe('sumRisk()', () => {
            it('should sum property amount of an array', () => {
                controller.summary = {
                    clientRisks: [{
                        companyFk: 442,
                        amount: 100
                    },
                    {
                        companyFk: 567,
                        amount: 200
                    }]};

                let result = controller.sumRisk();

                expect(result).toEqual(300);
            });
        });
    });
});