Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda