Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Store Helpers #174

Open
satheshrgs opened this issue Jan 8, 2020 · 1 comment
Open

Issue with Store Helpers #174

satheshrgs opened this issue Jan 8, 2020 · 1 comment

Comments

@satheshrgs
Copy link

I have a scenario in which a method uses Store Helper to get State. When I try to test the function using the mock store which I have created, it uses the original store instance ( since it is in a helper file).

code.js

export const testFunction() {
const res = getValueFromHelper();
console.log(res);
}

helper.js

export const getValueFromHelper = () => store.getState.SomeReducer;

code.test.js

const store = mockStore({ SomeReducer:{} })
console.log(store.getState().SomeReducer);  // Here value is correct
testFunction(); // But here it returning original state value

@dmitry-zaets
Copy link
Collaborator

Probably you have a typo here:

export const getValueFromHelper = () => store.getState.SomeReducer;

should be

export const getValueFromHelper = () => store.getState().SomeReducer;

Also you are referring to the global variable called store in your getValueFromHelper function.

So here you have 2 options.

  1. Assign a mocked store into this variable.
  2. Avoid using a global state variable and pass the store as a parameter to the function (preferred, to avoid dependencies)

helper.js

export const getValueFromHelper = (store) => store.getState.SomeReducer;

code.test.js

const store = mockStore({ SomeReducer:{} })
console.log(store.getState().SomeReducer);  // Here value is correct
testFunction(store); // But here it returning original state value

code.js

export const testFunction(state) {
  const res = getValueFromHelper(state);
  console.log(res);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants