From 1483058ca90bd04ed6c9066ccdad3d876eee81ec Mon Sep 17 00:00:00 2001 From: Brian Alexis <51463930+brrianalexis@users.noreply.github.com> Date: Mon, 16 Nov 2020 01:11:04 -0300 Subject: [PATCH] fix: keep toHaveStyle up-to-date w/ jest-dom's --- src/toHaveStyle.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/toHaveStyle.js b/src/toHaveStyle.js index e24a13e..d6d1485 100644 --- a/src/toHaveStyle.js +++ b/src/toHaveStyle.js @@ -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 + ) ); } @@ -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, {