salix/modules/client/front/sample/create/index.spec.js

184 lines
6.2 KiB
JavaScript

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('<vn-client-sample-create></vn-client-sample-create>');
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 = {
clientId: 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 = {
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`, () => {
jest.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 companyFk param`, () => {
controller.$.sampleType.selection = {
hasCompany: false,
code: 'MyReport'
};
controller.clientSample = {
clientId: 101,
recipient: 'client@email.com'
};
const expectedParams = {
clientId: 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 = {
clientId: 101,
recipient: 'client@email.com',
companyFk: 442
};
const expectedParams = {
clientId: 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: '<div></div>'
});
};
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');
});
});
});
});