У меня есть компонент под названием Dashboard. В методе жизненного цикла componentDidMount я вызываю асинхронный вспомогательный метод. Сейчас в моем модульном тесте я пытался заглушить один и тот же метод для проверки как вызова метода, так и ответа
Я пытался sinon.stub, чтобы заглушить метод перед монтированием компонента
import React from 'react';
import { getEvents} from '../actions';
class Dashboard extends React.Component {
//constructor
componentDidMount() {
getEvents().then(response => {
//does something
});
//other methods
}
import * as actions from '../../actions';
import sinon from 'sinon';
import Dashboard from '../index';
import { mount } from 'enzyme';
import { expect } from 'chai';
describe('testing dashboard', () => {
it('should call get events helper method', () => {
const stub = sinon.stub(actions, 'getEvents').returns(Promise.resolve({}));
const wrapper = mount(<Dashboard/>);
expect(stub).to.have.property('callCount', 1);
})
})
Утверждение не выполняется с нулевым счетчиком вызовов