A
A
anamorph2017-08-23 09:52:33
JavaScript
anamorph, 2017-08-23 09:52:33

How to test what happens after a button is clicked in React(Jest, enzyme)?

There is a button, when pressed, it gives some kind of action and changes its text instead of save to saving for a couple of seconds
btn.simulate('click') does not give the desired result. In the test, as the text Save remained, it remains

it('click on save-button should change inner text', () => {
     console.log(saveBtn.props().submitting)  // false и значит текст Save
    saveBtn.props().onClick() // или saveBtn.simulate('click') 
    console.log(saveBtn.props().submitting)  // false и значит текст Save, но должен быть true, а текст Saving ...
 })

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Drapeza, 2017-08-23
@anamorph

You don't really need to test this.
Separate testing parts of a React + Redux app:
1) Separate the reducer and the corresponding actions, which is very easy to do, since these are pure functions and simple objects.
2) A separate react component, and you only need to check that when simulate('click') the corresponding handler is called.
Example:

const props = {
    onClick: jest.fn()
};
let component;

beforeEach(() => {
    component = enzyme.shallow(<Button {...props} />);
});

it('срабатывает событие onClick', () => {
    field.simulate('click');
    expect(props.onClick).toHaveBeenCalled();
});

If you need to check if a component reacted to a change in its properties, then you change its properties directly via component.setProps({ key: 'value' });

V
Vahe, 2017-08-23
@vahe_2000

// ....
const wrapper = shallow(<Button/>);
wrapper.find('button').simulate('click');

//....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question