Select an option from a select box
Default Checked Props: id
and name
The propValue
, childrenPropValueForOption
, and options
arguments are
passed to .findWrapperForSelect
to find a
ReactWrapper
. The ReactWrapper
will be
used to simulate events.
propValue
(String
): Value is compared with the values of the checked props to assert a match.childrenPropValueForOption
(String
): Value is compared with thechildren
prop value of childrenoption
React elements for potentially matchingselect
React element.options
(Object
): Optional.
propToCheck
(String
): Name of prop to check against instead of the default checked props.
Page
object which invoked the method. This allow the method to be chained
with another Page
object method.
If a ReactWrapper
is found by
.findWrapperForSelect
, then the following events will
be simulated on the ReactWrapper
's React element:
blur
event on the React element which is focused. This will occur if there is a focused React element and it is not the same as theReactWrapper
's React element.focus
event on theReactWrapper
's React element unless it is already in focus.change
event on theReactWrapper
's React element. The matchingoption
React element will be the one whosechildren
prop value equalschildrePropValueForOption
. ForonChange
event handlers triggered by this simulatedchange
event,event.target.value
will equal the value of thevalue
prop for the matching option.
If no ReactWrapper
is found, then an error is thrown.
import React, { Component } from 'react'
import Page from 'react-page-object'
class App extends Component {
state = { selectedOption: 'value 1' }
onChange = event => this.setState({ selectedOption: event.target.value })
render() {
return (
<div>
{this.state.selectedOption}
<select id="select-id" onChange={this.onChange}>
<option value="value 1">option 1</option>
<option value="value 2">option 2</option>
</select>
<select name="select-name" onChange={this.onChange}>
<option value="value 1">option 1</option>
<option value="value 2">option 2</option>
</select>
<select className="select-class" onChange={this.onChange}>
<option value="value 1">option 1</option>
<option value="value 2">option 2</option>
</select>
</div>
)
}
}
describe('select', () => {
let page, wrapper
beforeEach(() => {
page = new Page(<App />)
})
afterEach(() => {
page.destroy()
})
it('selects the option - targeting id', () => {
page.select('select-id', 'option 2')
expect(page.content()).toMatch(/value 2/)
})
it('selects the option - targeting name', () => {
page.select('select-name', 'option 2')
expect(page.content()).toMatch(/value 2/)
})
it('selects the option - targeting non-default prop', () => {
page.select('select-class', 'option 2', { propToCheck: 'className' })
expect(page.content()).toMatch(/value 2/)
})
})