From f2819e57f12d0aa29ff6cb8a532aced9b3b80ab9 Mon Sep 17 00:00:00 2001 From: Joe Bourne Date: Fri, 30 Nov 2018 14:56:07 +0000 Subject: [PATCH 1/4] feat: abstract getStyleRule from toHaveStyleRule --- src/toHaveStyleRule.js | 55 +++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/src/toHaveStyleRule.js b/src/toHaveStyleRule.js index 5993fa6..e6e2604 100644 --- a/src/toHaveStyleRule.js +++ b/src/toHaveStyleRule.js @@ -80,15 +80,12 @@ const getRules = (ast, classNames, options) => { ) } -const handleMissingRules = options => ({ - pass: false, - message: () => - `No style rules found on passed Component${ - Object.keys(options).length - ? ` using options:\n${JSON.stringify(options)}` - : '' - }`, -}) +const handleMissingRules = options => + `No style rules found on passed Component${ + Object.keys(options).length + ? ` using options:\n${JSON.stringify(options)}` + : '' + }` const getDeclaration = (rule, property) => rule.declarations @@ -110,27 +107,45 @@ const normalizeOptions = options => }) : options -function toHaveStyleRule(component, property, expected, options = {}) { +function getStyleRule(component, property, options = {}) { const classNames = getClassNames(component) const ast = getCSS() const normalizedOptions = normalizeOptions(options) const rules = getRules(ast, classNames, normalizedOptions) if (!rules.length) { - return handleMissingRules(normalizedOptions) + throw new Error(handleMissingRules(normalizedOptions)) } const declarations = getDeclarations(rules, property) const declaration = declarations.pop() || {} - const received = declaration.value - const pass = - !received && !expected && this.isNot - ? false - : matcherTest(received, expected) - - return { - pass, - message: buildReturnMessage(this.utils, pass, property, received, expected), + return declaration.value +} + +function toHaveStyleRule(component, property, expected, options = {}) { + try { + const received = getStyleRule(component, property, options) + + const pass = + !received && !expected && this.isNot + ? false + : matcherTest(received, expected) + + return { + pass, + message: buildReturnMessage( + this.utils, + pass, + property, + received, + expected + ), + } + } catch (e) { + return { + pass: false, + message: () => e.message, + } } } From ff68d521d576c387606c479509056f8c62b2c283 Mon Sep 17 00:00:00 2001 From: Joe Bourne Date: Fri, 30 Nov 2018 14:57:16 +0000 Subject: [PATCH 2/4] build: export getStyleRule from index.js --- src/index.js | 4 +++- src/toHaveStyleRule.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 998a968..530105f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -const toHaveStyleRule = require('./toHaveStyleRule') +const { getStyleRule, toHaveStyleRule } = require('./toHaveStyleRule') const styleSheetSerializer = require('./styleSheetSerializer') const { resetStyleSheet } = require('./utils') @@ -6,3 +6,5 @@ resetStyleSheet() expect.addSnapshotSerializer(styleSheetSerializer) expect.extend({ toHaveStyleRule }) + +module.exports = { getStyleRule } diff --git a/src/toHaveStyleRule.js b/src/toHaveStyleRule.js index e6e2604..f534932 100644 --- a/src/toHaveStyleRule.js +++ b/src/toHaveStyleRule.js @@ -149,4 +149,4 @@ function toHaveStyleRule(component, property, expected, options = {}) { } } -module.exports = toHaveStyleRule +module.exports = { getStyleRule, toHaveStyleRule } From 47f17c2112aaef69d628302ee4548949eede4e77 Mon Sep 17 00:00:00 2001 From: Joe Bourne Date: Sat, 8 Dec 2018 13:42:22 +0000 Subject: [PATCH 3/4] build: export getStyleRule from utils instead of index --- src/index.js | 4 +--- src/utils.js | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 530105f..5bff341 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -const { getStyleRule, toHaveStyleRule } = require('./toHaveStyleRule') +const { toHaveStyleRule } = require('./toHaveStyleRule') const styleSheetSerializer = require('./styleSheetSerializer') const { resetStyleSheet } = require('./utils') @@ -6,5 +6,3 @@ resetStyleSheet() expect.addSnapshotSerializer(styleSheetSerializer) expect.extend({ toHaveStyleRule }) - -module.exports = { getStyleRule } diff --git a/src/utils.js b/src/utils.js index ceb39ee..6c49252 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,6 @@ const css = require('css') const { ServerStyleSheet, isStyledComponent } = require('styled-components') +const { getStyleRule } = require('./toHaveStyleRule') let StyleSheet @@ -85,4 +86,5 @@ module.exports = { getHashes, buildReturnMessage, matcherTest, + getStyleRule, } From ebc7f509046c4d35a481c7c01d0e13ac52b9c464 Mon Sep 17 00:00:00 2001 From: Joe Bourne Date: Fri, 21 Dec 2018 15:22:42 +0000 Subject: [PATCH 4/4] refactor: create utils directory at root --- src/utils.js | 2 -- utils/index.js | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 utils/index.js diff --git a/src/utils.js b/src/utils.js index 6c49252..ceb39ee 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,6 +1,5 @@ const css = require('css') const { ServerStyleSheet, isStyledComponent } = require('styled-components') -const { getStyleRule } = require('./toHaveStyleRule') let StyleSheet @@ -86,5 +85,4 @@ module.exports = { getHashes, buildReturnMessage, matcherTest, - getStyleRule, } diff --git a/utils/index.js b/utils/index.js new file mode 100644 index 0000000..c2e442a --- /dev/null +++ b/utils/index.js @@ -0,0 +1,3 @@ +const { getStyleRule } = require('../src/toHaveStyleRule') + +module.exports = { getStyleRule }