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 thechildrenprop value of childrenoptionReact elements for potentially matchingselectReact 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:
blurevent 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.focusevent on theReactWrapper's React element unless it is already in focus.changeevent on theReactWrapper's React element. The matchingoptionReact element will be the one whosechildrenprop value equalschildrePropValueForOption. ForonChangeevent handlers triggered by this simulatedchangeevent,event.target.valuewill equal the value of thevalueprop 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/)
})
})