Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!