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

184 lines
6.2 KiB
JavaScript
Raw Normal View History

2018-07-31 09:08:22 +00:00
import './index';
2019-01-30 07:28:09 +00:00
describe('Client', () => {
2018-07-31 09:08:22 +00:00
describe('Component vnClientSampleCreate', () => {
2019-11-06 08:32:54 +00:00
let $httpParamSerializer;
2018-07-31 09:08:22 +00:00
let $scope;
2019-11-11 10:23:39 +00:00
let $element;
2018-07-31 09:08:22 +00:00
let $httpBackend;
let $state;
let controller;
beforeEach(ngModule('client'));
2018-07-31 09:08:22 +00:00
2020-07-23 14:46:16 +00:00
beforeEach(inject(($componentController, _$httpBackend_, $rootScope, _$state_, _$httpParamSerializer_) => {
2018-07-31 09:08:22 +00:00
$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_;
2019-11-06 08:32:54 +00:00
$httpParamSerializer = _$httpParamSerializer_;
2019-11-11 10:23:39 +00:00
$element = angular.element('<vn-client-sample-create></vn-client-sample-create>');
controller = $componentController('vnClientSampleCreate', {$element, $scope});
2020-02-18 10:14:02 +00:00
const element = document.createElement('div');
document.body.querySelector = () => {
return {
querySelector: () => {
return element;
}
};
};
2018-07-31 09:08:22 +00:00
}));
2020-02-18 10:14:02 +00:00
describe('onSubmit()', () => {
it(`should call sendSample() method`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'sendSample');
2020-02-18 10:14:02 +00:00
controller.onSubmit();
expect(controller.sendSample).toHaveBeenCalledWith();
});
});
describe('send()', () => {
it(`should not perform an HTTP query if no recipient is specified`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller.$http, 'get');
2018-07-31 09:08:22 +00:00
2019-11-11 10:23:39 +00:00
controller.$.sampleType.selection = {
2018-07-31 09:08:22 +00:00
hasCompany: false,
code: 'MyReport'
};
controller.clientSample = {
2020-05-25 05:43:24 +00:00
recipientId: 101
2018-07-31 09:08:22 +00:00
};
2020-02-18 10:14:02 +00:00
controller.send(false, () => {});
2018-07-31 09:08:22 +00:00
2020-02-18 10:14:02 +00:00
expect(controller.$http.get).not.toHaveBeenCalled();
});
it(`should not perform an HTTP query if no sample is specified`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller.$http, 'get');
2020-02-18 10:14:02 +00:00
controller.$.sampleType.selection = null;
controller.clientSample = {
2020-05-25 05:43:24 +00:00
recipientId: 101,
2020-02-18 10:14:02 +00:00
recipient: 'client@email.com'
2019-11-06 08:32:54 +00:00
};
2020-02-18 10:14:02 +00:00
controller.send(false, () => {});
2018-07-31 09:08:22 +00:00
2020-02-18 10:14:02 +00:00
expect(controller.$http.get).not.toHaveBeenCalled();
2018-07-31 09:08:22 +00:00
});
2020-02-18 10:14:02 +00:00
it(`should not perform an HTTP query if company is required and not specified`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller.$http, 'get');
2018-07-31 09:08:22 +00:00
2019-11-11 10:23:39 +00:00
controller.$.sampleType.selection = {
2018-07-31 09:08:22 +00:00
hasCompany: true,
code: 'MyReport'
};
controller.clientSample = {
2020-05-25 05:43:24 +00:00
recipientId: 101,
2020-02-18 10:14:02 +00:00
recipient: 'client@email.com'
2019-11-06 08:32:54 +00:00
};
2018-07-31 09:08:22 +00:00
2020-02-18 10:14:02 +00:00
controller.send(false, () => {});
2018-07-31 09:08:22 +00:00
2020-02-18 10:14:02 +00:00
expect(controller.$http.get).not.toHaveBeenCalled();
2018-07-31 09:08:22 +00:00
});
2020-02-26 13:45:47 +00:00
it(`should perform an HTTP query without passing companyFk param`, () => {
2019-11-11 10:23:39 +00:00
controller.$.sampleType.selection = {
2018-07-31 09:08:22 +00:00
hasCompany: false,
code: 'MyReport'
};
controller.clientSample = {
2020-05-25 05:43:24 +00:00
recipientId: 101,
2020-02-18 10:14:02 +00:00
recipient: 'client@email.com'
2019-11-06 08:32:54 +00:00
};
2020-02-26 13:45:47 +00:00
const expectedParams = {
2020-05-25 05:43:24 +00:00
recipientId: 101,
2020-02-26 13:45:47 +00:00
recipient: 'client@email.com'
};
const serializedParams = $httpParamSerializer(expectedParams);
2019-11-06 08:32:54 +00:00
2020-02-18 10:14:02 +00:00
$httpBackend.expect('GET', `email/MyReport?${serializedParams}`).respond(true);
controller.send(false, () => {});
2018-07-31 09:08:22 +00:00
$httpBackend.flush();
});
2020-02-26 13:45:47 +00:00
it(`should perform an HTTP query passing companyFk param`, () => {
2019-11-11 10:23:39 +00:00
controller.$.sampleType.selection = {
2018-07-31 09:08:22 +00:00
hasCompany: true,
code: 'MyReport'
};
controller.clientSample = {
2020-05-25 05:43:24 +00:00
recipientId: 101,
2020-02-26 13:45:47 +00:00
recipient: 'client@email.com',
companyFk: 442
};
const expectedParams = {
2020-05-25 05:43:24 +00:00
recipientId: 101,
2020-02-18 10:14:02 +00:00
recipient: 'client@email.com',
2019-11-06 08:32:54 +00:00
companyId: 442
};
2020-02-26 13:45:47 +00:00
const serializedParams = $httpParamSerializer(expectedParams);
2019-11-06 08:32:54 +00:00
2020-02-18 10:14:02 +00:00
$httpBackend.expect('GET', `email/MyReport?${serializedParams}`).respond(true);
controller.send(false, () => {});
2018-07-31 09:08:22 +00:00
$httpBackend.flush();
2020-02-18 10:14:02 +00:00
});
});
describe('showPreview()', () => {
it(`should open a sample preview`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller.$.showPreview, 'show');
2020-02-18 10:14:02 +00:00
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`, () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller.$state, 'go');
2020-02-18 10:14:02 +00:00
controller.send = (isPreview, cb) => {
cb({
data: true
});
};
controller.sendSample();
2018-07-31 09:08:22 +00:00
expect(controller.$state.go).toHaveBeenCalledWith('client.card.sample.index');
});
});
});
});