Has anyone got any advice on how to unit test this component? I'm struggling to work out how to mock getFoo correctly and then wait for its promise to resolve before running my tests. I'm currently using Jest to try and do this.
import { getFoo } from "../../../api/fooApi";
class Accordion extends React.Component {
constructor(props) {
super(props);
this.state = {
isActive: false,
isLoaded: false
};
}
componentDidMount() {
getFoo(this.props.id).then(
result => {
this.setState({
isLoaded: true,
foo: result
});
},
error => {
this.setState({
isLoaded: true,
error
});
}
);
}
render() {
const { isActive, isLoaded, foo } = this.state;
if (!isLoaded) return <Spinner />;
return (
<Accordion styled>
<Accordion.Title
active={isActive}
onClick={() => {
this.setState({ isActive: !isActive });
}}
>
<Icon name="dropdown" />
{foo}
</Accordion.Title>
<Accordion.Content active={isActive}>
{foo}
</Accordion.Content>
</Accordion>
);
}
}
This is the actual method.
export async function getFoo(id) {
const res = await fetch(`/api/foo/${id}`);
if (res.status !== 200) throw Error;
return res.json();
}