Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
React Testing Library is ignoring media query in styled component

I have a button component that is hidden on <768px and displayed on >768px

import { styled } from '@mui/material/styles';
import Button from '@mui/material/Button';

interface TestButtonProps {
  buttonLabel: string;
}

const MyButton = styled(Button)(
  ({theme: {breakpoints} }) => ({
    display: 'none',

   [breakpoints.up('md')]: {
     display: 'block',
    }
  })
)

const TestButton = ({
  buttonLabel
  } : TestButtonProps) => {
    return (
      <div>
        <MyButton>
          {buttonLabel}
        </MyButton>
      </div>
    )
  }
  export default TestButton;

I'm trying to use React Testing Library to test that this component is displayed on >768px (so that I can later add a fireEvent test)

import { render, screen } from '@testing-library/react';
import TestButton from './test-button';

const mandatoryProps = {
  buttonLabel: 'Button Label',
};

describe('MyBupaHelpDrawer', () => {
  it('it should render the button above md screensize', () => {
  render(<TestButton {...mandatoryProps} />);
    screen.getByRole('button', { name: 'Button Label'}); 
  });
});

However, the test is failing.

TestingLibraryElementError: Unable to find an accessible element with the role "button" and name "Button Label"

Seems that the styling first rule "display: none" is picked up but the media query rule is not.

Also noted that the following test passes :

expect(window.innerWidth).toBe(1024); 

I've also tried conditionally rendering the component with react based on the screen size, rather than modifying the display attribute with css.

How can I modify my test to pick up the media query?

Should I be approaching this in a different way?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda