У меня есть этот код в моем компоненте, который принимает listUsers как требуемую функцию PropType. Я хочу проверить, что в componentDidMount() его нужно вызывать только один раз.
Код компонента:
static propTypes = {
securityMode: PropTypes.string.isRequired,
listUsers: PropTypes.func.isRequired
};
componentDidMount() {
const { listUsers } = this.props;
listUsers();
}
onCreateUserSucess= response => {
this.setState({ isCreateUserModalOpen: false });
const { listUsers, notify } = this.props;
listUsers();
this.closeCreateUserModal();
notify({
title: 'Created User',
message: `User ${response.username} was added to group: ${response.group}`,
status: STATUS.success,
dismissible: true,
dismissAfter: 3000
});
};
Я получаю сообщение об ошибке, говорящее, что spyOn может применяться только к функциям. Может ли кто-нибудь помочь мне протестировать. componentDidMount и onCreateUserSucess. Я даже пытался издеваться над функциями, но всегда получаю ошибки.



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Тестировать componentDidMount довольно просто. Предположим, что компонент, который вы создали выше, называется UsersDisplayer.
class UsersDisplayer extends Component {
constructor(props) {
// ...
}
// The code above goes here.
}
Чтобы проверить, работает ли функция listUsers, вы должны сделать что-то похожее на это:
// Import React, shallow and UsersDisplayer at the top.
describe('<UsersDisplayer />', () => {
let usersDisplayerWrapper;
let listUsers;
beforeEach(() => {
listUsers = jest.fn();
usersDisplayerWrapper = <UsersDisplayer listUsers = {listUsers} />;
});
describe('componentDidMount', () => {
it('calls listUsers props function', () => {
// The `componentDidMount` lifecycle method is called during the rendering of the component within the `beforeEach` block, that runs before each test suites.
expect(listUsers).toHaveBeenCalled();
});
});
describe('onCreateUserSucess', () => {
it('calls listUsers props function', () => {
// Create a dummy `response` data that will be passed to the function.
const response = {
username: 'username',
group: 'group'
};
// Simply just call the function of the instance.
usersDisplayerWrapper.instance().onCreateUserSucess(response);
expect(listUsers).toHaveBeenCalled();
});
});
});