JAVASCRIPT, TESTING, AND
DRUPAL
JS, TESTING, AND DRUPAL
What is the current state of JS in Drupal 7? JavaScript testing
Drupal 8
2 Monday, November 5, 12
ABOUT ME
I work for Aten Design Group in Denver, Colorado I am a developer. I like code.
DRUPAL 7
4 Monday, November 5, 12
DRUPAL 7
Drupal is a platform
Unlike other systems, you get many tools out-of-the-box What does this include?
jQuery Drupal
jQuery drupal.js
Plugins: Once, Cookie, Form form.js
jQuery UI ajax.js
6 Monday, November 5, 12
Drupal comes with: jQuery 1.4.4 (2 years old)
Newest version: jQuery 1.8.2 (1 month old)
Core stability, interaction with other modules other JS May miss out on new functionality (e.g, .on())
THERE’S A MODULE FOR THAT
8 Monday, November 5, 12
jQuery Update module will provide you with updates
jQuery 1.8.2 is in the dev version
USING JS
10 Monday, November 5, 12
HOW TO ADD JS
Theme Layer
Module
THEME JS
General UX code
Code that helps with the site’s appearance
Anything that ties heavily to your site’s HTML
12 Monday, November 5, 12
THEME JS
Drop the JS file into your theme folder Add the JS to your theme info file
scripts[] = js/main.js
You can also add via your html.tpl.php file (but you shouldn’t)
14 Monday, November 5, 12
MODULES AND LIBRARIES
Relates to your module’s functionality
MODULES AND LIBRARIES
Use drupal_add_js()
http://is.gd/drupal_add_js
16 Monday, November 5, 12
D R U PA L _ A D D _ J S
Don’t do this. It will include your JS everywhere ... even if your module isn’t being used
17 Monday, November 5, 12
D R U PA L _ A D D _ J S!
This will only include the JS when your hook is going to run. This is a good thing.
18 Monday, November 5, 12
D O N ’ T B U I L D J S
Don’t build your JS within your module
19 Monday, November 5, 12
Put JS in a separate file!
Use Drupal.settings to get at values from your module into JS
U S I N G d r u p a l . s e t t i n g s
21 Monday, November 5, 12
drupal.behaviors allows you to run code when DOM is loaded
It also ties into AJAX completion
This is simple to implement. Kinda.
d r u p a l . b e h a v i o r s E X A M P L E
Use the jQuery.once() plugin if you only want the code to execute on DOM load
23 Monday, November 5, 12
You can also use jQuery directly still May be easier if you’re used to it
You can use $.ajaxComplete()
TESTING
25 Monday, November 5, 12
We’ve added our JS
What about making sure this code works, though?
27 Monday, November 5, 12
Testing is good
JS testing is important because:
Front end code can change any stage in your project Browsers
29 Monday, November 5, 12
T E S T S WA R M
Automated testing with QUnit
31 Monday, November 5, 12
Test Swarm is an active project
It’s involved in the getting tests available in D8 And ... it allows you to create your own tests
Implement hook_testswarm_tests()
Create a list of tests for your module/JS Tests are written in QUnit
TESTING
33 Monday, November 5, 12
DRUPAL 8
35 Monday, November 5, 12
Testing: http://drupal.org/node/237566
Don’t use drupal_add_js: http://drupal.org/node/1737148 New libraries: http://drupal.org/node/1149866
Lots of other changes: http://groups.drupal.org/node/226738
AMD, jQuery dependencies
The future is coming along fast
Many of the changes look to create some good practices Smaller JS profiles
DRUPAL 8
37 Monday, November 5, 12
Get involved, find a way to contribute Voice an opinion
Feature freeze is coming up!