Refactor + updated tests
gitea/salix/2110-client_sample_validation This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-02-18 11:14:02 +01:00
parent 7e38100d05
commit 2460725d98
4 changed files with 130 additions and 147 deletions

View File

@ -15,7 +15,8 @@
<vn-horizontal> <vn-horizontal>
<vn-textfield vn-one <vn-textfield vn-one
label="Recipient" label="Recipient"
ng-model="$ctrl.clientSample.recipient"> ng-model="$ctrl.clientSample.recipient"
info="Its only used when sample is sent">
</vn-textfield> </vn-textfield>
</vn-horizontal> </vn-horizontal>
<vn-horizontal> <vn-horizontal>
@ -30,7 +31,7 @@
</vn-autocomplete> </vn-autocomplete>
<vn-autocomplete vn-one <vn-autocomplete vn-one
ng-model="$ctrl.companyId" ng-model="$ctrl.companyId"
model="ClientSample.companyFk" model="ClientSample.companyId"
data="companiesData" data="companiesData"
show-field="code" show-field="code"
value-field="id" value-field="id"

View File

@ -10,7 +10,7 @@ class Controller extends Component {
this.vnConfig = vnConfig; this.vnConfig = vnConfig;
this.clientSample = { this.clientSample = {
clientFk: this.$params.id, clientFk: this.$params.id,
companyFk: vnConfig.companyFk companyId: vnConfig.companyFk
}; };
} }
@ -26,44 +26,13 @@ class Controller extends Component {
} }
get companyId() { get companyId() {
if (!this.clientSample.companyFk) if (!this.clientSample.companyId)
this.clientSample.companyFk = this.vnConfig.companyFk; this.clientSample.companyId = this.vnConfig.companyFk;
return this.clientSample.companyFk; return this.clientSample.companyId;
} }
set companyId(value) { set companyId(value) {
this.clientSample.companyFk = value; this.clientSample.companyId = value;
}
showPreview() {
let sampleType = this.$.sampleType.selection;
if (!sampleType)
return this.vnApp.showError(this.$translate.instant('Choose a sample'));
if (sampleType.hasCompany && !this.clientSample.companyFk)
return this.vnApp.showError(this.$translate.instant('Choose a company'));
const params = {
clientId: this.$params.id,
recipient: this.clientSample.recipient,
isPreview: true
};
if (sampleType.hasCompany)
params.companyId = this.clientSample.companyFk;
const serializedParams = this.$httpParamSerializer(params);
const query = `email/${sampleType.code}?${serializedParams}`;
this.$http.get(query).then(res => {
this.$.showPreview.show();
let dialog = document.body.querySelector('div.vn-dialog');
let body = dialog.querySelector('tpl-body');
let scroll = dialog.querySelector('div:first-child');
body.innerHTML = res.data;
scroll.scrollTop = 0;
});
} }
onSubmit() { onSubmit() {
@ -73,28 +42,49 @@ class Controller extends Component {
); );
} }
showPreview() {
this.send(true, res => {
this.$.showPreview.show();
const dialog = document.body.querySelector('div.vn-dialog');
const body = dialog.querySelector('tpl-body');
const scroll = dialog.querySelector('div:first-child');
body.innerHTML = res.data;
scroll.scrollTop = 0;
});
}
sendSample() { sendSample() {
let sampleType = this.$.sampleType.selection; this.send(false, () => {
let params = { this.vnApp.showSuccess(this.$translate.instant('Notification sent!'));
this.$state.go('client.card.sample.index');
});
}
send(isPreview, cb) {
const sampleType = this.$.sampleType.selection;
const params = {
clientId: this.$params.id, clientId: this.$params.id,
recipient: this.clientSample.recipient recipient: this.clientSample.recipient
}; };
if (!params.recipient)
return this.vnApp.showError(this.$translate.instant('Email cannot be blank'));
if (!sampleType) if (!sampleType)
return this.vnApp.showError(this.$translate.instant('Choose a sample')); return this.vnApp.showError(this.$translate.instant('Choose a sample'));
if (sampleType.hasCompany && !this.clientSample.companyFk) if (sampleType.hasCompany && !this.clientSample.companyId)
return this.vnApp.showError(this.$translate.instant('Choose a company')); return this.vnApp.showError(this.$translate.instant('Choose a company'));
if (sampleType.hasCompany) if (sampleType.hasCompany)
params.companyId = this.clientSample.companyFk; params.companyId = this.clientSample.companyId;
if (isPreview) params.isPreview = true;
const serializedParams = this.$httpParamSerializer(params); const serializedParams = this.$httpParamSerializer(params);
const query = `email/${sampleType.code}?${serializedParams}`; const query = `email/${sampleType.code}?${serializedParams}`;
this.$http.get(query).then(res => { this.$http.get(query).then(cb);
this.vnApp.showSuccess(this.$translate.instant('Notification sent!'));
this.$state.go('client.card.sample.index');
});
} }
} }
Controller.$inject = ['$element', '$scope', 'vnApp', '$httpParamSerializer', 'vnConfig']; Controller.$inject = ['$element', '$scope', 'vnApp', '$httpParamSerializer', 'vnConfig'];

View File

@ -40,84 +40,16 @@ 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});
const element = document.createElement('div');
document.body.querySelector = () => {
return {
querySelector: () => {
return element;
}
};
};
})); }));
describe('showPreview()', () => {
it(`should perform a query (GET) and open a sample preview`, () => {
spyOn(controller.$.showPreview, 'show');
const element = document.createElement('div');
document.body.querySelector = () => {
return {
querySelector: () => {
return element;
}
};
};
controller.$.sampleType.selection = {
hasCompany: false,
code: 'MyReport'
};
controller.clientSample = {
clientFk: 101
};
let event = {preventDefault: () => {}};
const params = {
clientId: 101,
isPreview: true
};
const serializedParams = $httpParamSerializer(params);
$httpBackend.when('GET', `email/MyReport?${serializedParams}`).respond(true);
$httpBackend.expect('GET', `email/MyReport?${serializedParams}`);
controller.showPreview(event);
$httpBackend.flush();
expect(controller.$.showPreview.show).toHaveBeenCalledWith();
});
it(`should perform a query (GET) with companyFk param and open a sample preview`, () => {
spyOn(controller.$.showPreview, 'show');
const element = document.createElement('div');
document.body.querySelector = () => {
return {
querySelector: () => {
return element;
}
};
};
controller.$.sampleType.selection = {
hasCompany: true,
code: 'MyReport'
};
controller.clientSample = {
clientFk: 101,
companyFk: 442
};
let event = {preventDefault: () => {}};
const params = {
clientId: 101,
companyId: 442,
isPreview: true
};
const serializedParams = $httpParamSerializer(params);
$httpBackend.when('GET', `email/MyReport?${serializedParams}`).respond(true);
$httpBackend.expect('GET', `email/MyReport?${serializedParams}`);
controller.showPreview(event);
$httpBackend.flush();
expect(controller.$.showPreview.show).toHaveBeenCalledWith();
});
});
describe('onSubmit()', () => { describe('onSubmit()', () => {
it(`should call sendSample() method`, () => { it(`should call sendSample() method`, () => {
spyOn(controller, 'sendSample'); spyOn(controller, 'sendSample');
@ -127,55 +59,113 @@ describe('Client', () => {
}); });
}); });
describe('sendSample()', () => { describe('send()', () => {
it(`should perform a query (GET) and call go() method`, () => { it(`should not perform an HTTP query if no recipient is specified`, () => {
spyOn(controller.$state, 'go'); spyOn(controller.$http, 'get');
controller.$.sampleType.selection = { controller.$.sampleType.selection = {
hasCompany: false, hasCompany: false,
code: 'MyReport' code: 'MyReport'
}; };
controller.clientSample = { controller.clientSample = {
clientFk: 101
};
const params = {
clientId: 101 clientId: 101
}; };
const serializedParams = $httpParamSerializer(params);
$httpBackend.when('GET', `email/MyReport?${serializedParams}`).respond(true); controller.send(false, () => {});
$httpBackend.expect('GET', `email/MyReport?${serializedParams}`);
controller.sendSample();
$httpBackend.flush();
expect(controller.$state.go).toHaveBeenCalledWith('client.card.sample.index'); expect(controller.$http.get).not.toHaveBeenCalled();
}); });
it(`should perform a query (GET) with companyFk param and call go() method`, () => { it(`should not perform an HTTP query if no sample is specified`, () => {
spyOn(controller.$state, 'go'); 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 = { controller.$.sampleType.selection = {
hasCompany: true, hasCompany: true,
code: 'MyReport' code: 'MyReport'
}; };
controller.clientSample = { controller.clientSample = {
clientFk: 101, clientId: 101,
companyFk: 442 recipient: 'client@email.com'
}; };
const params = { 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, 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 companyId: 442
}; };
const serializedParams = $httpParamSerializer(params);
$httpBackend.when('GET', `email/MyReport?${serializedParams}`).respond(true); const serializedParams = $httpParamSerializer(controller.clientSample);
$httpBackend.expect('GET', `email/MyReport?${serializedParams}`); $httpBackend.expect('GET', `email/MyReport?${serializedParams}`).respond(true);
controller.sendSample(); controller.send(false, () => {});
$httpBackend.flush(); $httpBackend.flush();
});
});
describe('showPreview()', () => {
it(`should open a sample preview`, () => {
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`, () => {
spyOn(controller.$state, 'go');
controller.send = (isPreview, cb) => {
cb({
data: true
});
};
controller.sendSample();
expect(controller.$state.go).toHaveBeenCalledWith('client.card.sample.index'); expect(controller.$state.go).toHaveBeenCalledWith('client.card.sample.index');
}); });

View File

@ -1,3 +1,5 @@
Choose a sample: Selecciona una plantilla Choose a sample: Selecciona una plantilla
Choose a company: Selecciona una empresa Choose a company: Selecciona una empresa
Recipient: Destinatario Email cannot be blank: Debes introducir un email
Recipient: Destinatario
Its only used when sample is sent: Se utiliza únicamente cuando se envía la plantilla