Skip to content

Commit a712c13

Browse files
noahnuquantizor
andauthored
fix: incorrect negated assertion for rule existence check (#345)
Co-authored-by: Evan Jacobs <[email protected]>
1 parent 86d0ebf commit a712c13

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

src/native/toHaveStyleRule.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ function toHaveStyleRule(component, property, expected) {
1212
* Merge all styles into one final style object and search for the desired
1313
* stylename against this object
1414
*/
15-
const mergedStyles = styles.reduce((acc, item) => Object.assign({}, acc, item), {});
15+
const mergedStyles = styles.reduce((acc, item) => (Object.assign({}, acc, item)), {});
1616
const received = mergedStyles[camelCasedProperty];
17-
const pass = !received && !expected && this.isNot ? false : matcherTest(received, expected);
17+
const pass = matcherTest(received, expected, this.isNot);
1818

1919
return {
2020
pass,

src/toHaveStyleRule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function toHaveStyleRule(component, property, expected, options = {}) {
125125
const declarations = getDeclarations(rules, property);
126126
const declaration = declarations.pop() || {};
127127
const received = declaration.value;
128-
const pass = !received && !expected && this.isNot ? false : matcherTest(received, expected);
128+
const pass = matcherTest(received, expected, this.isNot);
129129

130130
return {
131131
pass,

src/utils.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ const buildReturnMessage = (utils, pass, property, received, expected) => () =>
5959
'Received:\n' +
6060
` ${utils.printReceived(`${property}: ${received}`)}`;
6161

62-
const matcherTest = (received, expected) => {
62+
const matcherTest = (received, expected, isNot) => {
63+
// when negating, assert on existence of the style, rather than the value
64+
if (isNot && expected === undefined) {
65+
return received !== undefined;
66+
}
67+
6368
try {
6469
const matcher = expected instanceof RegExp ? expect.stringMatching(expected) : expected;
6570

test/native/toHaveStyleRule.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ it('negated ".not" modifier with value', () => {
8080
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule('padding', undefined);
8181
});
8282

83+
it('negated ".not" modifier fails when rule present with no value being asserted', () => {
84+
const Button = styled.Text`
85+
background-color: blue;
86+
`;
87+
88+
expect(renderer.create(<Button />).toJSON()).toHaveStyleRule('background-color', 'blue');
89+
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule('opacity');
90+
expect(() => {
91+
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule('background-color');
92+
}).toThrowError();
93+
});
94+
8395
it('jest asymmetric matchers', () => {
8496
const Button = styled.Text`
8597
background-color: ${({ transparent }) => (transparent ? 'transparent' : 'papayawhip')};

test/toHaveStyleRule.spec.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,24 @@ it('negated ".not" modifier with value', () => {
130130
opacity: 0.65;
131131
`;
132132

133-
notToHaveStyleRule(<Button />, 'opacity', '0.50');
134-
notToHaveStyleRule(<Button />, 'opacity', '');
135-
notToHaveStyleRule(<Button />, 'opacity', null);
136-
notToHaveStyleRule(<Button />, 'opacity', false);
137-
notToHaveStyleRule(<Button />, 'opacity', undefined);
133+
notToHaveStyleRule(<Button />, "opacity", "0.50");
134+
notToHaveStyleRule(<Button />, "opacity", "");
135+
notToHaveStyleRule(<Button />, "opacity", null);
136+
notToHaveStyleRule(<Button />, "opacity", false);
137+
expect(() => {
138+
toHaveStyleRule(<Button />, "opacity", undefined);
139+
}).toThrowError();
140+
});
141+
142+
it('negated ".not" modifier fails when rule present with no value being asserted', () => {
143+
const Button = styled.button`
144+
opacity: 0.65;
145+
`;
146+
147+
toHaveStyleRule(<Button />, "opacity", "0.65");
148+
expect(() => {
149+
notToHaveStyleRule(<Button />, "opacity");
150+
}).toThrowError();
138151
});
139152

140153
it('jest asymmetric matchers', () => {

0 commit comments

Comments
 (0)