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

275
Visualizações
Mocking dot notation React components in Jest

I have a component that, depending on data being loaded, renders either a component or a loading component:

// components/example

import Component from 'components/component';


const Example = ({loaded}) => (
  <>
    {loaded ? <Component/> : <Component.Loading/>}
  </>
)

The loading component is written as dot notation, because I feel like it makes it easier to read and use. Is there any way to mock <Component.Loading/> in my unit tests?

This was my best guess, but it didn't work:

// components/example.test

import Example from './example'

const mockedComponent = jest.fn();

jest.mock('components/component', () => ({
  __esModule: true,
  default: () => mockedComponent(),
}));

const setup = () => {
  const utils = render(<Example loading={false} />);
  return {
    ...utils,
  };
};

it('Renders correctly', () => {
    const component = () => <>Main component</>;
    component.Loading = () => <>Loading component</>;
    mockedComponent.mockReturnValue(component);
    setup();  
});

Jest doesn't like this, and tells me that <Component.Loading/> is undefined.

  console.error
    Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Right so this while doing this I was going off how you usually mock react components:

jest.mock('component/something', () => ({
 __esModule: true,
  default: () => <>Mocked component</>,
}));

This usually works, but for dot-notation component the syntax is different:


const component = () => <>Main component</>;
component.Loading = () => <>Loading</>;

jest.mock('components/component', () => ({
  __esModule: true,
  default: component,
}));

If you want to go a step further you can mock with a jest.fn():

const mockedItemStatus = jest.fn();
const mockedItemStatusSkeleton = jest.fn();

const component = () => mockedItemStatus();
component.Loading = () => mockedItemStatusSkeleton();

jest.mock('components/component', () => ({
  __esModule: true,
  default: component,
}));

Then in your tests you can make the mock return the specific component you need:

  it('Renders correctly', () => {
    mockedItemStatusSkeleton.mockImplementation(() => <>Loading</>);
    render(<Component/>)
    expect(mockedItemStatusSkeleton).toHaveBeenCalled();
  });
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