diff --git a/.all-contributorsrc b/.all-contributorsrc index 2a1e3a19..6b0ccf55 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -238,6 +238,15 @@ "bug", "code" ] + }, + { + "login": "revathskumar", + "name": "Revath S Kumar", + "avatar_url": "https://avatars3.githubusercontent.com/u/463904?v=4", + "profile": "https://blog.revathskumar.com", + "contributions": [ + "code" + ] } ] } diff --git a/README.md b/README.md index 0ee33c31..26226363 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ [![downloads][downloads-badge]][npmtrends] [![MIT License][license-badge]][license] -[![All Contributors](https://img.shields.io/badge/all_contributors-23-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-24-orange.svg?style=flat-square)](#contributors) [![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc] @@ -381,11 +381,11 @@ expect(getByTestId(container, 'parent')).toContainHTML( ### `toHaveAttribute` ```typescript -toHaveAttribute(attr: string, value?: string) +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. +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) #### Examples @@ -411,6 +411,9 @@ const button = getByTestId(container, 'ok-button') expect(button).toHaveAttribute('disabled') expect(button).toHaveAttribute('type', 'submit') expect(button).not.toHaveAttribute('type', 'button') + +expect(button).toHaveAttribute('type', expect.stringContaining('sub')) +expect(button).toHaveAttribute('type', expect.not.stringContaining('but')) ```
@@ -760,8 +763,7 @@ Thanks goes to these people ([emoji key][emojis]): | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | [Anto Aravinth
Anto Aravinth](https://github.com/antoaravinth)
[πŸ’»](https://github.com/testing-library/jest-dom/commits?author=antoaravinth "Code") [⚠️](https://github.com/testing-library/jest-dom/commits?author=antoaravinth "Tests") [πŸ“–](https://github.com/testing-library/jest-dom/commits?author=antoaravinth "Documentation") | [Jonah Moses
Jonah Moses](https://github.com/JonahMoses)
[πŸ“–](https://github.com/testing-library/jest-dom/commits?author=JonahMoses "Documentation") | [Łukasz Gandecki
Łukasz Gandecki](http://team.thebrain.pro)
[πŸ’»](https://github.com/testing-library/jest-dom/commits?author=lgandecki "Code") [⚠️](https://github.com/testing-library/jest-dom/commits?author=lgandecki "Tests") [πŸ“–](https://github.com/testing-library/jest-dom/commits?author=lgandecki "Documentation") | [Ivan Babak
Ivan Babak](https://sompylasar.github.io)
[πŸ›](https://github.com/testing-library/jest-dom/issues?q=author%3Asompylasar "Bug reports") [πŸ€”](#ideas-sompylasar "Ideas, Planning, & Feedback") | [Jesse Day
Jesse Day](https://github.com/jday3)
[πŸ’»](https://github.com/testing-library/jest-dom/commits?author=jday3 "Code") | [Ernesto GarcΓ­a
Ernesto GarcΓ­a](http://gnapse.github.io)
[πŸ’»](https://github.com/testing-library/jest-dom/commits?author=gnapse "Code") [πŸ“–](https://github.com/testing-library/jest-dom/commits?author=gnapse "Documentation") [⚠️](https://github.com/testing-library/jest-dom/commits?author=gnapse "Tests") | [Mark Volkmann
Mark Volkmann](http://ociweb.com/mark/)
[πŸ›](https://github.com/testing-library/jest-dom/issues?q=author%3Amvolkmann "Bug reports") [πŸ’»](https://github.com/testing-library/jest-dom/commits?author=mvolkmann "Code") | | [smacpherson64
smacpherson64](https://github.com/smacpherson64)
[πŸ’»](https://github.com/testing-library/jest-dom/commits?author=smacpherson64 "Code") [πŸ“–](https://github.com/testing-library/jest-dom/commits?author=smacpherson64 "Documentation") [⚠️](https://github.com/testing-library/jest-dom/commits?author=smacpherson64 "Tests") | [John Gozde
John Gozde](https://github.com/jgoz)
[πŸ›](https://github.com/testing-library/jest-dom/issues?q=author%3Ajgoz "Bug reports") [πŸ’»](https://github.com/testing-library/jest-dom/commits?author=jgoz "Code") | [Iwona
Iwona](https://github.com/callada)
[πŸ’»](https://github.com/testing-library/jest-dom/commits?author=callada "Code") [πŸ“–](https://github.com/testing-library/jest-dom/commits?author=callada "Documentation") [⚠️](https://github.com/testing-library/jest-dom/commits?author=callada "Tests") | [Lewis
Lewis](https://github.com/6ewis)
[πŸ’»](https://github.com/testing-library/jest-dom/commits?author=6ewis "Code") | [Leandro Lourenci
Leandro Lourenci](https://blog.lourenci.com/)
[πŸ›](https://github.com/testing-library/jest-dom/issues?q=author%3Alourenci "Bug reports") [πŸ“–](https://github.com/testing-library/jest-dom/commits?author=lourenci "Documentation") [πŸ’»](https://github.com/testing-library/jest-dom/commits?author=lourenci "Code") [⚠️](https://github.com/testing-library/jest-dom/commits?author=lourenci "Tests") | [Shukhrat Mukimov
Shukhrat Mukimov](https://github.com/mufasa71)
[πŸ›](https://github.com/testing-library/jest-dom/issues?q=author%3Amufasa71 "Bug reports") | [Roman Usherenko
Roman Usherenko](https://github.com/dreyks)
[πŸ’»](https://github.com/testing-library/jest-dom/commits?author=dreyks "Code") [⚠️](https://github.com/testing-library/jest-dom/commits?author=dreyks "Tests") | -| [Joe Hsu
Joe Hsu](http://josephhsu.com)
[πŸ“–](https://github.com/testing-library/jest-dom/commits?author=jhsu "Documentation") | [Haz
Haz](https://twitter.com/diegohaz)
[πŸ›](https://github.com/testing-library/jest-dom/issues?q=author%3Adiegohaz "Bug reports") [πŸ’»](https://github.com/testing-library/jest-dom/commits?author=diegohaz "Code") | - +| [Joe Hsu
Joe Hsu](http://josephhsu.com)
[πŸ“–](https://github.com/testing-library/jest-dom/commits?author=jhsu "Documentation") | [Haz
Haz](https://twitter.com/diegohaz)
[πŸ›](https://github.com/testing-library/jest-dom/issues?q=author%3Adiegohaz "Bug reports") [πŸ’»](https://github.com/testing-library/jest-dom/commits?author=diegohaz "Code") | [Revath S Kumar
Revath S Kumar](https://blog.revathskumar.com)
[πŸ’»](https://github.com/testing-library/jest-dom/commits?author=revathskumar "Code") | This project follows the [all-contributors][all-contributors] specification. diff --git a/extend-expect.d.ts b/extend-expect.d.ts index 5115b2a5..a65bf554 100644 --- a/extend-expect.d.ts +++ b/extend-expect.d.ts @@ -1,4 +1,24 @@ declare namespace jest { + interface InverseStringAsymmetricMatchers { + stringMatching(str: string | RegExp): any + stringContaining(str: string): any + } + interface Expect { + stringMatching(str: string | RegExp): any + stringContaining(str: string): any + anything(): any + any(classType: any): any + not: InverseStringAsymmetricMatchers + } + + type attributeValueType = + | string + | Expect.stringContaining + | Expect.stringMatching + | Expect.any + | Expect.anything + | Expect.not + interface Matchers { /** * @deprecated @@ -11,7 +31,7 @@ declare namespace jest { toBeEnabled(): R toContainElement(element: HTMLElement | SVGElement | null): R toContainHTML(htmlText: string): R - toHaveAttribute(attr: string, value?: string): R + toHaveAttribute(attr: string, value?: attributeValueType): R toHaveClass(...classNames: string[]): R toHaveFocus(): R toHaveFormValues(expectedValues: {[name: string]: any}): R diff --git a/package.json b/package.json index f8f704e0..ef5f1162 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "chalk": "^2.4.1", "css": "^2.2.3", "css.escape": "^1.5.1", + "expect": "^24.7.1", "jest-diff": "^24.0.0", "jest-matcher-utils": "^24.0.0", "lodash": "^4.17.11", diff --git a/src/__tests__/to-have-attribute.js b/src/__tests__/to-have-attribute.js index 8110b0c7..0530f3a6 100644 --- a/src/__tests__/to-have-attribute.js +++ b/src/__tests__/to-have-attribute.js @@ -41,4 +41,22 @@ test('.toHaveAttribute', () => { expect(() => expect({thisIsNot: 'an html element'}).not.toHaveAttribute(), ).toThrowError() + + // Asymettic matchers + expect(queryByTestId('ok-button')).toHaveAttribute( + 'type', + expect.stringContaining('sub'), + ) + expect(queryByTestId('ok-button')).toHaveAttribute( + 'type', + expect.stringMatching(/sub*/), + ) + expect(queryByTestId('ok-button')).toHaveAttribute('type', expect.anything()) + + expect(() => + expect(queryByTestId('ok-button')).toHaveAttribute( + 'type', + expect.not.stringContaining('sub'), + ), + ).toThrowError() }) diff --git a/src/to-have-attribute.js b/src/to-have-attribute.js index 0dbba11f..0627cf35 100644 --- a/src/to-have-attribute.js +++ b/src/to-have-attribute.js @@ -1,4 +1,5 @@ import {matcherHint, stringify, printExpected} from 'jest-matcher-utils' +import {equals} from 'expect/build/jasmineUtils' import {checkHtmlElement, getMessage} from './utils' function printAttribute(name, value) { @@ -18,7 +19,7 @@ export function toHaveAttribute(htmlElement, name, expectedValue) { const receivedValue = htmlElement.getAttribute(name) return { pass: isExpectedValuePresent - ? hasAttribute && receivedValue === expectedValue + ? hasAttribute && equals(receivedValue, expectedValue) : hasAttribute, message: () => { const to = this.isNot ? 'not to' : 'to'