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

Proposal for handling properties/css rules with no value #205

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/toHaveStyleRule.js
Expand Up @@ -151,7 +151,7 @@ function toHaveStyleRule(component, property, expected, options = {}) {
}

const declarations = getDeclarations(rules, property)
const declaration = declarations.pop() || {}
const declaration = declarations.pop() || { value: null }
const received = declaration.value
const pass = matcherTest(received, expected)

Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/toHaveStyleRule.spec.js.snap
Expand Up @@ -6,7 +6,7 @@ exports[`message when property not found 1`] = `
Expected
\\"background-color: black\\"
Received:
\\"background-color: undefined\\""
\\"background-color: null\\""
`;

exports[`message when rules not found 1`] = `"No style rules found on passed Component"`;
Expand Down
20 changes: 17 additions & 3 deletions test/toHaveStyleRule.spec.js
Expand Up @@ -45,6 +45,7 @@ test('null', () => {

test('non-styled', () => {
notToHaveStyleRule(<div />, 'a', 'b')
notToHaveStyleRule(<div />, 'a')
})

test('message when rules not found', () => {
Expand Down Expand Up @@ -137,16 +138,29 @@ test('complex string', () => {
toHaveStyleRule(<Wrapper />, 'border', '1px solid rgba(0,0,0,0.125)')
})

test('undefined', () => {
test('conditional rule - null value', () => {
const Button = styled.button`
cursor: ${({ disabled }) => !disabled && 'pointer'};
opacity: ${({ disabled }) => disabled && '.65'};
`

toHaveStyleRule(<Button />, 'opacity', undefined)
toHaveStyleRule(<Button />, 'opacity', null)
toHaveStyleRule(<Button />, 'cursor', 'pointer')
toHaveStyleRule(<Button disabled />, 'opacity', '.65')
toHaveStyleRule(<Button disabled />, 'cursor', undefined)
toHaveStyleRule(<Button disabled />, 'cursor', null)
})

test('at rules initially no value', () => {
const Button = styled.button`
@media (max-width: 640px) {
width: 50%;
}
`
notToHaveStyleRule(<Button />, 'width')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MicheleBertoli I mentioned those changes in our previous discussion, as promised I deliver those as a separate PR. Changes were made due to the 'notToHaveStyleRule' check marked with this comment. It is a quick fix and probably there is a better way to do it. If you share any ideas I can try to fix in better way. In my old test suits such tests worked well and a scenario when you check lack of some rule seems to be useful when doing mobile first design. Anyway, I would like to get your opinion on it.

toHaveStyleRule(<Button />, 'width', null)
toHaveStyleRule(<Button />, 'width', expect.any(String), {
media: '(max-width: 640px)',
})
})

test('jest asymmetric matchers', () => {
Expand Down