fix(validate): allow getMessage to return message based on config

This commit is contained in:
Thomas Allmer 2019-12-13 12:38:51 +01:00 committed by Thomas Allmer
parent 70142b736b
commit c21eabbacd
3 changed files with 54 additions and 1 deletions

View file

@ -55,6 +55,7 @@ export class Validator {
name: this.name, name: this.name,
type: this.type, type: this.type,
params: this.param, params: this.param,
config: this.config,
...data, ...data,
}; };
if (typeof this.config.getMessage === 'function') { if (typeof this.config.getMessage === 'function') {

View file

@ -402,6 +402,7 @@ export function runValidateMixinFeedbackPart() {
await el.updateComplete; await el.updateComplete;
await el.feedbackComplete; await el.feedbackComplete;
expect(constructorMessageSpy.args[0][0]).to.eql({ expect(constructorMessageSpy.args[0][0]).to.eql({
config: { type: 'x' },
params: 4, params: 4,
modelValue: 'cat', modelValue: 'cat',
formControl: el, formControl: el,
@ -423,6 +424,9 @@ export function runValidateMixinFeedbackPart() {
await el.updateComplete; await el.updateComplete;
await el.feedbackComplete; await el.feedbackComplete;
expect(instanceMessageSpy.args[0][0]).to.eql({ expect(instanceMessageSpy.args[0][0]).to.eql({
config: {
getMessage: instanceMessageSpy,
},
params: 4, params: 4,
modelValue: 'cat', modelValue: 'cat',
formControl: el, formControl: el,
@ -447,6 +451,7 @@ export function runValidateMixinFeedbackPart() {
await el.updateComplete; await el.updateComplete;
await el.feedbackComplete; await el.feedbackComplete;
expect(spy.args[0][0]).to.eql({ expect(spy.args[0][0]).to.eql({
config: {},
params: 4, params: 4,
modelValue: 'cat', modelValue: 'cat',
formControl: el, formControl: el,
@ -473,7 +478,12 @@ export function runValidateMixinFeedbackPart() {
`); `);
await el.updateComplete; await el.updateComplete;
await el.feedbackComplete; await el.feedbackComplete;
expect(spy.args[0][0]).to.eql({
// ignore fieldName Promise as it will always be unique
const compare = spy.args[0][0];
delete compare.config.fieldName;
expect(compare).to.eql({
config: {},
params: 4, params: 4,
modelValue: 'cat', modelValue: 'cat',
formControl: el, formControl: el,

View file

@ -32,6 +32,48 @@ describe('Validator', () => {
expect(vali.config).to.eql({ my: 'config' }); expect(vali.config).to.eql({ my: 'config' });
}); });
it('has access to name, type, params, config in getMessage provided by config', () => {
const configSpy = sinon.spy();
class MyValidator extends Validator {
constructor(...args) {
super(...args);
this.name = 'MyValidator';
}
}
const vali = new MyValidator('myParam', { my: 'config', getMessage: configSpy });
vali._getMessage();
expect(configSpy.args[0][0]).to.deep.equal({
name: 'MyValidator',
type: 'error',
params: 'myParam',
config: { my: 'config', getMessage: configSpy },
});
});
it('has access to name, type, params, config in static get getMessage', () => {
let staticArgs;
class MyValidator extends Validator {
constructor(...args) {
super(...args);
this.name = 'MyValidator';
}
static getMessage(...args) {
staticArgs = args;
}
}
const vali = new MyValidator('myParam', { my: 'config' });
vali._getMessage();
expect(staticArgs[0]).to.deep.equal({
name: 'MyValidator',
type: 'error',
params: 'myParam',
config: { my: 'config' },
});
});
it('fires "param-changed" event on param change', async () => { it('fires "param-changed" event on param change', async () => {
const vali = new Validator('foo'); const vali = new Validator('foo');
const cb = sinon.spy(() => {}); const cb = sinon.spy(() => {});