import './index'; describe('Client', () => { describe('Component vnClientSampleCreate', () => { let $httpParamSerializer; let $scope; let $element; let $httpBackend; let $state; let controller; beforeEach(ngModule('client')); beforeEach(inject(($componentController, _$httpBackend_, $rootScope, _$state_, _$httpParamSerializer_) => { $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 = 1101; $httpBackend = _$httpBackend_; $httpParamSerializer = _$httpParamSerializer_; $element = angular.element(''); controller = $componentController('vnClientSampleCreate', {$element, $scope}); controller._client = {id: 1101}; const element = document.createElement('div'); document.body.querySelector = () => { return { querySelector: () => { return element; } }; }; // $httpBackend.expectGET('EmailUsers?filter=%7B%22where%22:%7B%7D%7D').respond(); })); describe('onSubmit()', () => { it(`should call send() method`, () => { controller.send = jest.fn(); controller.$.sampleType.selection = { hasCompany: false, code: 'MyReport', model: 'Clients' }; controller.clientSample = { recipient: 'email@email' }; controller.onSubmit(); expect(controller.send).toHaveBeenCalledWith(); }); }); describe('send()', () => { it(`should not perform an HTTP query if no recipient is specified`, () => { jest.spyOn(controller.$http, 'get'); controller.$.sampleType.selection = { hasCompany: false, code: 'MyReport', model: 'Clients' }; controller.clientSample = { recipientId: 1101 }; controller.send(); expect(controller.$http.get).not.toHaveBeenCalled(); }); it(`should not perform an HTTP query if no sample is specified`, () => { jest.spyOn(controller.$http, 'get'); controller.$.sampleType.selection = null; controller.clientSample = { recipientId: 1101, recipient: 'client@email.com' }; controller.send(); expect(controller.$http.get).not.toHaveBeenCalled(); }); it(`should not perform an HTTP query if company is required and not specified`, () => { jest.spyOn(controller.$http, 'get'); controller.$.sampleType.selection = { hasCompany: true, code: 'MyReport' }; controller.clientSample = { recipientId: 1101, recipient: 'client@email.com' }; controller.send(); expect(controller.$http.get).not.toHaveBeenCalled(); }); it(`should perform an HTTP query without passing companyFk param`, () => { $state.go = jest.fn(); controller.$.sampleType.selection = { hasCompany: false, code: 'my-report', model: 'Clients' }; controller.clientSample = { recipientId: 1101, recipient: 'client@email.com' }; const expectedPath = `Clients/${controller.client.id}/my-report-email`; $httpBackend.expect('POST', expectedPath).respond(true); controller.send(); $httpBackend.flush(); }); it(`should perform an HTTP query passing companyFk param`, () => { $state.go = jest.fn(); controller.$.sampleType.selection = { hasCompany: true, code: 'my-report', model: 'Clients' }; controller.clientSample = { recipientId: 1101, recipient: 'client@email.com', companyFk: 442 }; const expectedPath = `Clients/${controller.client.id}/my-report-email`; $httpBackend.expect('POST', expectedPath).respond(true); controller.send(); $httpBackend.flush(); }); }); describe('preview()', () => { it(`should open a sample preview`, () => { jest.spyOn(controller.$.showPreview, 'show'); controller.$.sampleType.selection = { hasCompany: true, code: 'my-report', model: 'Clients' }; controller.clientSample = { recipientId: 1101, recipient: 'client@email.com', companyFk: 442 }; const expectedParams = { companyId: 442, recipientId: 1101 }; const serializedParams = $httpParamSerializer(expectedParams); const expectedPath = `Clients/${controller.client.id}/my-report-html?${serializedParams}`; $httpBackend.expect('GET', expectedPath).respond(true); controller.preview(); $httpBackend.flush(); expect(controller.$.showPreview.show).toHaveBeenCalledWith(); }); }); describe('setClientSample()', () => { it(`should perform a query and then set the replyTo property to the clientSample object`, () => { const client = {email: 'test@example.com'}; const expectedEmail = 'batman@arkhamcity.com'; const serializedParams = $httpParamSerializer({filter: {where: {}}}); $httpBackend.expect('GET', `EmailUsers?${serializedParams}`).respond([{email: expectedEmail}]); controller.setClientSample(client); $httpBackend.flush(); expect(controller.clientSample.replyTo).toEqual(expectedEmail); expect(controller.clientSample.clientFk).toEqual(controller.$params.id); expect(controller.clientSample.recipient).toEqual(client.email); expect(controller.clientSample.companyId).toEqual(controller.vnConfig.companyFk); }); }); }); });