JavaScript Unit Tests Involving External Components: Our
First Mocks!
To get our FT further, we’re going to need to get the Persona window to pop up. For
that, we’ll need to de-spike our client-side JavaScript code that uses the Persona libraries.
We’ll test-drive that using JavaScript unit tests and mocking.
Housekeeping: A Site-Wide Static Files Folder
A bit of housekeeping first: create a site-wide static files directory inside
superlists/
superlists
, and move all the Bootsrap CSS, QUnit code, and
base.css
into it, so it looks
like this:
$
tree superlists -L 3 -I __pycache__
superlists
├── __init__.py
├── settings.py
├── static
│ ├── base.css
│ ├── bootstrap
│ │ ├── css
│ │ ├── fonts
│ │ └── js
│ └── tests
│ ├── qunit.css
│ └── qunit.js
├── urls.py
└── wsgi.py
6 directories, 7 files
Always do a commit before and after a bit of housekeeping like this.
That means adjusting our existing JavaScript unit tests:
lists/static/tests/tests.html (ch15l020).
<link
rel=
"stylesheet"
href=
"../../../superlists/static/tests/qunit.css"
>
[...]
<script
src=
"http://code.jquery.com/jquery.min.js"></script>
<script
src=
"../../../superlists/static/tests/qunit.js"
></script>
<script
src=
"../list.js"
></script>
And we check they still work, by opening them up in a browser:
256
|
Chapter 15: User Authentication, Integrating Third-Party Plugins, and Mocking with JavaScript