Skip to content

Commit 831faca

Browse files
santinoMicheleBertoli
authored andcommitted
toHaveStyleRule to work with not passed expected value and negated ".not" modifier (#206)
* Fix mediaRegex to allow dot special character to add compatibility with decimal values in media queries * toHaveStyleRule to work with not passed expected value and negated ".not" modifier * Updating documentation of `toHaveStyleRule` for negated ".not" modifier usage
1 parent 6944b7d commit 831faca

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ test('it works', () => {
348348

349349
The `toHaveStyleRule` matcher is useful to test if a given rule is applied to a component.
350350
The first argument is the expected property, the second is the expected value which can be a String, RegExp, Jest asymmetric matcher or `undefined`.
351+
When used with a negated ".not" modifier the second argument is optional and can be omitted.
351352

352353
```js
353354
const Button = styled.button`
@@ -362,8 +363,9 @@ test('it applies default styles', () => {
362363
expect(tree).toHaveStyleRule('color', 'red')
363364
expect(tree).toHaveStyleRule('border', '0.05em solid black')
364365
expect(tree).toHaveStyleRule('cursor', 'pointer')
365-
expect(tree).toHaveStyleRule('opacity', undefined) // equivalent of the following
366+
expect(tree).not.toHaveStyleRule('opacity') // equivalent of the following two
366367
expect(tree).not.toHaveStyleRule('opacity', expect.any(String))
368+
expect(tree).toHaveStyleRule('opacity', undefined)
367369
})
368370
369371
test('it applies styles according to passed props', () => {

src/native/toHaveStyleRule.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ function toHaveStyleRule(component, property, expected) {
1616
*/
1717
const mergedStyles = styles.reduce((acc, item) => ({ ...acc, ...item }), {})
1818
const received = mergedStyles[camelCasedProperty]
19-
const pass = matcherTest(received, expected)
19+
const matches = matcherTest(received, expected)
20+
// if expected value is not passed and we have a negated ".not" modifier we need to flip our assertion
21+
const pass = !expected && this.isNot ? !matches : matches
2022

2123
return {
2224
pass,

src/toHaveStyleRule.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ const getDeclaration = (rule, property) =>
101101
const getDeclarations = (rules, property) =>
102102
rules.map(rule => getDeclaration(rule, property)).filter(Boolean)
103103

104-
const normalizeOptions = (options) =>
104+
const normalizeOptions = options =>
105105
options.modifier
106-
? Object.assign(
107-
{},
108-
options,
109-
{ modifier: Array.isArray(options.modifier) ? options.modifier.join('') : options.modifier },
110-
)
106+
? Object.assign({}, options, {
107+
modifier: Array.isArray(options.modifier)
108+
? options.modifier.join('')
109+
: options.modifier,
110+
})
111111
: options
112112

113113
function toHaveStyleRule(component, property, expected, options = {}) {
@@ -123,7 +123,9 @@ function toHaveStyleRule(component, property, expected, options = {}) {
123123
const declarations = getDeclarations(rules, property)
124124
const declaration = declarations.pop() || {}
125125
const received = declaration.value
126-
const pass = matcherTest(received, expected)
126+
const matches = matcherTest(received, expected)
127+
// if expected value is not passed and we have a negated ".not" modifier we need to flip our assertion
128+
const pass = !expected && this.isNot ? !matches : matches
127129

128130
return {
129131
pass,

test/native/toHaveStyleRule.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ test('undefined', () => {
7878
)
7979
})
8080

81+
test('negated ".not" modifier with no value', () => {
82+
const Button = styled.Text`
83+
${({ transparent }) => !transparent && 'background-color: papayawhip;'};
84+
`
85+
86+
expect(renderer.create(<Button />).toJSON()).toHaveStyleRule(
87+
'background-color',
88+
'papayawhip'
89+
)
90+
expect(renderer.create(<Button transparent />).toJSON()).not.toHaveStyleRule(
91+
'background-color'
92+
)
93+
})
94+
8195
test('jest asymmetric matchers', () => {
8296
const Button = styled.Text`
8397
background-color: ${({ transparent }) =>

test/toHaveStyleRule.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ test('undefined', () => {
143143
toHaveStyleRule(<Button disabled />, 'cursor', undefined)
144144
})
145145

146+
test('negated ".not" modifier with no value', () => {
147+
const Button = styled.button`
148+
opacity: ${({ disabled }) => disabled && '.65'};
149+
`
150+
151+
notToHaveStyleRule(<Button />, 'opacity')
152+
toHaveStyleRule(<Button disabled />, 'opacity', '.65')
153+
})
154+
146155
test('jest asymmetric matchers', () => {
147156
const Button = styled.button`
148157
border: 0.1em solid

0 commit comments

Comments
 (0)