I'm a writing a test with Jest and Enzyme to test the functionality of a component in a React Native application.
There is a parent component and a child component that used a method in the parent to instantiate.
To elaborate, here is a abridged version of the parent class:
const TeamDetail = React.createClass({
propTypes: {
team: PropTypes.object.isRequired,
...[INSERT MANY REQUIRED PROPS HERE]
viewTeamMessageFeed: PropTypes.func.isRequired,
},
... INIT LOGIC ...
render() {
return (
<TabContainer
containerStyle={styles.tabContainer}
primaryTab={'board'}
tabs={this.tabs()}
/>
)
},
tabs() {
let tabs = [
this.tab('board', this.renderBoard()),
];
return (tabs);
},
tab(name: string, panel: Object) {
let empty = true;
return({name: name, panel: panel, empty: empty})
},
renderBoard() {
return(
<LeagueTeamAbout
item={this.props.team}
details={this.teamDetails()}
/>
)
},
...MORE METHODS...THIS NEXT ONE IS IMPORTANT:
renderMessagingButton() {
if (this.props.canMessage) {
return (
<StyledButton buttonText='Messaging' onPress={() => this.props.viewTeamMessageFeed(this.props.team)} />
);
}
return null;
...
OK, so that's the parent.
I'm testing the parent component <LeagueTeamAbout/>
That component has a details property that is set by the teamDetails() method. teamDetails()
returns a details object that includes a messaging attribute that is set by the renderMessagingButton()
method.
I'm trying to write a test that expects the messagingButton
to execute the viewTeamMessageFeed()
method upon being tapped by the user.
How do I write this test? I need to somehow call renderMessagingButton()
to mock the LeagueTeamAbout
details object.
How do I call the parent component's renderMessagingButton()
function from the LeagueTeamAbout-test.js
file?