E
E
eugenedrvnk2022-04-18 15:01:45
React
eugenedrvnk, 2022-04-18 15:01:45

How to test styles in react?

The component accepts 2 props:
- borderColor, and if it is passed - the color of the button's border should change
- children

const Button = ({borderColor, children}) => {
  return (
    <button style={{borderColor: borderColor ? `border: 1px solid ${borderColor}` : null}}>
      {children}
    </button>
  )
}


With testing children, in principle, everything is clear. But what about tests for borderColor? The react-testing-library doc does not have any emphasis on testing styles, and I have not seen this in other sources either.
From my point of view, all props accepted by the component should be tested. in the future, all interaction is built on them. Or does it have some obvious disadvantages in the case of styles?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
standbyoneself, 2022-04-18
@standbyoneself

jest-dom has a toHaveStyle matcher.
About testing all props: you chose the wrong library. The React Testing Library is more about testing logic from the user's point of view, i.e. omitting implementation details.
For unit testing React components, enzyme is more suitable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question