import './index'; describe('Client', () => { describe('Component vnClientSampleCreate', () => { let $httpParamSerializer; let $scope; let $element; let $httpBackend; let $state; let controller; beforeEach(ngModule('client')); beforeEach(angular.mock.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`, () => { spyOn(controller, 'sendSample'); controller.onSubmit(); expect(controller.sendSample).toHaveBeenCalledWith(); }); }); describe('send()', () => { it(`should not perform an HTTP query if no recipient is specified`, () => { spyOn(controller.$http, 'get'); controller.$.sampleType.selection = { hasCompany: false, code: 'MyReport' }; controller.clientSample = { clientId: 101 }; controller.send(false, () => {}); expect(controller.$http.get).not.toHaveBeenCalled(); }); it(`should not perform an HTTP query if no sample is specified`, () => { spyOn(controller.$http, 'get'); controller.$.sampleType.selection = null; controller.clientSample = { clientId: 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`, () => { spyOn(controller.$http, 'get'); controller.$.sampleType.selection = { hasCompany: true, code: 'MyReport' }; controller.clientSample = { clientId: 101, recipient: 'client@email.com' }; controller.send(false, () => {}); expect(controller.$http.get).not.toHaveBeenCalled(); }); it(`should perform an HTTP query without passing companyId param`, () => { controller.$.sampleType.selection = { hasCompany: false, code: 'MyReport' }; controller.clientSample = { clientId: 101, recipient: 'client@email.com' }; const serializedParams = $httpParamSerializer(controller.clientSample); $httpBackend.expect('GET', `email/MyReport?${serializedParams}`).respond(true); controller.send(false, () => {}); $httpBackend.flush(); }); it(`should perform an HTTP query passing companyId param`, () => { controller.$.sampleType.selection = { hasCompany: true, code: 'MyReport' }; controller.clientSample = { clientId: 101, recipient: 'client@email.com', companyId: 442 }; const serializedParams = $httpParamSerializer(controller.clientSample); $httpBackend.expect('GET', `email/MyReport?${serializedParams}`).respond(true); controller.send(false, () => {}); $httpBackend.flush(); }); }); describe('showPreview()', () => { it(`should open a sample preview`, () => { 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`, () => { spyOn(controller.$state, 'go'); controller.send = (isPreview, cb) => { cb({ data: true }); }; controller.sendSample(); expect(controller.$state.go).toHaveBeenCalledWith('client.card.sample.index'); }); }); }); });