diff --git a/src/native/toHaveStyleRule.js b/src/native/toHaveStyleRule.js
index 7fb3194..d81e46a 100644
--- a/src/native/toHaveStyleRule.js
+++ b/src/native/toHaveStyleRule.js
@@ -12,9 +12,9 @@ function toHaveStyleRule(component, property, expected) {
* Merge all styles into one final style object and search for the desired
* stylename against this object
*/
- const mergedStyles = styles.reduce((acc, item) => Object.assign({}, acc, item), {});
+ const mergedStyles = styles.reduce((acc, item) => (Object.assign({}, acc, item)), {});
const received = mergedStyles[camelCasedProperty];
- const pass = !received && !expected && this.isNot ? false : matcherTest(received, expected);
+ const pass = matcherTest(received, expected, this.isNot);
return {
pass,
diff --git a/src/toHaveStyleRule.js b/src/toHaveStyleRule.js
index 929853c..ba23682 100644
--- a/src/toHaveStyleRule.js
+++ b/src/toHaveStyleRule.js
@@ -125,7 +125,7 @@ function toHaveStyleRule(component, property, expected, options = {}) {
const declarations = getDeclarations(rules, property);
const declaration = declarations.pop() || {};
const received = declaration.value;
- const pass = !received && !expected && this.isNot ? false : matcherTest(received, expected);
+ const pass = matcherTest(received, expected, this.isNot);
return {
pass,
diff --git a/src/utils.js b/src/utils.js
index 3a09e99..2eb4874 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -59,7 +59,12 @@ const buildReturnMessage = (utils, pass, property, received, expected) => () =>
'Received:\n' +
` ${utils.printReceived(`${property}: ${received}`)}`;
-const matcherTest = (received, expected) => {
+const matcherTest = (received, expected, isNot) => {
+ // when negating, assert on existence of the style, rather than the value
+ if (isNot && expected === undefined) {
+ return received !== undefined;
+ }
+
try {
const matcher = expected instanceof RegExp ? expect.stringMatching(expected) : expected;
diff --git a/test/native/toHaveStyleRule.spec.js b/test/native/toHaveStyleRule.spec.js
index 4d0101e..240798f 100644
--- a/test/native/toHaveStyleRule.spec.js
+++ b/test/native/toHaveStyleRule.spec.js
@@ -80,6 +80,18 @@ it('negated ".not" modifier with value', () => {
expect(renderer.create().toJSON()).not.toHaveStyleRule('padding', undefined);
});
+it('negated ".not" modifier fails when rule present with no value being asserted', () => {
+ const Button = styled.Text`
+ background-color: blue;
+ `;
+
+ expect(renderer.create().toJSON()).toHaveStyleRule('background-color', 'blue');
+ expect(renderer.create().toJSON()).not.toHaveStyleRule('opacity');
+ expect(() => {
+ expect(renderer.create().toJSON()).not.toHaveStyleRule('background-color');
+ }).toThrowError();
+});
+
it('jest asymmetric matchers', () => {
const Button = styled.Text`
background-color: ${({ transparent }) => (transparent ? 'transparent' : 'papayawhip')};
diff --git a/test/toHaveStyleRule.spec.js b/test/toHaveStyleRule.spec.js
index 9042690..e5e088c 100644
--- a/test/toHaveStyleRule.spec.js
+++ b/test/toHaveStyleRule.spec.js
@@ -130,11 +130,24 @@ it('negated ".not" modifier with value', () => {
opacity: 0.65;
`;
- notToHaveStyleRule(, 'opacity', '0.50');
- notToHaveStyleRule(, 'opacity', '');
- notToHaveStyleRule(, 'opacity', null);
- notToHaveStyleRule(, 'opacity', false);
- notToHaveStyleRule(, 'opacity', undefined);
+ notToHaveStyleRule(, "opacity", "0.50");
+ notToHaveStyleRule(, "opacity", "");
+ notToHaveStyleRule(, "opacity", null);
+ notToHaveStyleRule(, "opacity", false);
+ expect(() => {
+ toHaveStyleRule(, "opacity", undefined);
+ }).toThrowError();
+});
+
+it('negated ".not" modifier fails when rule present with no value being asserted', () => {
+ const Button = styled.button`
+ opacity: 0.65;
+ `;
+
+ toHaveStyleRule(, "opacity", "0.65");
+ expect(() => {
+ notToHaveStyleRule(, "opacity");
+ }).toThrowError();
});
it('jest asymmetric matchers', () => {