fix(fieldset): test validation after dynamic child removal/addition
This commit is contained in:
parent
9ba6e02f29
commit
596488bbae
3 changed files with 41 additions and 6 deletions
|
|
@ -9,8 +9,6 @@ import {
|
|||
} from '@open-wc/testing';
|
||||
import sinon from 'sinon';
|
||||
import { localizeTearDown } from '@lion/localize/test-helpers.js';
|
||||
import '@lion/input/lion-input.js';
|
||||
|
||||
import '../lion-fieldset.js';
|
||||
|
||||
const tagString = 'lion-fieldset';
|
||||
|
|
@ -293,6 +291,38 @@ describe('<lion-fieldset>', () => {
|
|||
el.formElements.color.modelValue = 'cat';
|
||||
expect(el.error).to.deep.equal({});
|
||||
});
|
||||
|
||||
it('validates on children (de)registration', async () => {
|
||||
function hasEvenNumberOfChildren(modelValue) {
|
||||
return { even: Object.keys(modelValue).length % 2 === 0 };
|
||||
}
|
||||
const el = await fixture(html`
|
||||
<${tag} .errorValidators=${[[hasEvenNumberOfChildren]]}>
|
||||
<lion-input id="c1" name="c1"></lion-input>
|
||||
</${tag}>
|
||||
`);
|
||||
const child2 = await fixture(
|
||||
html`
|
||||
<lion-input name="c2"></lion-input>
|
||||
`,
|
||||
);
|
||||
|
||||
await nextFrame();
|
||||
expect(el.error.even).to.equal(true);
|
||||
|
||||
el.appendChild(child2);
|
||||
await nextFrame();
|
||||
expect(el.error.even).to.equal(undefined);
|
||||
|
||||
el.removeChild(child2);
|
||||
await nextFrame();
|
||||
expect(el.error.even).to.equal(true);
|
||||
|
||||
// Edge case: remove all children
|
||||
el.removeChild(el.querySelector('[id=c1]'));
|
||||
await nextFrame();
|
||||
expect(el.error.even).to.equal(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('interaction states', () => {
|
||||
|
|
|
|||
|
|
@ -386,10 +386,7 @@ export const ValidateMixin = dedupeMixin(
|
|||
*/
|
||||
validate() {
|
||||
if (this.modelValue === undefined) {
|
||||
this.constructor.validationTypes.forEach(type => {
|
||||
this[`${type}State`] = false;
|
||||
this[type] = {};
|
||||
});
|
||||
this.__resetValidationStates();
|
||||
return;
|
||||
}
|
||||
this.__oldValidationStates = this.getValidationStates();
|
||||
|
|
@ -399,6 +396,13 @@ export const ValidateMixin = dedupeMixin(
|
|||
this.dispatchEvent(new CustomEvent('validation-done', { bubbles: true, composed: true }));
|
||||
}
|
||||
|
||||
__resetValidationStates() {
|
||||
this.constructor.validationTypes.forEach(type => {
|
||||
this[`${type}State`] = false;
|
||||
this[type] = {};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Override if needed
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -513,6 +513,7 @@ describe('ValidateMixin', () => {
|
|||
`);
|
||||
|
||||
el.modelValue = 'a';
|
||||
expect(el.errorState).to.equal(true);
|
||||
expect(el.warningState).to.equal(true);
|
||||
expect(el.infoState).to.equal(true);
|
||||
expect(el.successState).to.equal(true);
|
||||
|
|
|
|||
Loading…
Reference in a new issue