TypeError: componentInstance.getAllHouses не является функцией - PullRequest
0 голосов
/ 26 мая 2020

Я получаю эту ошибку и не могу понять, как ее обойти или исправить ...

У нас есть тест с Jest:

it('renders houses correctly', () => {
    const component = renderer
        .create(<BrowserRouter><HouseList/></BrowserRouter>); //BrowserRouter or else 
                                                              //Invariant failed: You should not use <Link> outside a <Router>


    let componentJson = component.toJSON();

    let componentInstance = component.getInstance();

    expect(componentJson).toMatchSnapshot();

    // manually trigger the get
    componentInstance.getAllHouses(true); //TypeError: componentInstance.getAllHouses is not a function


    // re-rendering
    componentJson = component.toJSON();
    expect(componentJson).toMatchSnapshot();
});

И у нас есть a HouseList:

export default class HouseList extends Component {
    constructor(props) {
        super(props);

        this.state = {
            houses: []
        };
    }

    componentDidMount() {
        this.getAllHouses();
    }

    getAllHouses(isMock) {
        if(isMock === true){
            jest.mock("axios");
        }
        axios.get("http://localhost:8080/house")
            .then(response => response.data)
            .then((data) => {
                this.setState({houses: data});
            });

        return this.state.houses;
    }

    render() {
              //etc.
    }
}

Почему написано, что функции нет? Это потому, что я использовал jest.mock? Моя цель - иметь настоящий вызов ax ios к бэкэнду (который уже работает) и поддельный класс ax ios, реализующий get, который возвращает фиксированный массив домов.

Я уже сделал mock-axe ios, но я также не совсем уверен, как переключаться между ними с помощью if-statement. Я предполагал, что если я использую jest.mock("axios"), он автоматически заглянет в каталог / папку __mocks__ и получит оттуда топор ios.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...