question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapper agnostic way of retrieving/setting the value of a SELECT

See original GitHub issue

We’re struggling with the testing of SELECT fields in MiQ, while a simple input field can be changed using simulate('change'), the select needs to be hacked with tapping into the onSelect of the right component in the DOM. I was not able to find a clear way to find this select automatically, always had to juggle with the selector + children().children() calls.

I am assuming that if I change my component mapper, I will have to rewrite all my tests as they can have a different implementation of select, with the onChange function set on a different level. Would it be possible to somehow expose a way of querying/setting the value of a select in a unified way for any component mapper? I know that there’s a common wrapper around all selects, so it should be theoretically possible to find that and have something available there.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Hyperkid123commented, Oct 22, 2020

@skateman. Mapper agnostic way is using the formOptions.change(). You will have create a testing component that will expose the formApi. Either enhance the schema or add it to the template somewhere.

const DummyComponent = () => <div></div> // only exists to expose its props from the hook
const FormApiComponent = () => {
  const formOptions = useFormApi()
  return <DummyComponent {...formOptions} />
}

// in enzyme test
wrapper.find('DummyComponent').prop('change')('field-name', 'field-value')

For this example use some test-specific FormRenderer that will inject the extra component to the form. Using jest mock should be enough to modify your current renderer wrapper schema/template.

But generally, you can’t just tap into React.Context value which is being used to store the form context. So you have to add some testing API one way or the other. No other option I am afraid.

We could add a component like this to some test-utils package. But honestly, I don’t think that this is something we really need in the core library. It’s very easy and quick to implement. Some react testing libraries won’t event let you access component props/state and will only let you modify the components via browser events. Which is the right ™️ way to do it.

0reactions
skatemancommented, Oct 25, 2020

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Select input is not showing value when using map function
Im trying material ui select but it is not working as i would like to. it is not showing the default value when...
Read more >
<select>: The HTML Select element - HTML - MDN Web Docs
Each <option> element should have a value attribute containing the data value to submit to the server when that option is selected.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found