Background Image
Table of Contents Table of Contents
Previous Page  288 / 478 Next Page
Information
Show Menu
Previous Page 288 / 478 Next Page
Page Background

4. In the real world, when setting up a namespace like this, you’d want to follow a sort of “add-or-create” pattern,

so that, if there’s already a

window.Superlists

in the scope, we extend it rather than replacing it.

win

dow.Superlists = window.Superlists || {}

is one formulation, and jQuery’s

$.extend

is another

possibilty. But, there’s already a lot of content in this chapter, and I thought this was probably one too many

things to talk about!

The upshot of all this is: the only way this test will pass is if our

initialize

function

binds the

click

event on

id_login

to the method

.id.request

of the object we pass

it. If we get the tests passing when we use the mock object, we are reassured that our

initialize

function will also do the right thing when we give it a real object on our

real page.

Does that make sense? Let’s play around with the test and see if we can get the hang

of it.

When testing events on DOM elements, you need an actual element

to trigger events against, and to register listeners on. If you forget, it’s

a particularly fiendish test bug, because

.trigger

will just silently no-

op, and you’ll be left scratching your head about why it’s not work‐

ing. So don’t forget to add the

<div id="id_login">

inside the

qunit-

fixture

div!

Our first error is this:

1. Died on test #1

@file:///workspace/superlists/accounts/static/tests/tests.html:35:

Superlists is not defined

That’s the equivalent of an

ImportError

in Python. Let’s start work on

accounts/static/

accounts.js

:

accounts/static/accounts.js.

window

.

Superlists

=

null

;

Just as in Python we might do

Superlists = None

, here we do

window.Superlists =

null

. Using

window.

makes sure we get the global object:

1. Died on test #1

@file:///workspace/superlists/accounts/static/tests/tests.html:35:

Superlists is null

OK, next baby step or two:

accounts/static/accounts.js.

window

.

Superlists

=

{

Accounts

:

{}

};

gives:

4

260

|

Chapter 15: User Authentication, Integrating Third-Party Plugins, and Mocking with JavaScript