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

[Bug] Chained selectors with pseudo-state filters are parsed incorrectly in v7.1.1 #422

Open
endpoint-vtsang opened this issue Oct 6, 2022 · 1 comment

Comments

@endpoint-vtsang
Copy link

endpoint-vtsang commented Oct 6, 2022

Problem

In v7.1.1, Jest-styled-components replaced css dependency to @adobe/css-tools. There is a bug in @adobe/css-tools where its parses chained selectors incorrectly. Chained selectors with psuedo-states are parsed incorrectly.

For example

export const StyledButton = styled('button')`
  color: white;
  padding: 40px;
  background: black;

  &[data-focus-visible-added]:not(:disabled),
  &:focus-visible:not(:disabled) {
    background: red;
    box-shadow: green;
  }
`;

Before the update, css would parse the rule as two selectors

    {
        type: 'rule',
        selectors: [
          '.fNulUe[data-focus-visible-added]:not(:disabled)',
          '.fNulUe:focus-visible:not(:disabled)'
        ],
        declarations: [ [Object], [Object] ],
        position: { start: [Object], end: [Object], source: '' }
    },

After the update, @adobe/css-tools would parse the rule as one selector

    {
        type: 'rule',
        selectors: [
            '.fNulUe[data-focus-visible-added]:not(:disabled),.fNulUe:focus-visible:not(:disabled)'
        ],
        declarations: [ [Object], [Object] ],
        position: { start: [Object], end: [Object], source: '' }
    },

This would result in an error where it can't find the selector with toHaveStyleRule()

   No style rules found on passed Component using options:
    {"modifier":"&:focus-visible:not(:disabled)"}

      18 |
      19 |     await expect(myButton).toHaveFocus();
    > 20 |     await expect(myButton).toHaveStyleRule("box-shadow", "green", {
         |                            ^
      21 |       modifier: "&:focus-visible:not(:disabled)",
      22 |     });
      23 |   });

How to debug

In node_modules/jest-styled-components/src/toHaveStyleRule.js , add a console.log in getRules()

const getRules = (ast, classNames, options) => {
  const rules = (hasAtRule(options) ? getAtRules(ast, options) : ast.stylesheet.rules).map((rule) => ({
    ...rule,
    selectors: Array.isArray(rule.selectors) ? rule.selectors.map(normalizeQuotations) : rule.selectors,
  }));
  console.log(rules)
  return rules.filter((rule) => rule.type === 'rule' && hasClassNames(classNames, rule.selectors, options));
};

Link to barebone repo

@endpoint-vtsang endpoint-vtsang changed the title Chained selectors with pseudo-state filters are parsed incorrectly in v7.1.1 [Bug] Chained selectors with pseudo-state filters are parsed incorrectly in v7.1.1 Oct 6, 2022
@diogoribeiro
Copy link

Did someone have time to look at this?
I'm having the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants