Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect negated assertion for rule existence check #345

Merged
merged 2 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/native/toHaveStyleRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/toHaveStyleRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the strictness of the "falsey expected" condition. I believe the intention was to check for the existence of the second parameter to toHaveStyleRule, so "=== undefined" is better here. I can relax this, if there's concern around it being a breaking change.

return received !== undefined;
}

try {
const matcher = expected instanceof RegExp ? expect.stringMatching(expected) : expected;

Expand Down
12 changes: 12 additions & 0 deletions test/native/toHaveStyleRule.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ it('negated ".not" modifier with value', () => {
expect(renderer.create(<Button />).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(<Button />).toJSON()).toHaveStyleRule('background-color', 'blue');
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule('opacity');
expect(() => {
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule('background-color');
}).toThrowError();
});

it('jest asymmetric matchers', () => {
const Button = styled.Text`
background-color: ${({ transparent }) => (transparent ? 'transparent' : 'papayawhip')};
Expand Down
23 changes: 18 additions & 5 deletions test/toHaveStyleRule.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,24 @@ it('negated ".not" modifier with value', () => {
opacity: 0.65;
`;

notToHaveStyleRule(<Button />, 'opacity', '0.50');
notToHaveStyleRule(<Button />, 'opacity', '');
notToHaveStyleRule(<Button />, 'opacity', null);
notToHaveStyleRule(<Button />, 'opacity', false);
notToHaveStyleRule(<Button />, 'opacity', undefined);
notToHaveStyleRule(<Button />, "opacity", "0.50");
notToHaveStyleRule(<Button />, "opacity", "");
notToHaveStyleRule(<Button />, "opacity", null);
notToHaveStyleRule(<Button />, "opacity", false);
expect(() => {
toHaveStyleRule(<Button />, "opacity", undefined);
}).toThrowError();
});

it('negated ".not" modifier fails when rule present with no value being asserted', () => {
const Button = styled.button`
opacity: 0.65;
`;

toHaveStyleRule(<Button />, "opacity", "0.65");
expect(() => {
notToHaveStyleRule(<Button />, "opacity");
}).toThrowError();
});

it('jest asymmetric matchers', () => {
Expand Down