P
P
Pavel Didenko2019-12-19 10:47:32
Software testing
Pavel Didenko, 2019-12-19 10:47:32

How to test a component with a local MobX store?

There is a class component, in its constructor the local store MobX is initialized like this:

constructor(props) {
        super(props);
        this.store = new SubscribeStore({
            userId: this.props.userId,
            type: this.props.isFollowers ? 'followers' : 'subscriptions'
        });
    }

As a result, the store is in this and when writing tests, I can’t find a way to manage it.
There is an option to bring this store into the global space and pass it through the Provider, but I would not want to litter the global space. Who has experienced this?
For tests I use Jest and Enzyme

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
camelCaseVlad, 2019-12-19
@camelCaseVlad

Here's a different concept for you when testing. Test the functionality of the component, not the strings and functions of the component. Moreover, it is a mistake to take out private functions in the global space.
Write your tests in such a way that they don't fail if you then rewrite your application, removing mobh from it and rewriting components to hooks, but leaving the former functionality of the component.
Even write unit tests for the user. The user doesn't care if you used a component with mobh or hooks and a context.

P
Pavel Didenko, 2019-12-19
@Dasslier

So far I have made such a decision, maybe it will be useful to someone. But I think it's a crutch.
1) I import my class under test
2) I extend from this class with a new one
3) In the constructor I assign another object with mocks to this.store
4) in componentDidMount I call super.render ()
All this is done by rendering shallow from enzyme, and ShallowRenderer from Jest

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question