diff --git a/README.md b/README.md index 166bfa4c..6f4fc9f6 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ clear to read and to maintain. + - [Installation](#installation) - [Usage](#usage) - [Custom matchers](#custom-matchers) @@ -86,9 +87,10 @@ should be installed as one of your project's `devDependencies`: npm install --save-dev @testing-library/jest-dom ``` -> Note: We also recommend installing the jest-dom eslint plugin which provides auto-fixable lint rules -> that prevent false positive tests and improve test readability by ensuring you are using the right -> matchers in your tests. More details can be found at +> Note: We also recommend installing the jest-dom eslint plugin which provides +> auto-fixable lint rules that prevent false positive tests and improve test +> readability by ensuring you are using the right matchers in your tests. More +> details can be found at > [eslint-plugin-jest-dom](https://github.com/testing-library/eslint-plugin-jest-dom). ## Usage @@ -610,7 +612,7 @@ toHaveAttribute(attr: string, value?: any) This allows you to check whether the given element has an attribute or not. You can also optionally check that the attribute has a specific expected value or partial match using -[expect.stringContaining](https://jestjs.io/docs/en/expect.html#expectnotstringcontainingstring)/[expect.stringMatching](https://jestjs.io/docs/en/expect.html#expectstringmatchingstring-regexp) +[expect.stringContaining](https://jestjs.io/docs/en/expect.html#expectstringcontainingstring)/[expect.stringMatching](https://jestjs.io/docs/en/expect.html#expectstringmatchingstring-regexp). #### Examples @@ -922,10 +924,12 @@ expect(element).not.toHaveTextContent('content') ### `toHaveValue` ```typescript -toHaveValue(value: string | string[] | number) +toHaveValue(value?: any) ``` -This allows you to check whether the given form element has the specified value. +This allows you to check whether the given form element has the specified value +or partial value using +[expect.stringContaining](https://jestjs.io/docs/en/expect.html#expectstringcontainingstring)/[expect.stringMatching](https://jestjs.io/docs/en/expect.html#expectstringmatchingstring-regexp). It accepts ``, ` `) expect(queryByTestId('textarea')).toHaveValue('text value') + expect(queryByTestId('textarea')).toHaveValue( + expect.stringContaining('text'), + ) + expect(queryByTestId('textarea')).toHaveValue( + expect.stringMatching(/^text/), + ) }) test('throws when passed checkbox or radio', () => { diff --git a/src/to-have-value.js b/src/to-have-value.js index d7e41d98..bc91339a 100644 --- a/src/to-have-value.js +++ b/src/to-have-value.js @@ -1,11 +1,5 @@ import {matcherHint} from 'jest-matcher-utils' -import isEqualWith from 'lodash/isEqualWith' -import { - checkHtmlElement, - compareArraysAsSet, - getMessage, - getSingleElementValue, -} from './utils' +import {checkHtmlElement, getMessage, getSingleElementValue} from './utils' export function toHaveValue(htmlElement, expectedValue) { checkHtmlElement(htmlElement, toHaveValue, this) @@ -23,7 +17,7 @@ export function toHaveValue(htmlElement, expectedValue) { const expectsValue = expectedValue !== undefined return { pass: expectsValue - ? isEqualWith(receivedValue, expectedValue, compareArraysAsSet) + ? this.equals(receivedValue, expectedValue) : Boolean(receivedValue), message: () => { const to = this.isNot ? 'not to' : 'to'