import './index';

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

        beforeEach(ngModule('client'));

        beforeEach(angular.mock.inject(($componentController, _$httpBackend_, $rootScope, _$state_) => {
            $scope = $rootScope.$new();
            $scope.sampleType = {};
            $scope.watcher = {
                check: () => {},
                realSubmit: () => {
                    return {
                        then: callback => {
                            callback();
                        }
                    };
                }
            };
            $scope.showPreview = {
                element: {
                    querySelector: () => {
                        return {
                            innerHTML: () => {}
                        };
                    }
                },
                show: () => {}
            };
            $state = _$state_;
            $state.params.id = 101;
            $httpBackend = _$httpBackend_;
            controller = $componentController('vnClientSampleCreate', {$scope, $state});
        }));

        describe('showPreview()', () => {
            it(`should perform a query (GET) and open a sample preview`, () => {
                spyOn(controller.$scope.showPreview, 'show');

                controller.$scope.sampleType.selection = {
                    hasCompany: false,
                    code: 'MyReport'
                };

                controller.clientSample = {
                    clientFk: 101,
                    typeFK: 1
                };

                let event = {preventDefault: () => {}};

                $httpBackend.whenGET(`/mailer/notification/MyReport/101/preview`).respond(true);
                $httpBackend.expectGET(`/mailer/notification/MyReport/101/preview`);
                controller.showPreview(event);
                $httpBackend.flush();

                expect(controller.$scope.showPreview.show).toHaveBeenCalledWith();
            });

            it(`should perform a query (GET) with companyFk param and open a sample preview`, () => {
                spyOn(controller.$scope.showPreview, 'show');

                controller.$scope.sampleType.selection = {
                    hasCompany: true,
                    code: 'MyReport'
                };

                controller.clientSample = {
                    clientFk: 101,
                    companyFk: 442,
                    typeFK: 1
                };

                let event = {preventDefault: () => {}};

                $httpBackend.whenGET(`/mailer/notification/MyReport/442/101/preview`).respond(true);
                $httpBackend.expectGET(`/mailer/notification/MyReport/442/101/preview`);
                controller.showPreview(event);
                $httpBackend.flush();

                expect(controller.$scope.showPreview.show).toHaveBeenCalledWith();
            });
        });

        describe('onSubmit()', () => {
            it(`should call sendSample() method`, () => {
                spyOn(controller, 'sendSample');
                controller.onSubmit();

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

        describe('sendSample()', () => {
            it(`should perform a query (GET) and call go() method`, () => {
                spyOn(controller.$state, 'go');

                controller.$scope.sampleType.selection = {
                    hasCompany: false,
                    code: 'MyReport'
                };

                controller.clientSample = {
                    clientFk: 101,
                    typeFK: 1
                };

                $httpBackend.whenGET(`/mailer/notification/MyReport/101`).respond(true);
                $httpBackend.expectGET(`/mailer/notification/MyReport/101`);
                controller.sendSample();
                $httpBackend.flush();

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

            it(`should perform a query (GET) with companyFk param and call go() method`, () => {
                spyOn(controller.$state, 'go');

                controller.$scope.sampleType.selection = {
                    hasCompany: true,
                    code: 'MyReport'
                };

                controller.clientSample = {
                    clientFk: 101,
                    companyFk: 442,
                    typeFK: 1
                };

                $httpBackend.whenGET(`/mailer/notification/MyReport/442/101`).respond(true);
                $httpBackend.expectGET(`/mailer/notification/MyReport/442/101`);
                controller.sendSample();
                $httpBackend.flush();

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