24
Testable, Object Oriented JavaScript by Jon Kruger

Testable, Object-Oriented JavaScript

Embed Size (px)

DESCRIPTION

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.

Citation preview

Page 1: Testable, Object-Oriented JavaScript

Testable, Object Oriented

JavaScriptby Jon Kruger

Page 2: Testable, Object-Oriented JavaScript

History

Page 3: Testable, Object-Oriented JavaScript

The Future

Page 4: Testable, Object-Oriented JavaScript

Your typical JavaScript

Page 5: Testable, Object-Oriented JavaScript

What is the problem with this?

$("#Name").text("jon");

Page 6: Testable, Object-Oriented JavaScript

Widely accepted “best practices”

Object oriented programmingReadable codeSmall classes/filesLoosely coupledUnit testing

F

Page 7: Testable, Object-Oriented JavaScript

Ways we can test JavaScript

Cucumber, SpecFlow, Selenium, Watir, etc.

Page 8: Testable, Object-Oriented JavaScript

Ways we can test JavaScript

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"); });});

Page 9: Testable, Object-Oriented JavaScript

Ways we can test JavaScript

?????????????????

Page 10: Testable, Object-Oriented JavaScript

Jasmine

Page 11: Testable, Object-Oriented JavaScript

What is a unit test?Tests a small unit of functionalityMust run fastIsolate external dependencies

Page 12: Testable, Object-Oriented JavaScript

Problems to solveHow do we deal with the DOMHow do we deal with AJAX callsHow do we run the tests

Page 13: Testable, Object-Oriented JavaScript

Basic JavaScript Architecture

DOM

JavaScript

Server

AJAX

Page 14: Testable, Object-Oriented JavaScript

Basic JavaScript Architecture

JavaScript

Server

AJAX

DOM

Page 15: Testable, Object-Oriented JavaScript

Basic JavaScript Architecture

DOM

JavaScript

Server

AJAX

???????

Page 16: Testable, Object-Oriented JavaScript

Basic JavaScript Architecture

DOM

JavaScript

Server

AJAX

???????

Fake DOM

Page 17: Testable, Object-Oriented JavaScript

JSView

http://github.com/jonkruger/jsview

Page 18: Testable, Object-Oriented JavaScript

Live TDD Action!!!!

FTW!!!!

Page 19: Testable, Object-Oriented JavaScript

function Client(element, view){ if (view == null) view = new jQueryView("Client", element);

registerObjectProperties(this, view, ['Username']); }

The Client class now has the following methods:

getUsernamesetUsernameshowUsernamehideUsernameclickUsernamepressKeyInUsernamekeyDownInUsernameenableUsernamedisableUsername

whenUsernameChangeswhenUsernameClickedwhenUsernameIsClickedwhenUsernameGainsFocuswhenUsernameLosesFocuswhenKeyIsPressedInUsernamewhenKeyDownInUsername

Page 20: Testable, Object-Oriented JavaScript

function Client(element, view){ if (view == null) view = new jQueryView("Client", element);

registerObjectProperties(this, view, ['SendButton']); }

The Client class now has the following methods:

showSendButtonhideSendButtonclickSendButtonenableSendButtondisableSendButton

whenSendButtonClickedwhenSendButtonIsClicked

Page 21: Testable, Object-Oriented JavaScript

function Client(element, view){ if (view == null) view = new jQueryView("Client", element);

registerList(this, view, ['Tweets']); }

The Client class now has the following methods:

appendToTweetsprependToTweetsgetTweets

Page 22: Testable, Object-Oriented JavaScript

<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>

Page 23: Testable, Object-Oriented JavaScript

?

Page 24: Testable, Object-Oriented JavaScript

Slides, links, code, contact info

Slides: http://jonkruger.com/blogJSView:

http://github.com/jonkruger/jsviewJasmine:

http://pivotal.github.com/jasmine

mail: [email protected]: @JonKruger

Blog: http://jonkruger.com/blog