Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix negated ".not" modifier usage edge case #210

Merged
merged 3 commits into from Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}