Skip to content

Commit 47b04f6

Browse files
authored
fix: prevent duplicate error message announcement (#3182)
1 parent 1c38b4d commit 47b04f6

9 files changed

Lines changed: 38 additions & 23 deletions

File tree

‎packages/checkbox-group/test/checkbox-group.test.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, nextFrame } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync, nextFrame } from '@vaadin/testing-helpers';
33
import { sendKeys } from '@web/test-runner-commands';
44
import sinon from 'sinon';
55
import '@polymer/polymer/lib/elements/dom-bind.js';
@@ -461,8 +461,9 @@ describe('vaadin-checkbox-group', () => {
461461
expect(aria).to.include(label.id);
462462
});
463463

464-
it('should add error message to aria-labelledby when field is invalid', () => {
464+
it('should add error message to aria-labelledby when field is invalid', async () => {
465465
group.invalid = true;
466+
await aTimeout(0);
466467
const aria = group.getAttribute('aria-labelledby');
467468
expect(aria).to.include(helper.id);
468469
expect(aria).to.include(error.id);

‎packages/combo-box/test/aria.test.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { arrowDownKeyDown, escKeyDown, fixtureSync, nextFrame } from '@vaadin/testing-helpers';
2+
import { arrowDownKeyDown, aTimeout, escKeyDown, fixtureSync, nextFrame } from '@vaadin/testing-helpers';
33
import './not-animated-styles.js';
44
import '../vaadin-combo-box.js';
55
import { getAllItems } from './helpers.js';
@@ -39,8 +39,9 @@ describe('ARIA', () => {
3939
expect(aria).to.not.include(error.id);
4040
});
4141

42-
it('should add error message ID to aria-describedby when invalid', () => {
42+
it('should add error message ID to aria-describedby when invalid', async () => {
4343
comboBox.invalid = true;
44+
await aTimeout(0);
4445
const aria = input.getAttribute('aria-describedby');
4546
expect(aria).to.include(helper.id);
4647
expect(aria).to.include(error.id);

‎packages/custom-field/test/custom-field.test.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, focusin, focusout } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync, focusin, focusout } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import '../src/vaadin-custom-field.js';
55
import { dispatchChange } from './common.js';
@@ -205,8 +205,9 @@ describe('custom field', () => {
205205
expect(aria).to.include(label.id);
206206
});
207207

208-
it('should add error message to aria-labelledby when field is invalid', () => {
208+
it('should add error message to aria-labelledby when field is invalid', async () => {
209209
customField.invalid = true;
210+
await aTimeout(0);
210211
const aria = customField.getAttribute('aria-labelledby');
211212
expect(aria).to.include(helper.id);
212213
expect(aria).to.include(error.id);

‎packages/date-picker/test/wai-aria.test.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ describe('WAI-ARIA', () => {
2828
expect(aria).to.not.include(error.id);
2929
});
3030

31-
it('should add error message ID to aria-describedby when invalid', () => {
31+
it('should add error message ID to aria-describedby when invalid', async () => {
3232
datepicker.invalid = true;
33+
await aTimeout(0);
3334
const aria = input.getAttribute('aria-describedby');
3435
expect(aria).to.include(helper.id);
3536
expect(aria).to.include(error.id);

‎packages/date-time-picker/test/aria.test.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync } from '@vaadin/testing-helpers';
33
import '../vaadin-date-time-picker.js';
44

55
describe('ARIA', () => {
@@ -25,8 +25,9 @@ describe('ARIA', () => {
2525
expect(aria).to.include(label.id);
2626
});
2727

28-
it('should add error message to aria-labelledby when field is invalid', () => {
28+
it('should add error message to aria-labelledby when field is invalid', async () => {
2929
dateTimePicker.invalid = true;
30+
await aTimeout(0);
3031
const aria = dateTimePicker.getAttribute('aria-labelledby');
3132
expect(aria).to.include(helper.id);
3233
expect(aria).to.include(error.id);

‎packages/field-base/src/field-mixin.js‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,18 @@ export const FieldMixin = (superclass) =>
369369
* @protected
370370
*/
371371
_invalidChanged(invalid) {
372-
// Error message ID needs to be dynamically added / removed based on the validity
373-
// Otherwise assistive technologies would announce the error, even if we hide it.
374-
if (invalid) {
375-
this._fieldAriaController.setErrorId(this._errorId);
376-
} else {
377-
this._fieldAriaController.setErrorId(null);
378-
}
372+
// This timeout is needed to prevent NVDA from announcing the error message twice:
373+
// 1. Once adding the `[role=alert]` attribute by the `_updateErrorMessage` method (OK).
374+
// 2. Once linking the error ID with the ARIA target here (unwanted).
375+
// Related issue: https://github.com/vaadin/web-components/issues/3061.
376+
setTimeout(() => {
377+
// Error message ID needs to be dynamically added / removed based on the validity
378+
// Otherwise assistive technologies would announce the error, even if we hide it.
379+
if (invalid) {
380+
this._fieldAriaController.setErrorId(this._errorId);
381+
} else {
382+
this._fieldAriaController.setErrorId(null);
383+
}
384+
});
379385
}
380386
};

‎packages/field-base/test/field-mixin.test.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, nextFrame } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync, nextFrame } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
55
import { FieldMixin } from '../src/field-mixin.js';
@@ -578,8 +578,9 @@ describe('field-mixin', () => {
578578
expect(aria).to.equal(helper.id);
579579
});
580580

581-
it('should contain error id when the field is invalid', () => {
581+
it('should add error id asynchronously after the field becomes invalid', async () => {
582582
element.invalid = true;
583+
await aTimeout(0);
583584
const aria = input.getAttribute('aria-describedby');
584585
expect(aria).to.include(helper.id);
585586
expect(aria).to.include(error.id);
@@ -606,8 +607,9 @@ describe('field-mixin', () => {
606607
expect(aria).to.not.include(error.id);
607608
});
608609

609-
it('should contain error id when the field is invalid', () => {
610+
it('should add error id asynchronously after the field becomes invalid', async () => {
610611
element.invalid = true;
612+
await aTimeout(0);
611613
const aria = element.getAttribute('aria-labelledby');
612614
expect(aria).to.include(label.id);
613615
expect(aria).to.include(helper.id);

‎packages/radio-group/test/radio-group.test.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, nextFrame } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync, nextFrame } from '@vaadin/testing-helpers';
33
import { sendKeys } from '@web/test-runner-commands';
44
import sinon from 'sinon';
55
import '../vaadin-radio-group.js';
@@ -546,8 +546,9 @@ describe('radio-group', () => {
546546
expect(aria).to.include(label.id);
547547
});
548548

549-
it('should add error message to aria-labelledby when field is invalid', () => {
549+
it('should add error message to aria-labelledby when field is invalid', async () => {
550550
group.invalid = true;
551+
await aTimeout(0);
551552
const aria = group.getAttribute('aria-labelledby');
552553
expect(aria).to.include(helper.id);
553554
expect(aria).to.include(error.id);

‎packages/time-picker/test/aria.test.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { arrowDownKeyDown, escKeyDown, fixtureSync, nextFrame } from '@vaadin/testing-helpers';
2+
import { arrowDownKeyDown, aTimeout, escKeyDown, fixtureSync, nextFrame } from '@vaadin/testing-helpers';
33
import '../src/vaadin-time-picker.js';
44

55
describe('ARIA', () => {
@@ -45,8 +45,9 @@ describe('ARIA', () => {
4545
expect(aria).to.not.include(error.id);
4646
});
4747

48-
it('should add error message ID to aria-describedby when invalid', () => {
48+
it('should add error message ID to aria-describedby when invalid', async () => {
4949
timePicker.invalid = true;
50+
await aTimeout(0);
5051
const aria = input.getAttribute('aria-describedby');
5152
expect(aria).to.include(helper.id);
5253
expect(aria).to.include(error.id);

0 commit comments

Comments
 (0)