I have an issue with getting value of nested props function.
this.props.user.getPerson().getData()
getPerson
returns new object with person related data.
getData
returns person related data.
It's separated for readability and I want to make it work for unit testing.
Test 1:
let _wrapper,
initialProps
beforeEach(() => {
initialProps = {
user: {
getPerson: () => {}
}
}
_wrapper = shallow(<Test1 {...initialProps} />)
})
some tests...
})
It returned TypeError: Cannot read property 'getData' of undefined
.
Test2
let _wrapper,
initialProps
beforeEach(() => {
initialProps = {
user: {
getPerson: () => { getData: () => {} }
}
}
_wrapper = shallow(<Test2 {...initialProps} />)
})
some tests...
})
It returned TypeError: Cannot read property 'getData' of undefined
Same error as Test1.
I tried to get value of props function by passing function however it didn't work from second function.
How can I get value of nested props function?
You need to wrap object literals in parentheses to return them from arrow functions:
initialProps = {
user: {
getPerson: () => ({ getData: () => { } }) // wrap in parentheses
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments