Skip to content

Commit

Permalink
fix: keep toHaveStyle up-to-date w/ jest-dom's
Browse files Browse the repository at this point in the history
  • Loading branch information
brrianalexis committed Nov 16, 2020
1 parent 2df860e commit 1483058
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/toHaveStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,23 @@ function parseJStoCSS(document, styles) {

function getStyleDeclaration(document, css) {
const styles = {};

// The next block is necessary to normalize colors
const copy = document.createElement('div');
Object.keys(css).forEach(property => {
copy.style[property] = css[property];
styles[property] = copy.style[property];
Object.keys(css).forEach(prop => {
copy.style[prop] = css[prop];
styles[prop] = copy.style[prop];
});

return styles;
}

function styleIsSubset(styles, computedStyle) {
return (
!!Object.keys(styles).length &&
Object.entries(styles).every(([prop, value]) => computedStyle.getPropertyValue(prop.toLowerCase()) === value)
Object.entries(styles).every(
([prop, value]) => computedStyle[prop] === value || computedStyle.getPropertyValue(prop.toLowerCase()) === value
)
);
}

Expand All @@ -58,7 +63,7 @@ function printoutStyles(styles) {

function expectedStyleDiff(expected, computedStyles) {
const received = Array.from(computedStyles)
.filter(prop => expected[prop])
.filter(prop => expected[prop] !== undefined)
.reduce(
(obj, prop) =>
Object.assign(obj, {
Expand Down

0 comments on commit 1483058

Please sign in to comment.