Skip to content

Commit 7103cea

Browse files
santinoMicheleBertoli
authored andcommitted
Fix negated ".not" modifier usage edge case (#210)
* Fix edge case with negated ".not" modifier and undefined expected value * Update TS definitions to make expected value optional * Adding more extensive tests for negated ".not" modifier
1 parent 759ab6f commit 7103cea

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

src/native/toHaveStyleRule.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ function toHaveStyleRule(component, property, expected) {
1616
*/
1717
const mergedStyles = styles.reduce((acc, item) => ({ ...acc, ...item }), {})
1818
const received = mergedStyles[camelCasedProperty]
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
19+
const pass =
20+
!received && !expected && this.isNot
21+
? false
22+
: matcherTest(received, expected)
2223

2324
return {
2425
pass,

src/toHaveStyleRule.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ function toHaveStyleRule(component, property, expected, options = {}) {
123123
const declarations = getDeclarations(rules, property)
124124
const declaration = declarations.pop() || {}
125125
const received = declaration.value
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
126+
const pass =
127+
!received && !expected && this.isNot
128+
? false
129+
: matcherTest(received, expected)
129130

130131
return {
131132
pass,

test/native/toHaveStyleRule.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,33 @@ test('negated ".not" modifier with no value', () => {
9292
)
9393
})
9494

95+
test('negated ".not" modifier with value', () => {
96+
const Button = styled.Text`
97+
padding: 4px;
98+
`
99+
100+
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule(
101+
'padding',
102+
'2px'
103+
)
104+
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule(
105+
'padding',
106+
''
107+
)
108+
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule(
109+
'padding',
110+
null
111+
)
112+
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule(
113+
'padding',
114+
false
115+
)
116+
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule(
117+
'padding',
118+
undefined
119+
)
120+
})
121+
95122
test('jest asymmetric matchers', () => {
96123
const Button = styled.Text`
97124
background-color: ${({ transparent }) =>

test/toHaveStyleRule.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ test('negated ".not" modifier with no value', () => {
152152
toHaveStyleRule(<Button disabled />, 'opacity', '.65')
153153
})
154154

155+
test('negated ".not" modifier with value', () => {
156+
const Button = styled.button`
157+
opacity: 0.65;
158+
`
159+
160+
notToHaveStyleRule(<Button />, 'opacity', '0.50')
161+
notToHaveStyleRule(<Button />, 'opacity', '')
162+
notToHaveStyleRule(<Button />, 'opacity', null)
163+
notToHaveStyleRule(<Button />, 'opacity', false)
164+
notToHaveStyleRule(<Button />, 'opacity', undefined)
165+
})
166+
155167
test('jest asymmetric matchers', () => {
156168
const Button = styled.button`
157169
border: 0.1em solid

typings/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ interface AsymmetricMatcher {
33
sample?: string | RegExp | object | Array<any> | Function;
44
}
55

6-
type Value = string | number | RegExp | AsymmetricMatcher | undefined
6+
type Value = string | number | RegExp | AsymmetricMatcher | undefined;
77

88
interface Options {
99
media?: string;
@@ -14,6 +14,6 @@ interface Options {
1414
declare namespace jest {
1515

1616
interface Matchers<R> {
17-
toHaveStyleRule(property: string, value: Value, options?: Options): R;
17+
toHaveStyleRule(property: string, value?: Value, options?: Options): R;
1818
}
1919
}

0 commit comments

Comments
 (0)