Using Jasmine to test window.location calls

25,868 views
Skip to first unread message

Lalish-Menagh, Trevor

unread,
Jan 12, 2011, 3:56:17 PM1/12/11
to jasmi...@googlegroups.com
Hi all,

I want to test a function that reloads a web page:

loadLocale = function (locale) {
window.location = window.location.href + "?locale=" + locale;
}

With Jasmine I would think that this would work:

describe("loadLocale", function () {
it("loads the current page with a given locale", function () {
loadLocale("en");
expect(window.location.search).toEqual("?locale=en");
});
});

But I get a stack overflow:

js: exception from uncaught JavaScript throw: java.lang.StackOverflowError

Any ideas on how to test this?

Yours,
Trevor
--
Trevor Lalish-Menagh
tr...@trevmex.com
484.868.6150 (mobile)
trevmex (AIM)

Jeff Deville

unread,
Jan 12, 2011, 4:12:12 PM1/12/11
to jasmi...@googlegroups.com
Hey Trevor, 
Granted this is day 1 of Jasmine for me, but it seems like you'd need to create a spy for window.location, or you'd wind up actually changing the page the test runner is on.

Course...  jasmine could do things quite differently here.  :-/
-j


--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.


Erik Hanson

unread,
Jan 12, 2011, 7:59:51 PM1/12/11
to jasmi...@googlegroups.com
On Wed, Jan 12, 2011 at 12:56 PM, Lalish-Menagh, Trevor
<tr...@trevmex.com> wrote:
> I want to test a function that reloads a web page:
>
> loadLocale = function (locale) {
>    window.location = window.location.href + "?locale=" + locale;
> }

> Any ideas on how to test this?

I normally make a function like this:

window.redirect = function(destination) {
window.location = destination;
}

which I don't test with Jasmine. Then I'll use that in my production
code instead of setting window.location directly, and make a spy for
it in my tests.


-- Erik

Frank Schwieterman

unread,
Jan 12, 2011, 9:04:00 PM1/12/11
to jasmi...@googlegroups.com
spyOn(window, "location") would be ideal but IIRC is not allowed on
some browsers. Trev suggests a good solution, of wrapping it and
spying on the wrapper.

One thing you can do though, is let your code modify
window.location, only using inpage anchors. Remember setting
"window.location = "#inpageAnchor" won't actually navigate the
browser, just scroll to the <a name?="inpageAnchor"> if its there.
Your code should probably be configurable to target a particular
server, configure it to use (window.location + "#/") as the current
server, then set your expectations against window.location (trimming
left on the #).

Anil S. Bidve

unread,
Aug 4, 2017, 11:50:10 AM8/4/17
to Jasmine
window.location = "#inpageAnchor" won't actually navigate the
browser

The above solution works. Thank fschwiet.
Reply all
Reply to author
Forward
0 new messages