Updated unit tests
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Joan Sanchez 2022-10-04 09:21:24 +02:00
parent c8bad23621
commit 3dbd304217
5 changed files with 87 additions and 62 deletions

View File

@ -18,7 +18,8 @@ class Report {
access_token: this.vnToken.token
}, params);
const serializedParams = this.$httpParamSerializer(params);
window.open(`api/${path}?${serializedParams}`);
const query = serializedParams ? `?${serializedParams}` : '';
window.open(`api/${path}${query}`);
}
}
Report.$inject = ['$httpParamSerializer', 'vnToken'];

View File

@ -41,8 +41,7 @@
model="ClientSample.typeFk"
data="samplesVisible"
show-field="description"
label="Sample"
required="true">
label="Sample">
</vn-autocomplete>
</vn-horizontal>
<vn-horizontal>
@ -80,11 +79,9 @@
</vn-card>
<vn-button-bar>
<vn-submit
disabled="!sampleType.selection"
label="Send">
</vn-submit>
<vn-button
disabled="!sampleType.selection.hasPreview"
label="Preview"
ng-click="$ctrl.preview()">
</vn-button>

View File

@ -37,27 +37,38 @@ class Controller extends Section {
onSubmit() {
this.$.watcher.check();
const validationMessage = this.validate();
if (validationMessage)
return this.vnApp.showError(this.$t(validationMessage));
this.$.watcher.realSubmit().then(() => this.send());
}
validateParams(params) {
validate() {
const sampleType = this.$.sampleType.selection;
if (!params.recipient)
return this.vnApp.showError(this.$t('Email cannot be blank'));
if (!this.clientSample.recipient)
return 'Email cannot be blank';
if (!sampleType)
return this.vnApp.showError(this.$t('Choose a sample'));
return 'Choose a sample';
if (sampleType.hasCompany && !this.clientSample.companyFk)
return this.vnApp.showError(this.$t('Choose a company'));
return 'Choose a company';
if (sampleType.datepickerEnabled && !this.clientSample.from)
return 'Choose a date';
return;
}
setParams(params) {
const sampleType = this.$.sampleType.selection;
if (sampleType.hasCompany)
params.companyId = this.clientSample.companyFk;
if (sampleType.datepickerEnabled && !this.clientSample.from)
return this.vnApp.showError(this.$t('Choose a date'));
if (sampleType.datepickerEnabled)
params.from = this.clientSample.from;
}
@ -66,11 +77,14 @@ class Controller extends Section {
const sampleType = this.$.sampleType.selection;
const params = {
recipientId: this.$params.id,
recipient: this.clientSample.recipient
recipientId: this.$params.id
};
this.validateParams(params);
const validationMessage = this.validate();
if (validationMessage)
return this.vnApp.showError(this.$t(validationMessage));
this.setParams(params);
const path = `${sampleType.model}/${this.$params.id}/${sampleType.code}-html`;
this.$http.get(path, {params})
@ -94,7 +108,11 @@ class Controller extends Section {
replyTo: this.clientSample.replyTo
};
this.validateParams(params);
const validationMessage = this.validate();
if (validationMessage)
return this.vnApp.showError(this.$t(validationMessage));
this.setParams(params);
const path = `${sampleType.model}/${this.$params.id}/${sampleType.code}-email`;
this.vnEmail.send(path, params)

View File

@ -40,7 +40,7 @@ describe('Client', () => {
$httpParamSerializer = _$httpParamSerializer_;
$element = angular.element('<vn-client-sample-create></vn-client-sample-create>');
controller = $componentController('vnClientSampleCreate', {$element, $scope});
controller.client = {id: 1101};
controller._client = {id: 1101};
const element = document.createElement('div');
document.body.querySelector = () => {
return {
@ -49,11 +49,23 @@ describe('Client', () => {
}
};
};
// $httpBackend.expectGET('EmailUsers?filter=%7B%22where%22:%7B%7D%7D').respond();
}));
describe('onSubmit()', () => {
it(`should call send() method`, () => {
jest.spyOn(controller, 'send');
controller.send = jest.fn();
controller.$.sampleType.selection = {
hasCompany: false,
code: 'MyReport',
model: 'Clients'
};
controller.clientSample = {
recipient: 'email@email'
};
controller.onSubmit();
expect(controller.send).toHaveBeenCalledWith();
@ -73,7 +85,7 @@ describe('Client', () => {
recipientId: 1101
};
controller.send(false, () => {});
controller.send();
expect(controller.$http.get).not.toHaveBeenCalled();
});
@ -87,7 +99,7 @@ describe('Client', () => {
recipient: 'client@email.com'
};
controller.send(false, () => {});
controller.send();
expect(controller.$http.get).not.toHaveBeenCalled();
});
@ -104,84 +116,81 @@ describe('Client', () => {
recipient: 'client@email.com'
};
controller.send(false, () => {});
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: 'MyReport'
code: 'my-report',
model: 'Clients'
};
controller.clientSample = {
recipientId: 1101,
recipient: 'client@email.com'
};
const expectedParams = {
recipientId: 1101,
recipient: 'client@email.com'
};
const serializedParams = $httpParamSerializer(expectedParams);
$httpBackend.expect('GET', `email/MyReport?${serializedParams}`).respond(true);
controller.send(false, () => {});
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: 'MyReport'
code: 'my-report',
model: 'Clients'
};
controller.clientSample = {
recipientId: 1101,
recipient: 'client@email.com',
companyFk: 442
};
const expectedParams = {
recipientId: 1101,
recipient: 'client@email.com',
companyId: 442
};
const serializedParams = $httpParamSerializer(expectedParams);
$httpBackend.expect('GET', `email/MyReport?${serializedParams}`).respond(true);
controller.send(false, () => {});
const expectedPath = `Clients/${controller.client.id}/my-report-email`;
$httpBackend.expect('POST', expectedPath).respond(true);
controller.send();
$httpBackend.flush();
});
});
describe('showPreview()', () => {
describe('preview()', () => {
it(`should open a sample preview`, () => {
jest.spyOn(controller.$.showPreview, 'show');
controller.send = (isPreview, cb) => {
cb({
data: '<div></div>'
});
controller.$.sampleType.selection = {
hasCompany: true,
code: 'my-report',
model: 'Clients'
};
controller.showPreview();
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('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';

View File

@ -96,8 +96,8 @@ describe('Component vnTicketIndex', () => {
controller.setDelivered();
$httpBackend.flush();
expect($window.open).toHaveBeenCalledWith(`Tickets/${tickets[1].id}/delivery-note-pdf`);
expect($window.open).toHaveBeenCalledWith(`Tickets/${tickets[2].id}/delivery-note-pdf`);
expect($window.open).toHaveBeenCalledWith(`api/Tickets/${tickets[1].id}/delivery-note-pdf`);
expect($window.open).toHaveBeenCalledWith(`api/Tickets/${tickets[2].id}/delivery-note-pdf`);
});
});