-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: toHaveStyle custom matcher (#12)
* feature: toHaveStyle custom matcher * Fix test coverage * Use more robust css parser library * Handle css parsing errors gracefully * Improve how printed out styles look like in failing tests messages * Add documentation to the README * Use redent for indent
- Loading branch information
Showing
7 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,11 @@ | |
"author": "Ernesto Garcia <[email protected]> (http://gnapse.github.io/)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"jest-matcher-utils": "^22.4.3" | ||
"chalk": "^2.4.1", | ||
"css": "^2.2.3", | ||
"jest-diff": "^22.4.3", | ||
"jest-matcher-utils": "^22.4.3", | ||
"redent": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"kcd-scripts": "^0.37.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import {parse} from 'css' | ||
import {matcherHint} from 'jest-matcher-utils' | ||
import jestDiff from 'jest-diff' | ||
import chalk from 'chalk' | ||
import {checkHtmlElement} from './utils' | ||
|
||
function parseCSS(css) { | ||
const ast = parse(`selector { ${css} }`, {silent: true}).stylesheet | ||
if (ast.parsingErrors && ast.parsingErrors.length > 0) { | ||
const {reason, line, column} = ast.parsingErrors[0] | ||
return { | ||
parsingError: `Syntax error parsing expected css: ${reason} in ${line}:${column}`, | ||
} | ||
} | ||
const parsedRules = ast.rules[0].declarations | ||
.filter(d => d.type === 'declaration') | ||
.reduce( | ||
(obj, {property, value}) => Object.assign(obj, {[property]: value}), | ||
{}, | ||
) | ||
return {parsedRules} | ||
} | ||
|
||
function isSubset(styles, computedStyle) { | ||
return Object.entries(styles).every( | ||
([prop, value]) => computedStyle.getPropertyValue(prop) === value, | ||
) | ||
} | ||
|
||
function printoutStyles(styles) { | ||
return Object.keys(styles) | ||
.sort() | ||
.map(prop => `${prop}: ${styles[prop]};`) | ||
.join('\n') | ||
} | ||
|
||
// Highlights only style rules that were expected but were not found in the | ||
// received computed styles | ||
function expectedDiff(expected, computedStyles) { | ||
const received = Array.from(computedStyles) | ||
.filter(prop => expected[prop]) | ||
.reduce( | ||
(obj, prop) => | ||
Object.assign(obj, {[prop]: computedStyles.getPropertyValue(prop)}), | ||
{}, | ||
) | ||
const diffOutput = jestDiff( | ||
printoutStyles(expected), | ||
printoutStyles(received), | ||
) | ||
// Remove the "+ Received" annotation because this is a one-way diff | ||
return diffOutput.replace(`${chalk.red('+ Received')}\n`, '') | ||
} | ||
|
||
export function toHaveStyle(htmlElement, css) { | ||
checkHtmlElement(htmlElement) | ||
const {parsedRules: expected, parsingError} = parseCSS(css) | ||
if (parsingError) { | ||
return { | ||
pass: this.isNot, // Fail regardless of the test being positive or negative | ||
message: () => parsingError, | ||
} | ||
} | ||
const received = getComputedStyle(htmlElement) | ||
return { | ||
pass: isSubset(expected, received), | ||
message: () => { | ||
const matcher = `${this.isNot ? '.not' : ''}.toHaveStyle` | ||
return [ | ||
matcherHint(matcher, 'element', ''), | ||
expectedDiff(expected, received), | ||
].join('\n\n') | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters