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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toHaveStyleRule to work with not passed expected value and negated ".not" modifier #206

Merged
merged 4 commits into from Nov 10, 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
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -348,6 +348,7 @@ test('it works', () => {

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

```js
const Button = styled.button`
Expand All @@ -362,8 +363,9 @@ test('it applies default styles', () => {
expect(tree).toHaveStyleRule('color', 'red')
expect(tree).toHaveStyleRule('border', '0.05em solid black')
expect(tree).toHaveStyleRule('cursor', 'pointer')
expect(tree).toHaveStyleRule('opacity', undefined) // equivalent of the following
expect(tree).not.toHaveStyleRule('opacity') // equivalent of the following two
expect(tree).not.toHaveStyleRule('opacity', expect.any(String))
expect(tree).toHaveStyleRule('opacity', undefined)
})

test('it applies styles according to passed props', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/native/toHaveStyleRule.js
Expand Up @@ -16,7 +16,9 @@ function toHaveStyleRule(component, property, expected) {
*/
const mergedStyles = styles.reduce((acc, item) => ({ ...acc, ...item }), {})
const received = mergedStyles[camelCasedProperty]
const pass = matcherTest(received, expected)
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

return {
pass,
Expand Down
16 changes: 9 additions & 7 deletions src/toHaveStyleRule.js
Expand Up @@ -101,13 +101,13 @@ const getDeclaration = (rule, property) =>
const getDeclarations = (rules, property) =>
rules.map(rule => getDeclaration(rule, property)).filter(Boolean)

const normalizeOptions = (options) =>
const normalizeOptions = options =>
options.modifier
? Object.assign(
{},
options,
{ modifier: Array.isArray(options.modifier) ? options.modifier.join('') : options.modifier },
)
? Object.assign({}, options, {
modifier: Array.isArray(options.modifier)
? options.modifier.join('')
: options.modifier,
})
: options

function toHaveStyleRule(component, property, expected, options = {}) {
Expand All @@ -123,7 +123,9 @@ function toHaveStyleRule(component, property, expected, options = {}) {
const declarations = getDeclarations(rules, property)
const declaration = declarations.pop() || {}
const received = declaration.value
const pass = matcherTest(received, expected)
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

return {
pass,
Expand Down
14 changes: 14 additions & 0 deletions test/native/toHaveStyleRule.spec.js
Expand Up @@ -78,6 +78,20 @@ test('undefined', () => {
)
})

test('negated ".not" modifier with no value', () => {
const Button = styled.Text`
${({ transparent }) => !transparent && 'background-color: papayawhip;'};
`

expect(renderer.create(<Button />).toJSON()).toHaveStyleRule(
'background-color',
'papayawhip'
)
expect(renderer.create(<Button transparent />).toJSON()).not.toHaveStyleRule(
'background-color'
)
})

test('jest asymmetric matchers', () => {
const Button = styled.Text`
background-color: ${({ transparent }) =>
Expand Down
9 changes: 9 additions & 0 deletions test/toHaveStyleRule.spec.js
Expand Up @@ -143,6 +143,15 @@ test('undefined', () => {
toHaveStyleRule(<Button disabled />, 'cursor', undefined)
})

test('negated ".not" modifier with no value', () => {
const Button = styled.button`
opacity: ${({ disabled }) => disabled && '.65'};
`

notToHaveStyleRule(<Button />, 'opacity')
toHaveStyleRule(<Button disabled />, 'opacity', '.65')
})

test('jest asymmetric matchers', () => {
const Button = styled.button`
border: 0.1em solid
Expand Down