I am trying to test a simple function that makes a call to window.location.search. I'm trying to understand how to stub this call so that I can return a url of my choosing.
getParameterByName: (name) => name = name.replace(/[\/, "\\\[").replace(/[\]]/, "\\\]") regexS = "[\\?&]" + name + "=([^&#]*)" regex = new RegExp(regexS) results = regex.exec(window.location.search) //Stub call to window.location.search if(results == null) return "" else return decodeURIComponent(results.replace(/\+/g, " "))
describe "Data tests", () -> it "Should parse parameter from url", () -> data = new Data() console.log("search string: " + window.location.search) //prints "search string:" window.location.search = "myUrl" console.log("search string: " + window.location.search) //prints "search string:" console.log(data.getParameterByName('varName')) expect(true).toBe(true)
My original attempt was to return a value directly like so:
This, of course, doesn't work. I don't think I'm specifying the stub correctly, but it shows my intent.
Any ideas on how to solve this would be greatly appreciated.