![Show Menu](styles/mobile-menu.png)
![Page Background](./../common/page-substrates/page0300.png)
7. You can’t mock out
window.location.reload
, so instead you have to define an (untested) function called
Superlists.Accounts.refreshPage
, and then put a mock on
that
to check that it gets set as the
Ajax
.done
callback.
8 assertions of 8 passed, 0 failed.
And I ended up with this code:
accounts/static/accounts.js.
navigator
.
id
.
watch
({
loggedInUser
:
user
,
onlogin
:
function
(
assertion
) {
$
.
post
(
urls
.
login
,
{
assertion
:
assertion
,
csrfmiddlewaretoken
:
token
}
);
}
});
Logout
At the time of writing, the “onlogout” part of the watch API’s status was uncertain. It
works, but it’s not necessary for our purposes. We’ll just make it a do-nothing function,
as a placeholder. Here’s a minimal test for that:
accounts/static/tests/tests.html (ch15l053).
test("onlogout is just a placeholder", function () {
Superlists.Accounts.initialize(mockNavigator, user, token, urls);
var onlogoutCallback = mockNavigator.id.watch.firstCall.args[0].onlogout;
equal(typeof onlogoutCallback, "function", "onlogout should be a function");
});
And we get quite a simple logout function:
accounts/static/accounts.js (ch15l054).
},
onlogout
:
function
() {}
});
More Nested Callbacks! Testing Asynchronous Code
This is what JavaScript’s all about folks! Thankfully, sinon.js really does help. We still
need to test that our login
post
methods
also
set some callbacks for things to do
after
the POST request comes back:
.
done
(
function
() {
window
.
location
.
reload
(); })
.
fail
(
function
() {
navigator
.
id
.
logout
();});
I’m going to skip testing the
window.location.reload
, because it’s a bit unnecessarily
complicated,
7
and I think we can allow that this will be tested by our Selenium test. We
will do a test for the on-fail callback though, just to demonstrate that it is possible:
272
|
Chapter 15: User Authentication, Integrating Third-Party Plugins, and Mocking with JavaScript