From c21eabbacddf6c3afb007e21dc69a46be5169d7b Mon Sep 17 00:00:00 2001 From: Thomas Allmer Date: Fri, 13 Dec 2019 12:38:51 +0100 Subject: [PATCH] fix(validate): allow getMessage to return message based on config --- packages/validate/src/Validator.js | 1 + .../ValidateMixinFeedbackPart.suite.js | 12 +++++- packages/validate/test/Validator.test.js | 42 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/packages/validate/src/Validator.js b/packages/validate/src/Validator.js index 5bcc8d848..b315934ef 100644 --- a/packages/validate/src/Validator.js +++ b/packages/validate/src/Validator.js @@ -55,6 +55,7 @@ export class Validator { name: this.name, type: this.type, params: this.param, + config: this.config, ...data, }; if (typeof this.config.getMessage === 'function') { diff --git a/packages/validate/test-suites/ValidateMixinFeedbackPart.suite.js b/packages/validate/test-suites/ValidateMixinFeedbackPart.suite.js index ac0052b6c..85150094c 100644 --- a/packages/validate/test-suites/ValidateMixinFeedbackPart.suite.js +++ b/packages/validate/test-suites/ValidateMixinFeedbackPart.suite.js @@ -402,6 +402,7 @@ export function runValidateMixinFeedbackPart() { await el.updateComplete; await el.feedbackComplete; expect(constructorMessageSpy.args[0][0]).to.eql({ + config: { type: 'x' }, params: 4, modelValue: 'cat', formControl: el, @@ -423,6 +424,9 @@ export function runValidateMixinFeedbackPart() { await el.updateComplete; await el.feedbackComplete; expect(instanceMessageSpy.args[0][0]).to.eql({ + config: { + getMessage: instanceMessageSpy, + }, params: 4, modelValue: 'cat', formControl: el, @@ -447,6 +451,7 @@ export function runValidateMixinFeedbackPart() { await el.updateComplete; await el.feedbackComplete; expect(spy.args[0][0]).to.eql({ + config: {}, params: 4, modelValue: 'cat', formControl: el, @@ -473,7 +478,12 @@ export function runValidateMixinFeedbackPart() { `); await el.updateComplete; 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, modelValue: 'cat', formControl: el, diff --git a/packages/validate/test/Validator.test.js b/packages/validate/test/Validator.test.js index ba8f336eb..ea5659498 100644 --- a/packages/validate/test/Validator.test.js +++ b/packages/validate/test/Validator.test.js @@ -32,6 +32,48 @@ describe('Validator', () => { 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 () => { const vali = new Validator('foo'); const cb = sinon.spy(() => {});