У меня есть компонент нижнего колонтитула, на котором есть несколько кнопок. Все эти кнопки используют константу Message
, которая является модальным для antd:
Message.jsx
import { Modal } from 'antd';
const { confirm } = Modal;
export const Message = (text, okayHandler, cancelHandler) => {
confirm({
title: text,
okText: 'Yes',
cancelText: 'No',
onOk: okayHandler,
onCancel: cancelHandler,
});
};
export default Message;
Footer.jsx
class Footer extends Component {
state = {
from: null,
redirectToReferrer: false,
};
cancelClicked = () => {
Message('Return to the previous screen?', () => {
this.setState({ redirectToReferrer: true, from: '/home' });
});
};
render() {
const { redirectToReferrer, from } = this.state;
if (redirectToReferrer) {
return <Redirect to={{ pathname: from }} />;
}
return (
<Card style={footerSyles}>
<Button
bounds={`${(3 * width) / 7 + (7 * width) / 84},5,${width / 7},30`}
text="CANCEL"
type="primary"
icon="close"
actionPerformed={this.cancelClicked}
/>
</Card>
//actionPerformed is actually onClick, it's taken as a prop from my <Button /> component. Card is an antd component while button is not.
Я просто хочу проверить, когда нажата кнопка , вызывается cancelClicked . Если возможно, я хочу проверить, правильно ли изменяется состояние при нажатии кнопки «ОК» на модале. Мой тест выглядит следующим образом, но тест не пройден, потому что функция не вызывается:
Footer.test
const defaultFooter = shallow(<Footer position="relative" bounds="10,0,775,100" />);
test('Footer should open a popup when cancel button is clicked, and redirect to home page', () => {
const instance = defaultFooter.instance();
const spy = jest.spyOn(instance, 'cancelClicked');
instance.forceUpdate();
const p = defaultFooter.find('Button[text="CANCEL"]');
p.simulate('click');
expect(spy).toHaveBeenCalled();
});
Я также пытался использовать маунт, а не мелкий, но шпион до сих пор не вызван.