Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testable, Object-Oriented JavaScript

Jon Kruger
April 22, 2012
160

Testable, Object-Oriented JavaScript

An idea of how to make JavaScript testable, presented at Stir Trek 2011. The world of JavaScript frameworks has changed greatly since then, but I still agree with the concepts.

Jon Kruger

April 22, 2012
Tweet

Transcript

  1.  Inject HTML in our tests describe("Testing by injecting HTML

    into jQuery", function() { it("should work, but it's kind of a pain", function() { element = $('<div>text</div>'); element.text("some more text"); expect(element.text()).toEqual("some more text"); }); });
  2.  Tests a small unit of functionality  Must run

    fast  Isolate external dependencies
  3.  How do we deal with the DOM  How

    do we deal with AJAX calls  How do we run the tests
  4. function Client(element, view) { if (view == null) view =

    new jQueryView("Client", element); registerObjectProperties(this, view, ['Username']); } The Client class now has the following methods: getUsername setUsername showUsername hideUsername clickUsername pressKeyInUsername keyDownInUsername enableUsername disableUsername whenUsernameChanges whenUsernameClicked whenUsernameIsClicked whenUsernameGainsFocus whenUsernameLosesFocus whenKeyIsPressedInUsername whenKeyDownInUsername
  5. function Client(element, view) { if (view == null) view =

    new jQueryView("Client", element); registerObjectProperties(this, view, ['SendButton']); } The Client class now has the following methods: showSendButton hideSendButton clickSendButton enableSendButton disableSendButton whenSendButtonClicked whenSendButtonIsClicked
  6. function Client(element, view) { if (view == null) view =

    new jQueryView("Client", element); registerList(this, view, ['Tweets']); } The Client class now has the following methods: appendToTweets prependToTweets getTweets
  7. <html> <head> <script language="javascript"> $(document).ready(function() { entryForm = new EntryForm($("#EntryForm"));

    }); </script> </head> <body> <div id="EntryForm"> First Name:<br/> <input type="text" class="EntryForm-FirstName" /><br/> <br/> Last Name:<br/> <input type="text" class="EntryForm-LastName" /> </div> </body> </html>