From 9c32d6e9c821e42660aa14c3f60a23eea477184e Mon Sep 17 00:00:00 2001 From: Isaiah Thomason <47364027+ITenthusiasm@users.noreply.github.com> Date: Mon, 17 Jul 2023 21:31:46 -0400 Subject: [PATCH] Add `toHaveAccessibleErrorMessage` to `@testing-library/jest-dom` Types Created in response to (and thus depends on): - testing-library/jest-dom#503 --- types/testing-library__jest-dom/index.d.ts | 2 +- types/testing-library__jest-dom/matchers.d.ts | 43 +++++++++++++++++++ ...library__jest-dom-extend-matchers-tests.ts | 11 +++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/types/testing-library__jest-dom/index.d.ts b/types/testing-library__jest-dom/index.d.ts index e33ba298a98ae0a..17be53259f5b870 100644 --- a/types/testing-library__jest-dom/index.d.ts +++ b/types/testing-library__jest-dom/index.d.ts @@ -17,6 +17,6 @@ declare global { } } -declare module "expect" { +declare module 'expect' { interface Matchers extends TestingLibraryMatchers {} } diff --git a/types/testing-library__jest-dom/matchers.d.ts b/types/testing-library__jest-dom/matchers.d.ts index fad80008842f44e..a059ddb9fb9aea8 100644 --- a/types/testing-library__jest-dom/matchers.d.ts +++ b/types/testing-library__jest-dom/matchers.d.ts @@ -503,6 +503,47 @@ declare namespace matchers { * [testing-library/jest-dom#tohaveaccessibledescription](https://github.com/testing-library/jest-dom#tohaveaccessibledescription) */ toHaveAccessibleDescription(text?: string | RegExp | E): R; + + /** + * @description + * This allows you to assert that an element has the expected + * [accessible error message](https://w3c.github.io/aria/#aria-errormessage). + * + * You can pass the exact string of the expected accessible error message. + * Alternatively, you can perform a partial match by passing a regular expression + * or by using either + * [expect.stringContaining](https://jestjs.io/docs/en/expect.html#expectnotstringcontainingstring) + * or [expect.stringMatching](https://jestjs.io/docs/en/expect.html#expectstringmatchingstring-regexp). + * + * @example + * + * + * + * + * + * + * // Inputs with Valid Error Messages + * expect(getByRole('textbox', {name: 'Has Error'})).toHaveAccessibleErrorMessage() + * expect(getByRole('textbox', {name: 'Has Error'})).toHaveAccessibleErrorMessage('This field is invalid') + * expect(getByRole('textbox', {name: 'Has Error'})).toHaveAccessibleErrorMessage(/invalid/i) + * expect( + * getByRole('textbox', {name: 'Has Error'}), + * ).not.toHaveAccessibleErrorMessage('This field is absolutely correct!') + * + * // Inputs without Valid Error Messages + * expect( + * getByRole('textbox', {name: 'No Error Attributes'}), + * ).not.toHaveAccessibleErrorMessage() + * + * expect( + * getByRole('textbox', {name: 'Not Invalid'}), + * ).not.toHaveAccessibleErrorMessage() + * + * @see + * [testing-library/jest-dom#tohaveaccessibleerrormessage](https://github.com/testing-library/jest-dom#tohaveaccessibleerrormessage) + */ + toHaveAccessibleErrorMessage(text?: string | RegExp | E): R; + /** * @description * This allows to assert that an element has the expected [accessible name](https://w3c.github.io/accname/). @@ -573,6 +614,8 @@ declare namespace matchers { */ toBePartiallyChecked(): R; /** + * @deprecated + * * @description * * Check whether the given element has an [ARIA error message](https://www.w3.org/TR/wai-aria/#aria-errormessage) or not. diff --git a/types/testing-library__jest-dom/test/testing-library__jest-dom-extend-matchers-tests.ts b/types/testing-library__jest-dom/test/testing-library__jest-dom-extend-matchers-tests.ts index 8453d35669c024e..19f0d4487979494 100644 --- a/types/testing-library__jest-dom/test/testing-library__jest-dom-extend-matchers-tests.ts +++ b/types/testing-library__jest-dom/test/testing-library__jest-dom-extend-matchers-tests.ts @@ -56,6 +56,12 @@ customExpect(element).toHaveAccessibleDescription('some description'); customExpect(element).toHaveAccessibleDescription(/some description/); customExpect(element).toHaveAccessibleDescription(expect.stringContaining('partial')); customExpect(element).toHaveAccessibleDescription(); + +customExpect(element).toHaveAccessibleErrorMessage(); +customExpect(element).toHaveAccessibleErrorMessage('Invalid time: the time must be between 9:00 AM and 5:00 PM'); +customExpect(element).toHaveAccessibleErrorMessage(/invalid time/i); +customExpect(element).toHaveAccessibleErrorMessage(expect.stringContaining('Invalid time')); + customExpect(element).toHaveAccessibleName('a label'); customExpect(element).toHaveAccessibleName(/a label/); customExpect(element).toHaveAccessibleName(expect.stringContaining('partial label')); @@ -105,6 +111,11 @@ customExpect(element).not.toHaveDescription('some description'); customExpect(element).not.toHaveDescription(); customExpect(element).not.toHaveAccessibleDescription('some description'); customExpect(element).not.toHaveAccessibleDescription(); + +customExpect(element).not.toHaveAccessibleErrorMessage(); +customExpect(element).not.toHaveAccessibleErrorMessage('There is no date'); +customExpect(element).not.toHaveAccessibleErrorMessage(/everything is valid/i); + customExpect(element).not.toHaveAccessibleName('a label'); customExpect(element).not.toHaveAccessibleName(); customExpect(element).not.toBePartiallyChecked();