-
Notifications
You must be signed in to change notification settings - Fork 406
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
fix(toHaveStyle): strictly match empty values #276
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import {matcherHint} from 'jest-matcher-utils' | ||
import jestDiff from 'jest-diff' | ||
import chalk from 'chalk' | ||
import {checkHtmlElement, parseCSS, parseJStoCSS} from './utils' | ||
import {checkHtmlElement, parseCSS} from './utils' | ||
|
||
function getStyleDeclaration(document, css) { | ||
const styles = {} | ||
|
@@ -21,7 +21,8 @@ function isSubset(styles, computedStyle) { | |
!!Object.keys(styles).length && | ||
Object.entries(styles).every( | ||
([prop, value]) => | ||
computedStyle.getPropertyValue(prop.toLowerCase()) === value, | ||
computedStyle[prop] === value || | ||
computedStyle[prop.toLowerCase()] === value, | ||
) | ||
) | ||
} | ||
|
@@ -37,7 +38,7 @@ function printoutStyles(styles) { | |
// received computed styles | ||
function expectedDiff(expected, computedStyles) { | ||
const received = Array.from(computedStyles) | ||
.filter(prop => expected[prop]) | ||
.filter(prop => expected[prop] !== undefined) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed to get a proper diff for |
||
.reduce( | ||
(obj, prop) => | ||
Object.assign(obj, {[prop]: computedStyles.getPropertyValue(prop)}), | ||
|
@@ -51,14 +52,10 @@ function expectedDiff(expected, computedStyles) { | |
return diffOutput.replace(`${chalk.red('+ Received')}\n`, '') | ||
} | ||
|
||
function getCSStoParse(document, css) { | ||
return typeof css === 'object' ? parseJStoCSS(document, css) : css | ||
} | ||
|
||
export function toHaveStyle(htmlElement, css) { | ||
checkHtmlElement(htmlElement, toHaveStyle, this) | ||
const cssToParse = getCSStoParse(htmlElement.ownerDocument, css) | ||
const parsedCSS = parseCSS(cssToParse, toHaveStyle, this) | ||
const parsedCSS = | ||
typeof css === 'object' ? css : parseCSS(css, toHaveStyle, this) | ||
const {getComputedStyle} = htmlElement.ownerDocument.defaultView | ||
|
||
const expected = getStyleDeclaration(htmlElement.ownerDocument, parsedCSS) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,12 +192,6 @@ function compareArraysAsSet(a, b) { | |
return undefined | ||
} | ||
|
||
function parseJStoCSS(document, css) { | ||
const sandboxElement = document.createElement('div') | ||
Object.assign(sandboxElement.style, css) | ||
return sandboxElement.style.cssText | ||
} | ||
Comment on lines
-195
to
-199
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is currently not used anywhere else, so I decided to drop this completely |
||
|
||
function toSentence( | ||
array, | ||
{wordConnector = ', ', lastWordConnector = ' and '} = {}, | ||
|
@@ -218,6 +212,5 @@ export { | |
getTag, | ||
getSingleElementValue, | ||
compareArraysAsSet, | ||
parseJStoCSS, | ||
toSentence, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
computedStyle.getPropertyValue
supports only dash-cased notation. Property getters work for both dash and camelCase