Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@testing-library/jest-dom] Add types for toHaveDisplayValue #44385

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 43 additions & 1 deletion types/testing-library__jest-dom/index.d.ts
@@ -1,4 +1,4 @@
// Type definitions for @testing-library/jest-dom 5.6
// Type definitions for @testing-library/jest-dom 5.7
// Project: https://github.com/testing-library/jest-dom
// Definitions by: Ernesto García <https://github.com/gnapse>
// John Gozde <https://github.com/jgoz>
Expand Down Expand Up @@ -251,6 +251,48 @@ declare namespace jest {
* [testing-library/jest-dom#tohaveclass](https:github.com/testing-library/jest-dom#tohaveclass)
*/
toHaveClass(...classNames: string[]): R;
/**
* @description
* This allows you to check whether the given form element has the specified displayed value (the one the
* end user will see). It accepts <input>, <select> and <textarea> elements with the exception of <input type="checkbox">
* and <input type="radio">, which can be meaningfully matched only using toBeChecked or toHaveFormValues.
* @example
* <label for="input-example">First name</label>
* <input type="text" id="input-example" value="Luca" />
*
* <label for="textarea-example">Description</label>
* <textarea id="textarea-example">An example description here.</textarea>
*
* <label for="single-select-example">Fruit</label>
* <select id="single-select-example">
* <option value="">Select a fruit...</option>
* <option value="banana">Banana</option>
* <option value="ananas">Ananas</option>
* <option value="avocado">Avocado</option>
* </select>
*
* <label for="mutiple-select-example">Fruits</label>
* <select id="multiple-select-example" multiple>
* <option value="">Select a fruit...</option>
* <option value="banana" selected>Banana</option>
* <option value="ananas">Ananas</option>
* <option value="avocado" selected>Avocado</option>
* </select>
*
* const input = screen.getByLabelText('First name')
* const textarea = screen.getByLabelText('Description')
* const selectSingle = screen.getByLabelText('Fruit')
* const selectMultiple = screen.getByLabelText('Fruits')
*
* expect(input).toHaveDisplayValue('Luca')
* expect(textarea).toHaveDisplayValue('An example description here.')
* expect(selectSingle).toHaveDisplayValue('Select a fruit...')
* expect(selectMultiple).toHaveDisplayValue(['Banana', 'Avocado'])
*
* @see
* [testing-library/jest-dom#tohavedisplayvalue](https:github.com/testing-library/jest-dom#tohavedisplayvalue)
*/
toHaveDisplayValue(value: string | RegExp | Array<string | RegExp>): R;
/**
* @description
* Assert whether an element has focus or not.
Expand Down
Expand Up @@ -19,6 +19,10 @@ expect(element).toHaveAttribute('attr', 'yes');
expect(element).toHaveClass();
expect(element).toHaveClass('cls1');
expect(element).toHaveClass('cls1', 'cls2', 'cls3', 'cls4');
expect(element).toHaveDisplayValue('str');
expect(element).toHaveDisplayValue(['str1', 'str2']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're still missing the tests to check that regex is valid anywhere a string could be valid

expect(element).toHaveDisplayValue(/str/);
expect(element).toHaveDisplayValue([/str1/, 'str2']);
expect(element).toHaveFocus();
expect(element).toHaveFormValues({ foo: 'bar', baz: 1 });
expect(element).toHaveStyle('display: block');
Expand Down Expand Up @@ -57,6 +61,10 @@ expect(element).not.toHaveAttribute('attr', 'yes');
expect(element).not.toHaveClass();
expect(element).not.toHaveClass('cls1');
expect(element).not.toHaveClass('cls1', 'cls2', 'cls3', 'cls4');
expect(element).not.toHaveDisplayValue('str');
expect(element).not.toHaveDisplayValue(['str1', 'str2']);
expect(element).not.toHaveDisplayValue(/str/);
expect(element).not.toHaveDisplayValue([/str1/, 'str2']);
expect(element).not.toHaveFocus();
expect(element).not.toHaveFormValues({ foo: 'bar', baz: 1 });
expect(element).not.toHaveStyle('display: block');
Expand Down