chore: revert prevValidation result, make prevShown Validator[]

This commit is contained in:
Joren Broekema 2021-03-15 10:46:08 +01:00
parent af90b20ed0
commit 1d97721769
7 changed files with 54 additions and 29 deletions

View file

@ -2,6 +2,7 @@ node_modules
coverage/ coverage/
bundlesize/ bundlesize/
.history/ .history/
storybook-static/
*.d.ts *.d.ts
_site-dev _site-dev
_site _site

View file

@ -10,12 +10,20 @@ export class ResultValidator extends Validator {
/** /**
* @param {Object} context * @param {Object} context
* @param {Validator[]} context.regularValidationResult * @param {Validator[]} context.regularValidationResult
* @param {string} context.prevShownValidationFeedback * @param {Validator[]} context.prevValidationResult
* @param {Validator[]} context.prevShownValidationResult
* @param {Validator[]} [context.validators] * @param {Validator[]} [context.validators]
* @returns {boolean} * @returns {boolean}
*/ */
// eslint-disable-next-line no-unused-vars, class-methods-use-this /* eslint-disable no-unused-vars */
executeOnResults({ regularValidationResult, prevShownValidationFeedback, validators }) { // eslint-disable-next-line class-methods-use-this
executeOnResults({
regularValidationResult,
prevValidationResult,
prevShownValidationResult,
validators,
}) {
/* eslint-enable no-unused-vars */
return true; return true;
} }
} }

View file

@ -161,8 +161,10 @@ export const ValidateMixinImplementation = superclass =>
* @type {Validator[]} * @type {Validator[]}
*/ */
this.__validationResult = []; this.__validationResult = [];
/** @type {string} */ /** @type {Validator[]} */
this.__prevShownValidationFeedback = ''; this.__prevValidationResult = [];
/** @type {Validator[]} */
this.__prevShownValidationResult = [];
this.__onValidatorUpdated = this.__onValidatorUpdated.bind(this); this.__onValidatorUpdated = this.__onValidatorUpdated.bind(this);
this._updateFeedbackComponent = this._updateFeedbackComponent.bind(this); this._updateFeedbackComponent = this._updateFeedbackComponent.bind(this);
@ -279,6 +281,7 @@ export const ValidateMixinImplementation = superclass =>
return; return;
} }
this.__prevValidationResult = this.__validationResult;
if (clearCurrentResult) { if (clearCurrentResult) {
// Clear ('invalidate') all pending and existing validation results. // Clear ('invalidate') all pending and existing validation results.
// This is needed because we have async (pending) validators whose results // This is needed because we have async (pending) validators whose results
@ -396,7 +399,8 @@ export const ValidateMixinImplementation = superclass =>
return resultValidators.filter(v => return resultValidators.filter(v =>
v.executeOnResults({ v.executeOnResults({
regularValidationResult, regularValidationResult,
prevShownValidationFeedback: this.__prevShownValidationFeedback, prevValidationResult: this.__prevValidationResult,
prevShownValidationResult: this.__prevShownValidationResult,
}), }),
); );
} }
@ -579,7 +583,7 @@ export const ValidateMixinImplementation = superclass =>
}); });
if (this.__prioritizedResult.length > 0) { if (this.__prioritizedResult.length > 0) {
this.__prevShownValidationFeedback = this.__prioritizedResult[0].type; this.__prevShownValidationResult = this.__prioritizedResult;
} }
const messageMap = await this.__getFeedbackMessages(this.__prioritizedResult); const messageMap = await this.__getFeedbackMessages(this.__prioritizedResult);

View file

@ -17,18 +17,17 @@ export class DefaultSuccess extends ResultValidator {
* *
* @param {Object} context * @param {Object} context
* @param {Validator[]} context.regularValidationResult * @param {Validator[]} context.regularValidationResult
* @param {string} context.prevShownValidationFeedback * @param {Validator[]} context.prevValidationResult
* @param {Validator[]} context.prevShownValidationResult
* @returns {boolean} * @returns {boolean}
*/ */
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this
executeOnResults({ regularValidationResult, prevShownValidationFeedback }) { executeOnResults({ regularValidationResult, prevShownValidationResult }) {
const errorOrWarning = /** @param {Validator} v */ v => const errorOrWarning = /** @param {Validator} v */ v =>
v.type === 'error' || v.type === 'warning'; v.type === 'error' || v.type === 'warning';
const hasErrorOrWarning = !!regularValidationResult.filter(errorOrWarning).length; const hasErrorOrWarning = !!regularValidationResult.filter(errorOrWarning).length;
const hasShownErrorOrWarning = !!prevShownValidationResult.filter(errorOrWarning).length;
return ( return !hasErrorOrWarning && hasShownErrorOrWarning;
!hasErrorOrWarning &&
(prevShownValidationFeedback === 'error' || prevShownValidationFeedback === 'warning')
);
} }
} }

View file

@ -550,19 +550,16 @@ export function runValidateMixinSuite(customConfig) {
* *
* @param {Object} context * @param {Object} context
* @param {Validator[]} context.regularValidationResult * @param {Validator[]} context.regularValidationResult
* @param {string} context.prevShownValidationFeedback * @param {Validator[]} context.prevShownValidationResult
* @returns {boolean} * @returns {boolean}
*/ */
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this
executeOnResults({ regularValidationResult, prevShownValidationFeedback }) { executeOnResults({ regularValidationResult, prevShownValidationResult }) {
const errorOrWarning = /** @param {Validator} v */ v => const errorOrWarning = /** @param {Validator} v */ v =>
v.type === 'error' || v.type === 'warning'; v.type === 'error' || v.type === 'warning';
const hasErrorOrWarning = !!regularValidationResult.filter(errorOrWarning).length; const hasErrorOrWarning = !!regularValidationResult.filter(errorOrWarning).length;
const hasShownErrorOrWarning = !!prevShownValidationResult.filter(errorOrWarning).length;
return ( return !hasErrorOrWarning && hasShownErrorOrWarning;
!hasErrorOrWarning &&
(prevShownValidationFeedback === 'error' || prevShownValidationFeedback === 'warning')
);
} }
}; };
@ -613,7 +610,8 @@ export function runValidateMixinSuite(customConfig) {
.modelValue=${'myValue'} .modelValue=${'myValue'}
>${lightDom}</${withSuccessTag}> >${lightDom}</${withSuccessTag}>
`)); `));
const prevShownValidationFeedback = el.__prevShownValidationFeedback; const prevValidationResult = el.__prevValidationResult;
const prevShownValidationResult = el.__prevShownValidationResult;
const regularValidationResult = [ const regularValidationResult = [
...el.__syncValidationResult, ...el.__syncValidationResult,
...el.__asyncValidationResult, ...el.__asyncValidationResult,
@ -621,7 +619,8 @@ export function runValidateMixinSuite(customConfig) {
expect(resultValidateSpy.args[0][0]).to.eql({ expect(resultValidateSpy.args[0][0]).to.eql({
regularValidationResult, regularValidationResult,
prevShownValidationFeedback, prevValidationResult,
prevShownValidationResult,
}); });
}); });

View file

@ -1,5 +1,6 @@
import { expect } from '@open-wc/testing'; import { expect } from '@open-wc/testing';
import { ResultValidator } from '../../src/validate/ResultValidator.js'; import { ResultValidator } from '../../src/validate/ResultValidator.js';
import { DefaultSuccess } from '../../src/validate/resultValidators/DefaultSuccess.js';
import { Required } from '../../src/validate/validators/Required.js'; import { Required } from '../../src/validate/validators/Required.js';
import { MinLength } from '../../src/validate/validators/StringValidators.js'; import { MinLength } from '../../src/validate/validators/StringValidators.js';
@ -15,19 +16,31 @@ describe('ResultValidator', () => {
* *
* @param {Object} context * @param {Object} context
* @param {Validator[]} context.regularValidationResult * @param {Validator[]} context.regularValidationResult
* @param {string} context.prevShownValidationFeedback * @param {Validator[]} context.prevValidationResult
* @param {Validator[]} context.prevShownValidationResult
* @returns {boolean} * @returns {boolean}
*/ */
executeOnResults({ regularValidationResult, prevShownValidationFeedback }) { executeOnResults({ regularValidationResult, prevShownValidationResult }) {
const hasSuccess = const errorOrWarning = /** @param {Validator} v */ v =>
regularValidationResult.length && prevShownValidationFeedback === 'error'; v.type === 'error' || v.type === 'warning';
return !!hasSuccess; const hasErrorOrWarning = !!regularValidationResult.filter(errorOrWarning).length;
const hasShownErrorOrWarning = !!prevShownValidationResult.filter(errorOrWarning).length;
return !hasErrorOrWarning && hasShownErrorOrWarning;
} }
} }
expect( expect(
new MyResultValidator().executeOnResults({ new MyResultValidator().executeOnResults({
regularValidationResult: [new Required(), new MinLength(3)], regularValidationResult: [new Required(), new MinLength(3)],
prevShownValidationFeedback: 'error', prevValidationResult: [],
prevShownValidationResult: [],
}),
).to.be.false;
expect(
new MyResultValidator().executeOnResults({
regularValidationResult: [new DefaultSuccess()],
prevValidationResult: [new Required()],
prevShownValidationResult: [new Required()],
}), }),
).to.be.true; ).to.be.true;
}); });

View file

@ -37,7 +37,8 @@ export declare class ValidateHost {
__syncValidationResult: Validator[]; __syncValidationResult: Validator[];
__asyncValidationResult: Validator[]; __asyncValidationResult: Validator[];
__validationResult: Validator[]; __validationResult: Validator[];
__prevShownValidationFeedback: string; __prevValidationResult: Validator[];
__prevShownValidationResult: Validator[];
connectedCallback(): void; connectedCallback(): void;
disconnectedCallback(): void; disconnectedCallback(): void;