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 access_token: this.vnToken.token
}, params); }, params);
const serializedParams = this.$httpParamSerializer(params); const serializedParams = this.$httpParamSerializer(params);
window.open(`api/${path}?${serializedParams}`); const query = serializedParams ? `?${serializedParams}` : '';
window.open(`api/${path}${query}`);
} }
} }
Report.$inject = ['$httpParamSerializer', 'vnToken']; Report.$inject = ['$httpParamSerializer', 'vnToken'];

View File

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

View File

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

View File

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

View File

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