I tried all the possibilities to mock location.pathname
This is my code
if(typeof location !== undefined) {
this.setParentPath(location.pathname);
}
This is one of the possibility I used to cover the location
as undefined
it('should gets the parentPath without location', async () => {
const locationSpy = jest.spyOn(location);
locationSpy.mockImplementation(() => undefined);
await asyncMount(
<Provider UsersStore={UsersStore} AppStore={AppStore}>
<Users />
</Provider>,
);
expect(UsersStore.tppPath).toEqual('');
});
It's giving me this error
Error: Cannot spy the undefined property because it is not a function; undefined given instead
at ModuleMockerClass.spyOn (/Users/arpit/Documents/prakhar/new/developer-portal/node_modules/jest-mock/build/index.js:789:15)
at Object.<anonymous> (/Users/arpit/Documents/prakhar/new/developer-portal/src/components/users/Users.spec.jsx:190:38)
at Object.asyncJestTest (/Users/arpit/Documents/prakhar/new/developer-portal/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:100:37)
at /Users/arpit/Documents/prakhar/new/developer-portal/node_modules/jest-jasmine2/build/queueRunner.js:47:12
at new Promise (<anonymous>)
at mapper (/Users/arpit/Documents/prakhar/new/developer-portal/node_modules/jest-jasmine2/build/queueRunner.js:30:19)
at /Users/arpit/Documents/prakhar/new/developer-portal/node_modules/jest-jasmine2/build/queueRunner.js:77:41
at processTicksAndRejections (internal/process/task_queues.js:97:5)
Just focusing on location or window elements for this question though..
Please suggest any solution specific to this only not for any
MemoryRouter
Or any other react-router component