I’ve created a mock state in my unit test, however, when I run the test, I get a message that says: TypeError: Cannot read property 'flags' of undefined
.
I found a similar post on StackOverflow and I followed the solution there: https://stackoverflow.com/questions/56399446/mocking-store-getstate, however, I still keep getting the undefined state error 🙁
Help is appreciated!
My function:
import {store} from "@store/Store";
const {getState} = store;
export const myFunc = () => {
const state = getState();
const flag = state.flags.booleanFlag;
return flag ? "valid message" : "invalid message";
}
My unit test:
import {store} from "@store/Store";
jest.mock("../../app/store/Store");
const mockState = {
flags: {
booleanFlag: true,
}
};
store.getState = () => mockState;
describe('Utils', () => {
describe('myFunc', () => {
it('should return the correct message when the flag is true', () => {
const expected = "valid message";
expect(myFunc()).toEqual(expected);
});
});
});
Note: The actual state of my application is as follows:
{
flags: {
booleanFlag: false,
}
}