Skip to content

Commit de040e4

Browse files
authored
Simplify media query regex for stripping spaces (#379)
1 parent 3e9954c commit de040e4

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/toHaveStyleRule.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ const getClassNames = (received) => {
3737
const hasAtRule = (options) => Object.keys(options).some((option) => ['media', 'supports'].includes(option));
3838

3939
const getAtRules = (ast, options) => {
40-
const mediaRegex = /(\([a-z-]+:)\s?([a-z0-9.]+\))/g;
41-
4240
return Object.keys(options)
4341
.map((option) =>
4442
ast.stylesheet.rules
45-
.filter((rule) => rule.type === option && rule[option] === options[option].replace(mediaRegex, '$1$2'))
43+
.filter((rule) => rule.type === option && rule[option] === options[option].replace(/:\s/g, ":"))
4644
.map((rule) => rule.rules)
4745
.reduce((acc, rules) => acc.concat(rules), [])
4846
)
@@ -96,8 +94,7 @@ const getRules = (ast, classNames, options) => {
9694
const handleMissingRules = (options) => ({
9795
pass: false,
9896
message: () =>
99-
`No style rules found on passed Component${
100-
Object.keys(options).length ? ` using options:\n${JSON.stringify(options)}` : ''
97+
`No style rules found on passed Component${Object.keys(options).length ? ` using options:\n${JSON.stringify(options)}` : ''
10198
}`,
10299
});
103100

@@ -111,8 +108,8 @@ const getDeclarations = (rules, property) => rules.map((rule) => getDeclaration(
111108
const normalizeOptions = (options) =>
112109
options.modifier
113110
? Object.assign({}, options, {
114-
modifier: Array.isArray(options.modifier) ? options.modifier.join('') : options.modifier,
115-
})
111+
modifier: Array.isArray(options.modifier) ? options.modifier.join('') : options.modifier,
112+
})
116113
: options;
117114

118115
function toHaveStyleRule(component, property, expected, options = {}) {

test/toHaveStyleRule.spec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ it('at rules', () => {
220220
@media (min-width: 576px) and (max-width: 767.98px) {
221221
color: red;
222222
}
223+
@media (min-width: calc(768px + 1px)) and (max-width:calc(1024px + 1px)) {
224+
color: purple;
225+
}
223226
`;
224227

225228
toHaveStyleRule(<Wrapper />, 'color', 'red');
@@ -244,6 +247,9 @@ it('at rules', () => {
244247
toHaveStyleRule(<Wrapper />, 'color', 'red', {
245248
media: '(min-width: 576px) and (max-width: 767.98px)',
246249
});
250+
toHaveStyleRule(<Wrapper />, "color", "purple", {
251+
media: "(min-width: calc(768px + 1px)) and (max-width:calc(1024px + 1px))"
252+
});
247253
});
248254

249255
it('selector modifiers', () => {
@@ -392,10 +398,7 @@ it('component modifiers', () => {
392398
'color',
393399
'blue',
394400
{
395-
// eslint-disable-next-line prettier/prettier
396-
modifier: css`
397-
${Text}
398-
`,
401+
modifier: css`${Text}`
399402
}
400403
);
401404
toHaveStyleRule(
@@ -415,10 +418,7 @@ it('component modifiers', () => {
415418
'color',
416419
'purple',
417420
{
418-
// eslint-disable-next-line prettier/prettier
419-
modifier: css`
420-
${Text} &
421-
`,
421+
modifier: css`${Text} &`
422422
}
423423
);
424424
});

0 commit comments

Comments
 (0)