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

105
Visualizações
Asserting that dynamic class name is not present

I am using React Testing Library and would like to assert that a particular dynamic class name is not present on my element.

MyEl.jsx

const MyEl = (someCondition = false, customClassName = '') => (
    <div className={!!someCondition ? `my-custom-class-${customClassName}` : null}>
        some content
    </div>
);

I know that the toHaveClass matcher for Jest expects a proper String, but I thought I might be able to get away with doing an expect of a partial string instead...

it('Should not have custom class', () => {
    render(<MyEl />);

    const mainEl = screen.getByText('some content');

    expect(mainEl).not.toHaveClass(expect.stringContaining('my-custom-class-'));
})

This gives me the error that it is expecting a full string.

Is there something similar to what I am trying to do that will actually work?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use expect.not.stringContaining(string)

expect.not.stringContaining(string) matches the received value if it is not a string or if it is a string that does not contain the exact expected string.

It is the inverse of expect.stringContaining.

And, you can get the element's class content by el.className attribute.

index.tsx:

import React from 'react';

export const MyEl = ({ someCondition = false, customClassName = '' }) => (
  <div className={!!someCondition ? `my-custom-class-${customClassName}` : ''}>some content</div>
);

index.test.tsx:

import { render, screen } from '@testing-library/react';
import React from 'react';
import { MyEl } from '.';

describe('71648378', () => {
  it('Should not have custom class', () => {
    render(<MyEl />);

    const mainEl = screen.getByText('some content');
    expect(mainEl.className).toEqual(expect.not.stringContaining('my-custom-class-'));
  });

  it('Should  have custom class', () => {
    render(<MyEl someCondition customClassName="abc" />);

    const mainEl = screen.getByText('some content');
    expect(mainEl.className).toEqual(expect.stringContaining('my-custom-class-'));
  });
});

Test result:

 PASS  stackoverflow/71648378/index.test.tsx (9.36 s)
  71648378
    ✓ Should not have custom class (23 ms)
    ✓ Should  have custom class (2 ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        10.176 s, estimated 12 s
about 4 years ago · Juan Pablo Isaza Relatório
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