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

describe('Supplier', () => {
    describe('Component vnSupplierConsumption', () => {
        let $scope;
        let controller;
        let $httpParamSerializer;
        let $httpBackend;
        const supplierId = 2;

        beforeEach(ngModule('supplier'));

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

        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 expectedParams = {
                    recipientId: 2,
                    from: now,
                    to: now
                };
                const serializedParams = $httpParamSerializer(expectedParams);
                const path = `api/Suppliers/${supplierId}/campaign-metrics-pdf?${serializedParams}`;

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

        describe('sendEmail()', () => {
            it('should throw an error', () => {
                jest.spyOn(controller.vnApp, 'showError');

                const expectedParams = {
                    filter: {
                        where: {
                            supplierFk: supplierId,
                            email: {neq: null}
                        },
                        limit: 1
                    }
                };
                const serializedParams = $httpParamSerializer(expectedParams);
                $httpBackend.expectGET(`SupplierContacts?${serializedParams}`).respond({});
                controller.sendEmail();
                $httpBackend.flush();

                expect(controller.vnApp.showError)
                    .toHaveBeenCalledWith(`This supplier doesn't have a contact with an email address`);
            });

            it('should make a GET query sending the report', () => {
                let serializedParams;
                const params = {
                    filter: {
                        where: {
                            supplierFk: supplierId,
                            email: {neq: null}
                        },
                        limit: 1
                    }
                };
                serializedParams = $httpParamSerializer(params);
                $httpBackend.whenGET(`SupplierContacts?${serializedParams}`).respond([
                    {id: 1, email: 'batman@gothamcity.com'}
                ]);

                const now = Date.vnNew();
                controller.$.model.userParams = {
                    from: now,
                    to: now
                };
                const expectedParams = {
                    recipient: 'batman@gothamcity.com',
                    from: now,
                    to: now
                };

                serializedParams = $httpParamSerializer(expectedParams);
                const path = `Suppliers/${supplierId}/campaign-metrics-email`;

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