Closed andrewmclagan closed 7 years ago
Does your code work in your app, but just not in your test?
Sorry should have been clearer. The this.submit
is never executed within the test. Although it may be due to enzyme / react-bootstrap. Im thinking that the event simulator in enzyme is not bubbling, and may need to be placed directly on the <a>
element rather then the bootstrap component. will report if i find anything odd.
+1 I'm using react-bootstrap
.
However if I specify onSubmit
in the parent component it is working!
I think I ran into the same issue. I was able to work around it by grabbing an instance of the component, setting the stub, and then calling forceUpdate
on the component AND calling update
on the wrapper.
See: airbnb/enzyme/issues/586
Im thinking that the event simulator in enzyme is not bubbling
This is correct.
Enzyme's shallow( ... ).simulate()
doesn't buble events, it just gets the event handler and invokes it directly. No real eventing going on.
It does not work for me. In my case onSubmit undefined, even when I passed the handleSubmit to props. Any idea how to simulate click with onSubmit={handleSubmit(this.update)
export class EditDevice extends Component {
update = device => {
console.log(device, 'device')
if (device.miConfiguration)
device.miConfiguration.isMiEnabled = device.miConfiguration.miConfigurationType !== MiConfigurationTypes.AccessPointOnly
this.props.update(device).then(({success, ...error}) => {
if (!success)
throw new SubmissionError(error)
this.returnToList()
})}
returnToList = () => this.props.history.push({pathname: '/setup/devices', state: {initialSkip: this.props.skip}})
render = () => {
let {isLoadingInProgress, handleSubmit, initialValues: {deviceType} = {}, change} = this.props
const actions = [
<Button
name='cancel'
onClick={this.returnToList}
>
<FormattedMessage id='common.cancel' />
</Button>,
<Button
name='save'
onClick={handleSubmit(this.update)}
color='primary'
style={{marginLeft: 20}}
>
<FormattedMessage id='common.save' />
</Button>]
return (
<Page onSubmit={handleSubmit(this.update)} title={<FormattedMessage id='devices.deviceInfo' />} actions={actions} footer={actions}>
<form >
{isLoadingInProgress && <LinearProgress mode='indeterminate'/>}
<div style={pageStyles.gridWrapper}>
<Grid container>
<Grid item xs={12}>
<Typography type='subheading'>
<FormattedMessage id='devices.overview' />
</Typography>
</Grid>
</form>
</Page>
)
}
}
describe.only('EditPage', () => {
let page, submitting, touched, error, reset, onSave, onSaveResponse, push
let device = {miConfiguration:{isMiEnabled: 'dimon', miConfigurationType: 'Type'}}
let update = sinon.stub().resolves({success: true})
let handleSubmit = fn => fn(device)
beforeEach(() => {
submitting = false
touched = false
error = null
reset = sinon.spy()
})
const props = {
initialValues:{
...device,
},
update,
handleSubmit,
submitting: submitting,
deviceId:999,
history:{push: push = sinon.spy()},
skip: skip,
reset,
}
page = shallow(<EditDevice {...props}/>)
it('should call push back to list on successful response', async () => {
let update = sinon.stub().resolves({success: true})
let device = {miConfiguration:{isMiEnabled: 'dimon', miConfigurationType: 'Type'}}
page.find(Field).findWhere(x => x.props().name === 'name').simulate('change', {}, 'good name')
await page.find(Page).props().footer.find(x => x.props.name === saveButtonName).props.onClick()
push.calledOnce.should.be.true
push.calledWith({pathname: '/setup/devices', state: {initialSkip: skip}}).should.be.true
})
})
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Im struggling to find a solution when testing submit code within a hander. I'am currently exporting two variants of my form components as described in the react-redux documentation.
Component
Test