UNPKG

@lion/form-core

Version:

Form-core contains all essential building blocks for creating form fields and fieldsets

48 lines (44 loc) 1.91 kB
import { expect } from '@open-wc/testing'; import { ResultValidator } from '../../src/validate/ResultValidator.js'; import { DefaultSuccess } from '../../src/validate/resultValidators/DefaultSuccess.js'; import { Required } from '../../src/validate/validators/Required.js'; import { MinLength } from '../../src/validate/validators/StringValidators.js'; /** * @typedef {import('../../src/validate/Validator').Validator} Validator */ describe('ResultValidator', () => { it('has an "executeOnResults" function returning active state', async () => { // This test shows the best practice of creating executeOnResults method class MyResultValidator extends ResultValidator { /** * * @param {Object} context * @param {Validator[]} context.regularValidationResult * @param {Validator[]} context.prevValidationResult * @param {Validator[]} context.prevShownValidationResult * @returns {boolean} */ executeOnResults({ regularValidationResult, prevShownValidationResult }) { const errorOrWarning = /** @param {Validator} v */ v => v.type === 'error' || v.type === 'warning'; const hasErrorOrWarning = !!regularValidationResult.filter(errorOrWarning).length; const hasShownErrorOrWarning = !!prevShownValidationResult.filter(errorOrWarning).length; return !hasErrorOrWarning && hasShownErrorOrWarning; } } expect( new MyResultValidator().executeOnResults({ regularValidationResult: [new Required(), new MinLength(3)], prevValidationResult: [], prevShownValidationResult: [], }), ).to.be.false; expect( new MyResultValidator().executeOnResults({ regularValidationResult: [new DefaultSuccess()], prevValidationResult: [new Required()], prevShownValidationResult: [new Required()], }), ).to.be.true; }); });