mardi 25 février 2020

Enzyme tests(snapshot) do not work if there is a @observer(mobx) annotation in the component being tested

People! I have a problem with writing tests for the project. The project has a mobx store. Enzyme tests(snapshot) do not work if there is a @observer(mobx) annotation in the component being tested.

Getting an error:

Test suite failed to run TypeError: Cannot read property 'componentWillReact' of undefined

Test:

const props = {
  store: {...store},
};

describe('ViewModal component testing', () => {

  it('mount to dom', async () => {
    const component = shallow(
            <Provider {...props}>
                <ViewModal />
            </Provider>);
    expect(component).toMatchSnapshot();
  });
});

Component for test:

@inject('store')
@observer
export default class ViewModal extends React.Component<IProps> {
   componentDidMount() {
     this.props.store.getItem();
   }

  render() {
    const {item} =  this.props.store;
    return (
        <Row>
            {item}
        </Row>
    );
  }

}

If you remove @observer, the test is successful. How do I make the test run successfully with @observer in the component?

Sorry, my English very bad.))

Aucun commentaire:

Enregistrer un commentaire