I have a method/function in actual JS file(main.js) that goes like this:
function returnPath() {
return `path=${encodeURIComponent(window.location.href.replace(window.location.origin, ''))}`;
}
Now, in my jasmine spec file (mainSpec.js), I have the below code:
describe('Test returnPath function', () => {
it('test the path string returned', () => {
const window = {
location: {
href: "https://www.example.com/hello/world",
origin: "https://www.example.com/"
}
};
const pathStr = returnPath();
// assert
expect(pathStr).toBe('path=hello%2Fworld');
});
});
But, when I run my test, it fails as the window.location.href does not point to the custom object initialized in the 'it' block.
How to inject/provide objects needed by the actual function from the spec file?