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

Aaaand we run our FT:

$

python3 manage.py test functional_tests.test_list_item_validation.\

ItemValidationTest.test_error_messages_are_cleared_on_input

[...]

Ran 1 test in 3.023s

OK

Hooray! That’s a commit!

Javascript Testing in the TDD Cycle

You may be wondering how these JavaScript tests fit in with our “double loop” TDD

cycle. The answer is that they play exactly the same role as our Python unit tests.

1. Write an FT and see it fail.

2. Figure out what kind of code you need next: Python or JavaScript?

3. Write a unit test in either language, and see it fail.

4. Write some code in either language, and make the test pass.

5. Rinse and repeat.

Want a little more practice with JavaScript? See if you can get our

error messages to be hidden when the user clicks inside the input

element, as well as just when they type in it. You should be able to FT

it too.

Columbo Says: Onload Boilerplate and Namespacing

Oh, andone last thing.Whenever youhave some JavaScript that interactswith theDOM,

it’s always good to wrap it in some “onload” boilerplate code to make sure that the page

has fully loaded before it tries to do anything. Currently it works anyway, because we’ve

placed the

<script>

tag right at the bottom of the page, but we shouldn’t rely on that.

The jQuery

onload

boilerplate is quite minimal:

lists/static/list.js.

$

(

document

).

ready

(

function

() {

$

(

'input'

).

on

(

'keypress'

,

function

() {

$

(

'.has-error'

).

hide

();

});

});

234

|

Chapter 13: Dipping Our Toes, Very Tentatively, into JavaScript