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

describe('Client', () => {
    describe('Component vnClientConsumption', () => {
        let $scope;
        let controller;
        let $httpParamSerializer;
        let $httpBackend;

        beforeEach(ngModule('client'));

        beforeEach(inject(($componentController, $rootScope, _$httpParamSerializer_, _$httpBackend_) => {
            $scope = $rootScope.$new();
            $httpParamSerializer = _$httpParamSerializer_;
            $httpBackend = _$httpBackend_;
            const $element = angular.element('<vn-client-consumption></vn-client-consumption');
            controller = $componentController('vnClientConsumption', {$element, $scope});
            controller.$.model = crudModel;
            controller.client = {
                id: 1101
            };
        }));

        describe('showReport()', () => {
            it('should call the window.open function', () => {
                jest.spyOn(window, 'open').mockReturnThis();

                const now = Date.vnNew();
                controller.$.model.userParams = {
                    from: now,
                    to: now
                };

                controller.showReport();

                const clientId = controller.client.id;
                const expectedParams = {
                    recipientId: clientId,
                    from: now,
                    to: now
                };
                const serializedParams = $httpParamSerializer(expectedParams);
                const expectedPath = `api/Clients/${clientId}/campaign-metrics-pdf?${serializedParams}`;

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

        describe('sendEmail()', () => {
            it('should make a GET query sending the report', () => {
                const now = Date.vnNew();
                controller.$.model.userParams = {
                    from: now,
                    to: now
                };
                const clientId = controller.client.id;
                const expectedPath = `Clients/${clientId}/campaign-metrics-email`;

                $httpBackend.expect('POST', expectedPath).respond({});
                controller.sendEmail();
                $httpBackend.flush();
            });
        });
    });
});