Skip to content

Commit

Permalink
Complex direct descendents etc work now.
Browse files Browse the repository at this point in the history
  • Loading branch information
ankeetmaini committed Apr 25, 2020
1 parent b055195 commit 7851c98
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
15 changes: 15 additions & 0 deletions packages/jest/src/__tests__/matchers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,19 @@ describe('toHaveCompliedCss', () => {
expect(el).toHaveCompiledCss('color', 'blue', { media: '(min-width: 2px)' });
expect(el).toHaveCompiledCss('color', 'red', { media: '(min-width: 1px)' });
});

it('should match complicated direct ancestors', () => {
const { getByText } = render(
<div
css={`
> :first-child {
color: red;
}
`}>
hello world
</div>
);
const el = getByText('hello world');
expect(el).toHaveCompiledCss('color', 'red', { target: '> :first-child' });
});
});
6 changes: 4 additions & 2 deletions packages/jest/src/matchers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const kebabCase = (str: string) =>
.replace(/\s+/g, '-')
.toLowerCase();

const removeSpaces = (str?: string) => str && str.replace(/\s/g, '');

const mapProperties = (properties: Record<string, any>) =>
Object.keys(properties).map(property => `${kebabCase(property)}:${properties[property]}`);

Expand All @@ -31,7 +33,7 @@ const findMediaRules = (
for (const rule of allRules) {
if (!rule) return;
if ('media' in rule) {
if (rule.media === media.replace(/\s/g, '') && 'rules' in rule) return rule.rules;
if (removeSpaces(rule.media) === removeSpaces(media) && 'rules' in rule) return rule.rules;
if ('rules' in rule) return findMediaRules(rule.rules, media);
}
}
Expand All @@ -55,7 +57,7 @@ const getRules = (ast: CSS.Stylesheet, filter: MatchFilter, className: string) =
const klass = target ? `.${className}${target}` : `.${className}`;
return allRules?.filter(r => {
if ('selectors' in r) {
return r.selectors?.find(s => s === klass);
return r.selectors?.find(s => removeSpaces(s) === removeSpaces(klass));
}
return;
});
Expand Down
2 changes: 1 addition & 1 deletion packages/jest/src/types.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Pseudos } from 'csstype';
export type MatchFilter = {
target?: Pseudos;
target?: Pseudos | string;
media?: string;
};

0 comments on commit 7851c98

Please sign in to comment.