Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Autocomplete] Testing, simulate change doesn't work. #19880

Closed
johnaagudelo0820 opened this issue Feb 27, 2020 · 2 comments
Closed

[Autocomplete] Testing, simulate change doesn't work. #19880

johnaagudelo0820 opened this issue Feb 27, 2020 · 2 comments
Labels
component: autocomplete This is the name of the generic UI component, not the React module! duplicate This issue or pull request already exists

Comments

@johnaagudelo0820
Copy link

I need to simulate an onChange event with enzyme to update a state's component that is not working, I share the component's code in order to be helped.

Component:

import React, { useState } from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';

const top100Films = [
  { title: 'The Shawshank Redemption', year: 1994 },
  { title: 'The Godfather', year: 1972 },
  { title: 'The Godfather: Part II', year: 1974 },
];

const Counter = () => {
  const [value, setValue] = useState({ title: 'The Godfather', year: 1972 });

  const handleAutocomplete = (e, item) => {
    setValue(item);
  }

  return (
    <>
      {value && (
        <p id="option">{value.title}</p>
      )}
      <Autocomplete
        id="combo-box-demo"
        name="tags"
        debug
        options={top100Films}
        getOptionLabel={option => option.title}
        onChange={handleAutocomplete}
        style={{ width: 300 }}
        renderInput={params => <TextField {...params} label="Combo box" variant="outlined" />}
      />
    </>
  )
}


test component.

import React from 'react';
import { mount } from 'enzyme';

import Counter from '../components/Counter';

describe('<Counter />', () => {

  it('shoult update component', () => {
    const wrapper = mount(<Counter />);
    const autocomplete = wrapper.find('input');
    console.log(autocomplete.debug());
    autocomplete.simulate('change', { target: { value: 'The Shawshank Redemption' }});
    wrapper.update();
    expect(wrapper.find('p').text()).toEqual('The Shawshank Redemption');
  });
});

We use GitHub issues exclusively as a bugs and feature request tracker.

For support, please check out https://material-ui.com/getting-started/support/. Thanks!

@oliviertassinari oliviertassinari added the support: Stack Overflow Please ask the community on Stack Overflow label Feb 27, 2020
@support support bot closed this as completed Feb 27, 2020
@oliviertassinari oliviertassinari added the component: autocomplete This is the name of the generic UI component, not the React module! label Feb 27, 2020
@bbigras
Copy link

bbigras commented Jun 8, 2020

Any workaround for this? Or maybe it's not an issue anymore.

#18251

EDIT: I think I can find what I need in #18251

@oliviertassinari
Copy link
Member

Duplicate of #18251

@oliviertassinari oliviertassinari marked this as a duplicate of #18251 Jun 9, 2020
@oliviertassinari oliviertassinari added the duplicate This issue or pull request already exists label Jun 9, 2020
@mui mui deleted a comment from support bot Jun 9, 2020
@oliviertassinari oliviertassinari removed the support: Stack Overflow Please ask the community on Stack Overflow label Jun 9, 2020
@support support bot reopened this Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: autocomplete This is the name of the generic UI component, not the React module! duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants