Skip to content

Commit

Permalink
Fix negated ".not" modifier usage edge case (#210)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
santino authored and MicheleBertoli committed Nov 11, 2018
1 parent 759ab6f commit 7103cea
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/native/toHaveStyleRule.js
Expand Up @@ -16,9 +16,10 @@ function toHaveStyleRule(component, property, expected) {
*/
const mergedStyles = styles.reduce((acc, item) => ({ ...acc, ...item }), {})
const received = mergedStyles[camelCasedProperty]
const matches = matcherTest(received, expected)
// if expected value is not passed and we have a negated ".not" modifier we need to flip our assertion
const pass = !expected && this.isNot ? !matches : matches
const pass =
!received && !expected && this.isNot
? false
: matcherTest(received, expected)

return {
pass,
Expand Down
7 changes: 4 additions & 3 deletions src/toHaveStyleRule.js
Expand Up @@ -123,9 +123,10 @@ function toHaveStyleRule(component, property, expected, options = {}) {
const declarations = getDeclarations(rules, property)
const declaration = declarations.pop() || {}
const received = declaration.value
const matches = matcherTest(received, expected)
// if expected value is not passed and we have a negated ".not" modifier we need to flip our assertion
const pass = !expected && this.isNot ? !matches : matches
const pass =
!received && !expected && this.isNot
? false
: matcherTest(received, expected)

return {
pass,
Expand Down
27 changes: 27 additions & 0 deletions test/native/toHaveStyleRule.spec.js
Expand Up @@ -92,6 +92,33 @@ test('negated ".not" modifier with no value', () => {
)
})

test('negated ".not" modifier with value', () => {
const Button = styled.Text`
padding: 4px;
`

expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule(
'padding',
'2px'
)
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule(
'padding',
''
)
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule(
'padding',
null
)
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule(
'padding',
false
)
expect(renderer.create(<Button />).toJSON()).not.toHaveStyleRule(
'padding',
undefined
)
})

test('jest asymmetric matchers', () => {
const Button = styled.Text`
background-color: ${({ transparent }) =>
Expand Down
12 changes: 12 additions & 0 deletions test/toHaveStyleRule.spec.js
Expand Up @@ -152,6 +152,18 @@ test('negated ".not" modifier with no value', () => {
toHaveStyleRule(<Button disabled />, 'opacity', '.65')
})

test('negated ".not" modifier with value', () => {
const Button = styled.button`
opacity: 0.65;
`

notToHaveStyleRule(<Button />, 'opacity', '0.50')
notToHaveStyleRule(<Button />, 'opacity', '')
notToHaveStyleRule(<Button />, 'opacity', null)
notToHaveStyleRule(<Button />, 'opacity', false)
notToHaveStyleRule(<Button />, 'opacity', undefined)
})

test('jest asymmetric matchers', () => {
const Button = styled.button`
border: 0.1em solid
Expand Down
4 changes: 2 additions & 2 deletions typings/index.d.ts
Expand Up @@ -3,7 +3,7 @@ interface AsymmetricMatcher {
sample?: string | RegExp | object | Array<any> | Function;
}

type Value = string | number | RegExp | AsymmetricMatcher | undefined
type Value = string | number | RegExp | AsymmetricMatcher | undefined;

interface Options {
media?: string;
Expand All @@ -14,6 +14,6 @@ interface Options {
declare namespace jest {

interface Matchers<R> {
toHaveStyleRule(property: string, value: Value, options?: Options): R;
toHaveStyleRule(property: string, value?: Value, options?: Options): R;
}
}

0 comments on commit 7103cea

Please sign in to comment.