SlideShare a Scribd company logo
1 of 69
Packaging Your Code with
                Testable jQuery Plugins
                        Andy Peterson     




                            Testing, Plugins, Integrating, etc    




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
You


                  jQuery Plugins?

                  Tested Javascript?

                  Selenium or other UI tests?




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Andy Peterson

                        started with commercial & enterprise
                        desktop software (Mac), then consulting

                        agile practices and disciplines

                        Javascript-- dabbled for the last few
                        years, but over the last 9 months, feel like
                        it’s become a part of my professional
                        practice


Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Software services company with offices in San
                        Francisco and Los Angeles. 20 people

                        We're an agile shop, focus on RoR & Java webapp
                        dev along with more mobile

                        1/2 our work is bootstrapping startups




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Why am I here?

                  We didn't have a good story about Javascript
                  testing

                  Which led us to develop practices

                  Which fed back into how we approach
                  Javascript coding in general



Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Agenda
                  Javascript testing is hard. Why?

                  demo

                  The Trick: UNIT test + Plugins

                  problems & solutions




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
We Don’t Try




                   Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010
Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010          Copyright (c) 2010 Andrew J. Peterson
Why We Don’t Try

                  Never did it before

                  Had a bad experience

                  Patterns unclear

                  No infrastructure

                  App’s not complicated enough


Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Too May Tools




            http://www.slideshare.net/jeresig/understanding-javascript-testing
Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Nobody Else Does




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
And when we tried...




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
page.[dynamic]

    <html>
    <head>
    <link href=”site.css” type=”text/stylesheet” />
    <script src=‘page.js’ type=”text/javascript”>
    </script>
    <body>
      <h1>Joe’s Page</h1>
      ...
    </body>
    </html>


Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
page.js


         $(function() {
           $(‘h1’).css(‘color’,‘red’);
         });




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Test on the Page?


    Screw.Unit(function() {
        it(‘should change to red’, function() {
        expect($(‘h1’).css(‘color’)).to(equal,‘red’);
      });
    });




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Brings Up Lots of Qs
                  How do I get the markup (if dynamic)?

                  “on ready” timing

                  What happens when other things happen on
                  this page? As it evolves?

                  How do I know it really did something?

                                               (Pre-conditions)


Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Whew!




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Why is it so
                                     complicated?
                  We’re trying to build integration tests--

                        page markup + lifecycle

                        multiple javascript sources

                  Lots of pieces to integrate

                        browsers, test engines, integration servers,
                        developer machines

Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Simplify???




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Plugins + Unit Test
it’s a good solution to lots of problems
Plugins Solve


                  Separate from “on doc ready” timing

                  clear testing pattern

                  provide good modularity pattern

                  decoupled, abstract, encapulation



Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Plugins are Easy to
                             Write
       (function($) {
         $.fn.myPlugin = function(req, opts) {
           return this.each(function() {
             $(this).myStuff...
           });
         }
       })(jQuery);




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
jQuery Plugin Writing

                  http://docs.jquery.com/Plugins/
                  Authoring
                  http://www.learningjquery.com/
                  2007/10/a-plugin-development-
                  pattern
                  look at event patterns


Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Unit Testing Solves
                                   “test one piece at a time”




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Requirements for a Tool...
                  Easy to Install

                  instant feedback & fast for TDD or BDD

                  reliable & automate(able)

                  mocking & stubbing

                  minimal paradigm or language shift

                  any browser

Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Which Javascript Testing
                 Library?

                  Lots to choose from

                        QUnit

                        xUnit Style: JSUnit, YUITest, etc. 

                        BDD Style: JSSpec, JSpec,
                        Jasmine, ScrewUnit 



Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
We Picked Blue Ridge
                    (not by me)
                  Conventions (+ Rails Tools)

                  ScrewUnit (BDD)

                  in & out of browser with Rhino + env.js

                  Provides: TDD, Reliable, headless, little
                  paradigm shift, now multi-browser

                  NOTE: maven plugin too!


Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Install


    ./script/plugin install git://github.com/relevance/blue-
    ridge.git

    ./script/generate blue_ridge

    rake test:javascripts




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
How do we test
                                makeRed?

                        Javascript UNIT tests

                        source markup (fixture) +

                        code under test

                        = reliable, easy entry



Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Rewrite as Plugin


    jquery.make_red.js

        jQuery.fn.makeRed = function() {
          this.css(‘color’,‘red’);
        });




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
HTML “Fixture” for Test

    fixtures/make_red.html
    <head>
      <title>Make Red Results</title>
      <link rel="stylesheet" href="screw.css" type="text/css"
    charset="utf-8"/>
       <script type="text/javascript" src="../../../vendor/plugins/
    blue-ridge/lib/blue-ridge.js"></script>
    </head>
    <body>
      <h1 style=‘color: pink’ >gotta test...</h1>
    </body>


Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Test with “Fixture”
    / make_red_spec.js
     /
    require("spec_helper.js");
    require(".../make_red.js"); / code we are testing
                                  /
    Screw.Unit(function() {
      before(function() {
       expect($(‘h1’).css(‘color’)).to_not(equal,‘red’);
    "        $('h1').makeRed();
        });
        it(‘should change to red’, function() {
            expect($(‘h1’).css(‘color’)).to(equal,‘red’);
      });
    });
Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Test with “Fixture”




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Test with “Fixture”

    $ rake spec:javascript
    (in /Users/ndp/demo)
    Running application_spec.js with fixture 'fixtures/
    application.html'...
    .

    1 test(s), 0 failure(s)
    0.076 seconds elapsed


Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Link to on Page

    <html>
    <head>
    <script src=”.../jquery.make_red.js” />
    <script type=”text/javascript”>
      $(function() {
        $(‘h1’).colorRed();
      });
    </script>



Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Implications of Unit
                  Testing
         (over Integration Testing)


Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Must modularize code
                           (using events is a good approach)




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Build “Fixture” HTML
                              seems a little counter-intuitive




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Alternatives to Fixtures

                  generate dynamically w/ Javascript

                  generate -- figure out how to render your
                  views

                  “bake out” -- eg.   

                        http://pivotallabs.com/users/jb/blog/
                        articles/1152-javascripttests-bind-reality-


Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
May need simple
          integration tests anyhow
                                               (but much less)




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Tricks & Gotchas?
  Plugin + Unit Testing Gotchas
Remember


                  Javascript PLUGIN

                  Fixture (Input) HTML markup

                  Test of Plugin

                  Individual Units



Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Unit Tests

                  Perfect for
                        DOM Manipulation
                        Business rules
                  (This gets you 80-90% there!)

Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Plugin Becomes Complex



                  Use Tests to aid in Refactoring




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
On Doc Ready Big


                  Can use “controller” pattern

                  Can use “composing” plugin

                  (not testing doc ready)




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Ajax


                                                       EZ: Stub/Mock out $.ajax

                                                       $.ajax = function(url, fn) {
                                                         / assert called correctly
                                                          /
                                                       }

                                                       test success: fn separately



Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010        Copyright (c) 2010 Andrew J. Peterson
Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
How to test?
    $.fn.clockify = function() {
        setInterval(function() {
          this.html(Time().toString());
        }, 1000);
    }
    ...
    / test:
      /
        describe('clockify', function() {
          before(function() {
            $('p').clockify();
          });
          it('should update every second', function() {
              / ???
               /
          });
        });
Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Separate setInterval()
    $.fn.clockify = function() {
      var $j = this;
      setInterval(function() {
        $j.updateClock();
      }, 1000);
      return this;
    }
    $.fn.updateClock = function() {
      this.each(function(i, e) {
        $(e).html((new Date()).toString());
      });
    }



Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Separate setInterval()
       describe('clockify', function() {
        before(function() {
          setInterval = mock_function(setInterval);
        });
        it('should call setInterval with 1000', function() {
             setInterval.should_be_invoked().
                      with_arguments(null, 1000).exactly(1);
           $('p').clockify().html('running');
         });
       });



Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Or Even...
    $.fn.interval = function(fn, millis) {
      var $this = this;
      setInterval(function() {
        $this.each(function() {
          fn.apply(this);
        }, millis);
      return this;
    }
    $.fn.updateClock = function() {
      return this.html((new Date()).toString());
    }
    $.fn.clockify = function() {
      return this.interval(function() { $(this).updateClock(); }, 1000);
    }
Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Testing Animations Opts


                  skip

                  stub

                  use integration testing tools




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Animations: Turning Off




    jQuery.fx.off = true


Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Animations: Configure
    $.fn.masterRadios = function(settings) {
        settings = $.extend({},
                      $.fn.masterRadios.defaults,
                      settings);
    ...
          $form.find('.spesh').each(function(index, elem) {
            settings.showFn.call($(elem));
          });
    }

    $.fn.masterRadios.defaults = {
       hideFn: jQuery.fn.hide,
       showFn: jQuery.fn.show
    };

Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Input Markup is Complex
                  (therefore don’t want “fixture”)

                  Simplify

                        Semantic Markup?

                        Can Generate within Javascript (Safely)

                        Separate Concerns

                  Refactor?

Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Markup Evolves



                  Less of a problem than it seems

                  Sometimes you just need integration tests




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Drag & Drop


                  Don’t try to script whole drag process

                  Unit test draggable, droppable

                  Events

                  Trigger & Bind



Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Plugins “Draw Inside the
                    Lines”


                  $(‘div.specific’).plugin()
                  shouldn’t affect other
                  nodes in the doc




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Expect No Change
    <html>
    <body>
      <div class=‘control’>
         ...
      <div id=‘fixture’>
         ...
      <div class=‘control’>
         ...

Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Expect No Change
        var control; / remember!
                      /
        before(function() {
          control = $('.control').html();
        });

      after(function() {
        expect($('.control').html()).to(equal,
    control);
      });

Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Unit Tests v. Small Files
                  Keep code modular

                        Put units of JS in their own files (LOTS)

                        Share small JS with

                              HTML pages

                              Tests

                  Lots of options to combine at deploy time!

Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
CSS?



                  Include CSS in tests

                  Helps visualize




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
What about JS Classes?
                  Hmm...

                  Use

                        object literals

                        closures

                        DOM nodes

                  Is this enuf?

Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Limitations


                  Not testing doc ready

                  Nor Markup generation

                  Nor integration of the two

                  THUS: Selenium-- but simpler



Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Other Benefits
                                             (besides testing)




                  Can move HTML generation where it is most
                  appropriate

                  Can move business logic to client

                  Less “chatty” apps




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Our Experience



                  3 projects

                  definitely a net value add




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Summary
         prefer UNIT tests over
         INTEGRATION tests for ease
         of testability and
         development

         package as jQuery Plugins

         work through tricky problems




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Carbon Five
                        We get to work on diverse projects and learn lots
                        of new technologies.

                        We're always looking for both seasoned
                        developers and junior programmers to work,
                        please feel free to talk to me now or email me




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson
Thanks!



                  andy@carbonfive.com

                  http://github.com/ndp




Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010   Copyright (c) 2010 Andrew J. Peterson

More Related Content

Similar to SF jQuery Conf: Packaging Code With Testable J Query Plugins

Ionic Advisory: Your partner at every stage of development
Ionic Advisory: Your partner at every stage of development Ionic Advisory: Your partner at every stage of development
Ionic Advisory: Your partner at every stage of development Ionic Framework
 
Acs south coast nsw openness
Acs south coast nsw opennessAcs south coast nsw openness
Acs south coast nsw opennessNick Hodge
 
Test Driven Development and Quality Improvement
Test Driven Development and Quality ImprovementTest Driven Development and Quality Improvement
Test Driven Development and Quality ImprovementCarlos Solís
 
2011 - SharePoint + jQuery
2011 - SharePoint + jQuery2011 - SharePoint + jQuery
2011 - SharePoint + jQueryChris O'Connor
 
WTF is Python
WTF is PythonWTF is Python
WTF is Pythonsneeu
 
jQuery From the Ground Up
jQuery From the Ground UpjQuery From the Ground Up
jQuery From the Ground UpKevin Griffin
 
HTML5: Toolkits and Gaps
HTML5: Toolkits and GapsHTML5: Toolkits and Gaps
HTML5: Toolkits and Gapsdylanks
 
Testing the BBC Website
Testing the BBC WebsiteTesting the BBC Website
Testing the BBC Websiteshudderfix
 
Introduction to Protractor - Habilelabs
Introduction to Protractor - HabilelabsIntroduction to Protractor - Habilelabs
Introduction to Protractor - HabilelabsHabilelabs
 
Open Source CDNs | LAWebSpeed April 29th 2014
Open Source CDNs | LAWebSpeed April 29th 2014Open Source CDNs | LAWebSpeed April 29th 2014
Open Source CDNs | LAWebSpeed April 29th 2014Justin Dorfman
 
Meetup AngularJS Rio - Testes e2e para apps AngularJS com Protractor
Meetup AngularJS Rio - Testes e2e para apps AngularJS com ProtractorMeetup AngularJS Rio - Testes e2e para apps AngularJS com Protractor
Meetup AngularJS Rio - Testes e2e para apps AngularJS com ProtractorStefan Teixeira
 
Codeception Testing Framework -- English #phpkansai
Codeception Testing Framework -- English #phpkansaiCodeception Testing Framework -- English #phpkansai
Codeception Testing Framework -- English #phpkansaiFlorent Batard
 
jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');mikehostetler
 
Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)
Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)
Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)Google Developer Relations Team
 
DevOps Hiring
DevOps HiringDevOps Hiring
DevOps Hiringkshep
 
Implementing AutoComplete for Freemarker and Velocity languages in ACE Editor
Implementing AutoComplete for Freemarker and Velocity languages in ACE EditorImplementing AutoComplete for Freemarker and Velocity languages in ACE Editor
Implementing AutoComplete for Freemarker and Velocity languages in ACE Editorpeychevi
 
Updates of socket.io@1.0
Updates of socket.io@1.0Updates of socket.io@1.0
Updates of socket.io@1.0Jxck Jxck
 

Similar to SF jQuery Conf: Packaging Code With Testable J Query Plugins (20)

Agile framework Support
Agile framework SupportAgile framework Support
Agile framework Support
 
Ionic Advisory: Your partner at every stage of development
Ionic Advisory: Your partner at every stage of development Ionic Advisory: Your partner at every stage of development
Ionic Advisory: Your partner at every stage of development
 
Acs south coast nsw openness
Acs south coast nsw opennessAcs south coast nsw openness
Acs south coast nsw openness
 
Test Driven Development and Quality Improvement
Test Driven Development and Quality ImprovementTest Driven Development and Quality Improvement
Test Driven Development and Quality Improvement
 
2011 - SharePoint + jQuery
2011 - SharePoint + jQuery2011 - SharePoint + jQuery
2011 - SharePoint + jQuery
 
WTF is Python
WTF is PythonWTF is Python
WTF is Python
 
jQuery From the Ground Up
jQuery From the Ground UpjQuery From the Ground Up
jQuery From the Ground Up
 
HTML5: Toolkits and Gaps
HTML5: Toolkits and GapsHTML5: Toolkits and Gaps
HTML5: Toolkits and Gaps
 
Testing the BBC Website
Testing the BBC WebsiteTesting the BBC Website
Testing the BBC Website
 
Introduction to Protractor - Habilelabs
Introduction to Protractor - HabilelabsIntroduction to Protractor - Habilelabs
Introduction to Protractor - Habilelabs
 
Open Source CDNs | LAWebSpeed April 29th 2014
Open Source CDNs | LAWebSpeed April 29th 2014Open Source CDNs | LAWebSpeed April 29th 2014
Open Source CDNs | LAWebSpeed April 29th 2014
 
Meetup AngularJS Rio - Testes e2e para apps AngularJS com Protractor
Meetup AngularJS Rio - Testes e2e para apps AngularJS com ProtractorMeetup AngularJS Rio - Testes e2e para apps AngularJS com Protractor
Meetup AngularJS Rio - Testes e2e para apps AngularJS com Protractor
 
Codeception Testing Framework -- English #phpkansai
Codeception Testing Framework -- English #phpkansaiCodeception Testing Framework -- English #phpkansai
Codeception Testing Framework -- English #phpkansai
 
Railsconf 2010
Railsconf 2010Railsconf 2010
Railsconf 2010
 
jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');jQuery('#knowledge').appendTo('#you');
jQuery('#knowledge').appendTo('#you');
 
Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)
Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)
Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)
 
DevOps Hiring
DevOps HiringDevOps Hiring
DevOps Hiring
 
Implementing AutoComplete for Freemarker and Velocity languages in ACE Editor
Implementing AutoComplete for Freemarker and Velocity languages in ACE EditorImplementing AutoComplete for Freemarker and Velocity languages in ACE Editor
Implementing AutoComplete for Freemarker and Velocity languages in ACE Editor
 
Updates of socket.io@1.0
Updates of socket.io@1.0Updates of socket.io@1.0
Updates of socket.io@1.0
 
App Engine Meetup
App Engine MeetupApp Engine Meetup
App Engine Meetup
 

Recently uploaded

Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dashnarutouzumaki53779
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dash
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

SF jQuery Conf: Packaging Code With Testable J Query Plugins

  • 1. Packaging Your Code with Testable jQuery Plugins Andy Peterson      Testing, Plugins, Integrating, etc     Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 2. You jQuery Plugins? Tested Javascript? Selenium or other UI tests? Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 3. Andy Peterson started with commercial & enterprise desktop software (Mac), then consulting agile practices and disciplines Javascript-- dabbled for the last few years, but over the last 9 months, feel like it’s become a part of my professional practice Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 4. Software services company with offices in San Francisco and Los Angeles. 20 people We're an agile shop, focus on RoR & Java webapp dev along with more mobile 1/2 our work is bootstrapping startups Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 5. Why am I here? We didn't have a good story about Javascript testing Which led us to develop practices Which fed back into how we approach Javascript coding in general Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 6. Agenda Javascript testing is hard. Why? demo The Trick: UNIT test + Plugins problems & solutions Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 7. We Don’t Try Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 8. Why We Don’t Try Never did it before Had a bad experience Patterns unclear No infrastructure App’s not complicated enough Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 9. Too May Tools http://www.slideshare.net/jeresig/understanding-javascript-testing Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 10. Nobody Else Does Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 11. And when we tried... Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 12. page.[dynamic] <html> <head> <link href=”site.css” type=”text/stylesheet” /> <script src=‘page.js’ type=”text/javascript”> </script> <body> <h1>Joe’s Page</h1> ... </body> </html> Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 13. page.js $(function() { $(‘h1’).css(‘color’,‘red’); }); Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 14. Test on the Page? Screw.Unit(function() { it(‘should change to red’, function() { expect($(‘h1’).css(‘color’)).to(equal,‘red’); }); }); Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 15. Brings Up Lots of Qs How do I get the markup (if dynamic)? “on ready” timing What happens when other things happen on this page? As it evolves? How do I know it really did something? (Pre-conditions) Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 16. Whew! Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 17. Why is it so complicated? We’re trying to build integration tests-- page markup + lifecycle multiple javascript sources Lots of pieces to integrate browsers, test engines, integration servers, developer machines Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 18. Simplify??? Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 19. Plugins + Unit Test it’s a good solution to lots of problems
  • 20. Plugins Solve Separate from “on doc ready” timing clear testing pattern provide good modularity pattern decoupled, abstract, encapulation Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 21. Plugins are Easy to Write (function($) { $.fn.myPlugin = function(req, opts) { return this.each(function() { $(this).myStuff... }); } })(jQuery); Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 22. jQuery Plugin Writing http://docs.jquery.com/Plugins/ Authoring http://www.learningjquery.com/ 2007/10/a-plugin-development- pattern look at event patterns Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 23. Unit Testing Solves “test one piece at a time” Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 24. Requirements for a Tool... Easy to Install instant feedback & fast for TDD or BDD reliable & automate(able) mocking & stubbing minimal paradigm or language shift any browser Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 25. Which Javascript Testing Library? Lots to choose from QUnit xUnit Style: JSUnit, YUITest, etc.  BDD Style: JSSpec, JSpec, Jasmine, ScrewUnit  Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 26. We Picked Blue Ridge (not by me) Conventions (+ Rails Tools) ScrewUnit (BDD) in & out of browser with Rhino + env.js Provides: TDD, Reliable, headless, little paradigm shift, now multi-browser NOTE: maven plugin too! Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 27. Install ./script/plugin install git://github.com/relevance/blue- ridge.git ./script/generate blue_ridge rake test:javascripts Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 28. How do we test makeRed? Javascript UNIT tests source markup (fixture) + code under test = reliable, easy entry Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 29. Rewrite as Plugin jquery.make_red.js jQuery.fn.makeRed = function() { this.css(‘color’,‘red’); }); Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 30. HTML “Fixture” for Test fixtures/make_red.html <head> <title>Make Red Results</title> <link rel="stylesheet" href="screw.css" type="text/css" charset="utf-8"/> <script type="text/javascript" src="../../../vendor/plugins/ blue-ridge/lib/blue-ridge.js"></script> </head> <body> <h1 style=‘color: pink’ >gotta test...</h1> </body> Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 31. Test with “Fixture” / make_red_spec.js / require("spec_helper.js"); require(".../make_red.js"); / code we are testing / Screw.Unit(function() { before(function() { expect($(‘h1’).css(‘color’)).to_not(equal,‘red’); " $('h1').makeRed(); }); it(‘should change to red’, function() { expect($(‘h1’).css(‘color’)).to(equal,‘red’); }); }); Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 32. Test with “Fixture” Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 33. Test with “Fixture” $ rake spec:javascript (in /Users/ndp/demo) Running application_spec.js with fixture 'fixtures/ application.html'... . 1 test(s), 0 failure(s) 0.076 seconds elapsed Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 34. Link to on Page <html> <head> <script src=”.../jquery.make_red.js” /> <script type=”text/javascript”> $(function() { $(‘h1’).colorRed(); }); </script> Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 35.
  • 36. Implications of Unit Testing (over Integration Testing) Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 37. Must modularize code (using events is a good approach) Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 38. Build “Fixture” HTML seems a little counter-intuitive Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 39. Alternatives to Fixtures generate dynamically w/ Javascript generate -- figure out how to render your views “bake out” -- eg.    http://pivotallabs.com/users/jb/blog/ articles/1152-javascripttests-bind-reality- Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 40. May need simple integration tests anyhow (but much less) Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 41. Tricks & Gotchas? Plugin + Unit Testing Gotchas
  • 42. Remember Javascript PLUGIN Fixture (Input) HTML markup Test of Plugin Individual Units Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 43. Unit Tests Perfect for DOM Manipulation Business rules (This gets you 80-90% there!) Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 44. Plugin Becomes Complex Use Tests to aid in Refactoring Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 45. On Doc Ready Big Can use “controller” pattern Can use “composing” plugin (not testing doc ready) Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 46. Ajax EZ: Stub/Mock out $.ajax $.ajax = function(url, fn) { / assert called correctly / } test success: fn separately Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 47. Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 48. How to test? $.fn.clockify = function() { setInterval(function() { this.html(Time().toString()); }, 1000); } ... / test: / describe('clockify', function() { before(function() { $('p').clockify(); }); it('should update every second', function() { / ??? / }); }); Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 49. Separate setInterval() $.fn.clockify = function() { var $j = this; setInterval(function() { $j.updateClock(); }, 1000); return this; } $.fn.updateClock = function() { this.each(function(i, e) { $(e).html((new Date()).toString()); }); } Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 50. Separate setInterval() describe('clockify', function() { before(function() { setInterval = mock_function(setInterval); }); it('should call setInterval with 1000', function() { setInterval.should_be_invoked(). with_arguments(null, 1000).exactly(1); $('p').clockify().html('running'); }); }); Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 51. Or Even... $.fn.interval = function(fn, millis) { var $this = this; setInterval(function() { $this.each(function() { fn.apply(this); }, millis); return this; } $.fn.updateClock = function() { return this.html((new Date()).toString()); } $.fn.clockify = function() { return this.interval(function() { $(this).updateClock(); }, 1000); } Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 52. Testing Animations Opts skip stub use integration testing tools Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 53. Animations: Turning Off jQuery.fx.off = true Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 54. Animations: Configure $.fn.masterRadios = function(settings) { settings = $.extend({}, $.fn.masterRadios.defaults, settings); ... $form.find('.spesh').each(function(index, elem) { settings.showFn.call($(elem)); }); } $.fn.masterRadios.defaults = { hideFn: jQuery.fn.hide, showFn: jQuery.fn.show }; Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 55. Input Markup is Complex (therefore don’t want “fixture”) Simplify Semantic Markup? Can Generate within Javascript (Safely) Separate Concerns Refactor? Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 56. Markup Evolves Less of a problem than it seems Sometimes you just need integration tests Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 57. Drag & Drop Don’t try to script whole drag process Unit test draggable, droppable Events Trigger & Bind Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 58. Plugins “Draw Inside the Lines” $(‘div.specific’).plugin() shouldn’t affect other nodes in the doc Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 59. Expect No Change <html> <body> <div class=‘control’> ... <div id=‘fixture’> ... <div class=‘control’> ... Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 60. Expect No Change var control; / remember! / before(function() { control = $('.control').html(); }); after(function() { expect($('.control').html()).to(equal, control); }); Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 61. Unit Tests v. Small Files Keep code modular Put units of JS in their own files (LOTS) Share small JS with HTML pages Tests Lots of options to combine at deploy time! Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 62. CSS? Include CSS in tests Helps visualize Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 63. What about JS Classes? Hmm... Use object literals closures DOM nodes Is this enuf? Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 64. Limitations Not testing doc ready Nor Markup generation Nor integration of the two THUS: Selenium-- but simpler Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 65. Other Benefits (besides testing) Can move HTML generation where it is most appropriate Can move business logic to client Less “chatty” apps Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 66. Our Experience 3 projects definitely a net value add Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 67. Summary prefer UNIT tests over INTEGRATION tests for ease of testability and development package as jQuery Plugins work through tricky problems Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 68. Carbon Five We get to work on diverse projects and learn lots of new technologies. We're always looking for both seasoned developers and junior programmers to work, please feel free to talk to me now or email me Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson
  • 69. Thanks! andy@carbonfive.com http://github.com/ndp Andy Peterson • andy@carbonfive.com • SF jQuery Conf • April 2010 Copyright (c) 2010 Andrew J. Peterson

Editor's Notes

  1. Boy meets girl joke
  2. Did it really do something? Preconditions? On Load timing What if the markup is dynamic As the page grows, how is this going to work?
  3. Claim only a single name in the jQuery namespace Accept an options argument to control plugin behavior Provide public access to default plugin settings Provide public access to secondary functions (as applicable) Keep private functions private Support the Metadata Plugin
  4. There are others... this one is good - PUT FILES HERE _ WRITE TST HERE OPEN IN BROWSER RUN ON COMMAND LINE
  5. Loosely coupled to the markup (but still CSS)