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

133
Visualizações
Unit test renderTags prop of Autocomplete

I have been stuck trying to unit test the renderTags property of a mui Autocomplete component. I created a custom component which uses this. But I can't figure out how to unit test it. Below I'll show you what I have tried so far.

The component looks like this:

function ChipInput(props: Props): ReactElement {
  const [field, meta, { setValue }] = useField<string[]>(props.name);
  const errorText = (meta.touched && meta.error) || undefined;

  const handleChange = useCallback((_, value) => setValue(value), [setValue]);

  const renderTags = (value: string[], getTagProps) =>
    value.map((option: string, index: number) => (
      <Chip variant="outlined" label={option} {...getTagProps({ index })} />
    ));

  const renderInput = params => (
    <TextField
      {...params}
      variant="filled"
      label={props.label}
      error={errorText !== undefined}
      helperText={errorText || props.helperText}
    />
  );

  return (
    <Column column={props.column}>
      <Autocomplete
        id={`chipInput-${props.name}`}
        multiple
        options={[]}
        value={field.value || []}
        onChange={handleChange}
        freeSolo
        renderTags={renderTags}
        renderInput={renderInput}
      />
    </Column>
  );
}

And this is how I am currently trying to unit test it. It works like this for renderInput but not for renderTags:

describe('ChipInput', () => {
  const useFieldMock = jest.spyOn(Formik, 'useField');
  const flavours = mockUseField<unknown>(['salt', 'paprika']);

  const props: ComponentProps<typeof ChipInput> = {
    name: 'chipInput',
    label: 'Chip input',
    column: 1,
  };

  it('should render chips', () => {
    jest.clearAllMocks();
    useFieldMock.mockReturnValue(flavours);

    const component = shallow(<ChipInput {...props} />);
    const autocomplete = component.find(Autocomplete);

    console.log(autocomplete.prop('value'));
    const renderTags = autocomplete.prop('renderTags') as () => ReactElement;
    const input = shallow(renderTags());

    expect(input).toExist();
  });
});

In the console log, I can see [ 'salt', 'paprika' ] printed, so I know the value has been set correctly. But somehow, on shallow(renderTags()) it throws the error TypeError: Cannot read property 'map' of undefined, because apparently in renderTags the value param is still empty. Even though as seen in the console log, it has been set.

Can anyone point me in the right direction? I feel I must misunderstand something. Thanks in advance!

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

0

The problem is that when you take autocomplete.prop('renderTags') it returns the renderTags function that you defined in the component as is. And its type is not () => ReactElement but (value: string[], getTagProps: Function) => ReactElement[]. Then you call renderTags without arguments, value is undefined, and you get the error when value.map(...) is executed. To fix the issue you need to pass a proper set of arguments:

// I've made the second argument(getTagProps) return an empty object
// update with what you expect
const input = shallow(renderTags(['salt', 'paprika'], () => ({})));

In general, I would suggest moving renderTags out of the component and making it exportable, so it can be tested in isolation. This would make testing way easier(no need to render the entire component).

about 4 years ago · Juan Pablo Isaza Relatório

0

Try calling it by passing arguments like this..

const component = shallow(<ChipInput {...props} />);

const autocomplete = component.find(Autocomplete);

autocomplete.at(0).prop('renderTags')(['salt', 'paprika'],{})
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