From e057e9cf65a870670cf81b319f5c3b48a73b44aa Mon Sep 17 00:00:00 2001 From: Connor Meredith <4907463+connormeredith@users.noreply.github.com> Date: Tue, 19 Nov 2019 08:35:38 +0000 Subject: [PATCH] Added jest matcher support for .toHaveValue() --- README.md | 17 +++++++----- extend-expect.d.ts | 2 +- src/__tests__/to-have-value.js | 48 ++++++++++++++++++++++++++++++---- src/to-have-value.js | 10 ++----- 4 files changed, 56 insertions(+), 21 deletions(-) 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'