Я запускаю тест с Jest для запуска в тесте, который должен проверять, прошла ли функция через props
с использованием избыточных отправок.
Вот тест:
describe("when mounted", () => {
beforeEach(() => {
props.fetchBitcoin = mockFetchBitcoin;
loot = mount(<Loot {...props} />);
});
it("fires the `fetchBitcoin()` from the props", () => {
expect(mockFetchBitcoin).toHaveBeenCalled();
});
});
Но когда я запускаю
this.props.fetchBitcoin();
, я получаю следующую ошибку
TypeError: this.props.fetchBitcoin is not a function
9 | }
10 | componentDidMount() {
> 11 | this.props.fetchBitcoin();
| ^
12 | }
13 | }
Компонент, который получает реквизит, выглядит так:
import React from "react";
import { Component } from "react";
import { connect } from "react-redux";
import { fetchBitcoin } from "../actions/bitcoin";
export class Loot extends Component {
render() {
return <h3>Bitcoin balance:</h3>;
}
componentDidMount() {
this.props.fetchBitcoin();
}
}
export default connect(state => state, { fetchBitcoin })(Loot);
И выборка Bitcoin () выглядит так:
import { FETCH_BITCOIN } from "./constants";
export const fetchBitcoin = () => {
return dispatch => {
return fetch("https://api.coindesk.com/v1/bpi/currentprice.json")
.then(response => response.json())
.then(json => dispatch({ type: FETCH_BITCOIN, bitcoin: json }));
};
};