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 = 101; $httpBackend = _$httpBackend_; $httpParamSerializer = _$httpParamSerializer_; $element = angular.element(''); controller = $componentController('vnClientSampleCreate', {$element, $scope}); const element = document.createElement('div'); document.body.querySelector = () => { return { querySelector: () => { return element; } }; }; })); describe('onSubmit()', () => { it(`should call sendSample() method`, () => { jest.spyOn(controller, 'sendSample'); controller.onSubmit(); expect(controller.sendSample).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' }; controller.clientSample = { recipientId: 101 }; controller.send(false, () => {}); 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: 101, recipient: 'client@email.com' }; controller.send(false, () => {}); 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: 101, recipient: 'client@email.com' }; controller.send(false, () => {}); expect(controller.$http.get).not.toHaveBeenCalled(); }); it(`should perform an HTTP query without passing companyFk param`, () => { controller.$.sampleType.selection = { hasCompany: false, code: 'MyReport' }; controller.clientSample = { recipientId: 101, recipient: 'client@email.com' }; const expectedParams = { recipientId: 101, recipient: 'client@email.com' }; const serializedParams = $httpParamSerializer(expectedParams); $httpBackend.expect('GET', `email/MyReport?${serializedParams}`).respond(true); controller.send(false, () => {}); $httpBackend.flush(); }); it(`should perform an HTTP query passing companyFk param`, () => { controller.$.sampleType.selection = { hasCompany: true, code: 'MyReport' }; controller.clientSample = { recipientId: 101, recipient: 'client@email.com', companyFk: 442 }; const expectedParams = { recipientId: 101, recipient: 'client@email.com', companyId: 442 }; const serializedParams = $httpParamSerializer(expectedParams); $httpBackend.expect('GET', `email/MyReport?${serializedParams}`).respond(true); controller.send(false, () => {}); $httpBackend.flush(); }); }); describe('showPreview()', () => { it(`should open a sample preview`, () => { jest.spyOn(controller.$.showPreview, 'show'); controller.send = (isPreview, cb) => { cb({ data: '
' }); }; controller.showPreview(); expect(controller.$.showPreview.show).toHaveBeenCalledWith(); }); }); describe('sendSample()', () => { it(`should perform a query (GET) and call go() method`, () => { jest.spyOn(controller.$state, 'go'); controller.send = (isPreview, cb) => { cb({ data: true }); }; controller.sendSample(); expect(controller.$state.go).toHaveBeenCalledWith('client.card.sample.index'); }); }); describe('getWorkerEmail()', () => { it(`should perform a query and then set the replyTo property to the clientSample object`, () => { const expectedEmail = 'batman@arkhamcity.com'; const serializedParams = $httpParamSerializer({filter: {where: {}}}); $httpBackend.expect('GET', `EmailUsers?${serializedParams}`).respond([{email: expectedEmail}]); controller.getWorkerEmail(); $httpBackend.flush(); expect(controller.clientSample.replyTo).toEqual(expectedEmail); }); }); }); });