@testing-library/jest-dom
Advanced tools
Comparing version 5.2.0 to 5.3.0
@@ -12,2 +12,22 @@ "use strict"; | ||
function getExpectedClassNamesAndOptions(params) { | ||
const lastParam = params.pop(); | ||
let expectedClassNames, options; | ||
if (typeof lastParam === 'object') { | ||
expectedClassNames = params; | ||
options = lastParam; | ||
} else { | ||
expectedClassNames = params.concat(lastParam); | ||
options = { | ||
exact: false | ||
}; | ||
} | ||
return { | ||
expectedClassNames, | ||
options | ||
}; | ||
} | ||
function splitClassNames(str) { | ||
@@ -25,6 +45,21 @@ if (!str) { | ||
function toHaveClass(htmlElement, ...expectedClassNames) { | ||
function toHaveClass(htmlElement, ...params) { | ||
(0, _utils.checkHtmlElement)(htmlElement, toHaveClass, this); | ||
const { | ||
expectedClassNames, | ||
options | ||
} = getExpectedClassNamesAndOptions(params); | ||
const received = splitClassNames(htmlElement.getAttribute('class')); | ||
const expected = expectedClassNames.reduce((acc, className) => acc.concat(splitClassNames(className)), []); | ||
if (options.exact) { | ||
return { | ||
pass: isSubset(expected, received) && expected.length === received.length, | ||
message: () => { | ||
const to = this.isNot ? 'not to' : 'to'; | ||
return (0, _utils.getMessage)(`Expected the element ${to} have EXACTLY defined classes`, expected.join(' '), 'Received', received.join(' ')); | ||
} | ||
}; | ||
} | ||
return expected.length > 0 ? { | ||
@@ -31,0 +66,0 @@ pass: isSubset(expected, received), |
{ | ||
"name": "@testing-library/jest-dom", | ||
"version": "5.2.0", | ||
"version": "5.3.0", | ||
"description": "Custom jest matchers to test the state of the DOM", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -49,2 +49,3 @@ <div align="center"> | ||
- [Installation](#installation) | ||
@@ -502,3 +503,3 @@ - [Usage](#usage) | ||
```typescript | ||
toHaveClass(...classNames: string[]) | ||
toHaveClass(...classNames: string[], options?: {exact: boolean}) | ||
``` | ||
@@ -530,2 +531,5 @@ | ||
expect(deleteButton).toHaveClass('btn-danger extra btn', {exact: true}) // to check if the element has EXACTLY a set of classes | ||
expect(deleteButton).not.toHaveClass('btn-danger extra', {exact: true}) // if it has more than expected it is going to fail | ||
expect(noClasses).not.toHaveClass() | ||
@@ -946,2 +950,3 @@ ``` | ||
<!-- prettier-ignore-end --> | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
@@ -948,0 +953,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83647
858
998