Skip to content

Commit 596488b

Browse files
committed
fix(fieldset): test validation after dynamic child removal/addition
1 parent 9ba6e02 commit 596488b

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

packages/fieldset/test/lion-fieldset.test.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import {
99
} from '@open-wc/testing';
1010
import sinon from 'sinon';
1111
import { localizeTearDown } from '@lion/localize/test-helpers.js';
12-
import '@lion/input/lion-input.js';
13-
1412
import '../lion-fieldset.js';
1513

1614
const tagString = 'lion-fieldset';
@@ -293,6 +291,38 @@ describe('<lion-fieldset>', () => {
293291
el.formElements.color.modelValue = 'cat';
294292
expect(el.error).to.deep.equal({});
295293
});
294+
295+
it('validates on children (de)registration', async () => {
296+
function hasEvenNumberOfChildren(modelValue) {
297+
return { even: Object.keys(modelValue).length % 2 === 0 };
298+
}
299+
const el = await fixture(html`
300+
<${tag} .errorValidators=${[[hasEvenNumberOfChildren]]}>
301+
<lion-input id="c1" name="c1"></lion-input>
302+
</${tag}>
303+
`);
304+
const child2 = await fixture(
305+
html`
306+
<lion-input name="c2"></lion-input>
307+
`,
308+
);
309+
310+
await nextFrame();
311+
expect(el.error.even).to.equal(true);
312+
313+
el.appendChild(child2);
314+
await nextFrame();
315+
expect(el.error.even).to.equal(undefined);
316+
317+
el.removeChild(child2);
318+
await nextFrame();
319+
expect(el.error.even).to.equal(true);
320+
321+
// Edge case: remove all children
322+
el.removeChild(el.querySelector('[id=c1]'));
323+
await nextFrame();
324+
expect(el.error.even).to.equal(undefined);
325+
});
296326
});
297327

298328
describe('interaction states', () => {

packages/validate/src/ValidateMixin.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,7 @@ export const ValidateMixin = dedupeMixin(
386386
*/
387387
validate() {
388388
if (this.modelValue === undefined) {
389-
this.constructor.validationTypes.forEach(type => {
390-
this[`${type}State`] = false;
391-
this[type] = {};
392-
});
389+
this.__resetValidationStates();
393390
return;
394391
}
395392
this.__oldValidationStates = this.getValidationStates();
@@ -399,6 +396,13 @@ export const ValidateMixin = dedupeMixin(
399396
this.dispatchEvent(new CustomEvent('validation-done', { bubbles: true, composed: true }));
400397
}
401398

399+
__resetValidationStates() {
400+
this.constructor.validationTypes.forEach(type => {
401+
this[`${type}State`] = false;
402+
this[type] = {};
403+
});
404+
}
405+
402406
/**
403407
* Override if needed
404408
*/

packages/validate/test/ValidateMixin.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ describe('ValidateMixin', () => {
513513
`);
514514

515515
el.modelValue = 'a';
516+
expect(el.errorState).to.equal(true);
516517
expect(el.warningState).to.equal(true);
517518
expect(el.infoState).to.equal(true);
518519
expect(el.successState).to.equal(true);

0 commit comments

Comments
 (0)