Search code examples
reactjsjestjsjest-dom

Jest-dom how to check if component have styles by id


I'm trying to check if this ModalVideo component have some styles applied when opened (prop isOpen={true}), like so:

import React from 'react';
import { render, cleanup } from '@testing-library/react';
import '@testing-library/jest-dom';
import TestThemeProvider, {
  defaultTheme,
} from '../../../providers/themeProvider/testThemeProvider';

import ModalVideo from '.';

afterEach(cleanup);

describe('ModalVideo', () => {
  

  test('ModalVideo has correct styles when is full screen', () => {    
    const { getByTestId } = render(
      <TestThemeProvider>
        <ModalVideo
          data-testid="modalVideo"
          isOpen={true}
          videoURL={
            'https://player.vimeo.com/external/the-url'
          }
          callToAction={'Play video'}
        ></ModalVideo>
      </TestThemeProvider>
    );

    expect(getByTestId(/modalVideo/i)).toHaveStyle(`        
        position: fixed;
        display: flex;
    `);
  });
});

But the IDE fires this warning: enter image description here

Also tried as string: enter image description here

Any idea how can I achieve it?


Solution

  • I solved it by adding a class to the container (.modal) and then using querySelector

    test('ModalVideo has correct styles when is full screen', () => {
        const { container } = render(
          <TestThemeProvider>
            <ModalVideo
              data-testid="modalVideo"
              isOpen={true}
              videoURL={
                'https://player.vimeo.com/external/the-url'
              }
              callToAction={'Play video'}
            ></ModalVideo>
          </TestThemeProvider>
        );
    
        const modal = container.querySelector('.modal');
        expect(modal).toBeVisible();
        expect(modal).toHaveStyle(`
            position: fixed;
            display: flex;
        `);
      });