SlideShare a Scribd company logo
by Jon Kruger
Testable, Object-Oriented JavaScript
Testable, Object-Oriented JavaScript
Testable, Object-Oriented JavaScript
$("#Name").text("jon");
   Object oriented programming
   Readable code
   Small classes/files
   Loosely coupled
   Unit testing




                       F
   Cucumber, SpecFlow, Selenium, Watir, etc.
   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");
  });
});
   ?????????????????
Testable, Object-Oriented JavaScript
   Tests a small unit of functionality
   Must run fast
   Isolate external dependencies
   How do we deal with the DOM
   How do we deal with AJAX calls
   How do we run the tests
DOM
JavaScript
      AJAX

 Server
DOM
JavaScript
      AJAX

 Server
DOM
      ???????

JavaScript
      AJAX

 Server
DOM        Fake DOM
       ???????

  JavaScript
            AJAX

      Server
http://github.com/jonkruger/jsview
FTW!!!!
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                          whenUsernameChanges
setUsername                          whenUsernameClicked
showUsername                         whenUsernameIsClicked
hideUsername                         whenUsernameGainsFocus
clickUsername                        whenUsernameLosesFocus
pressKeyInUsername                   whenKeyIsPressedInUsername
keyDownInUsername                    whenKeyDownInUsername
enableUsername
disableUsername
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                       whenSendButtonClicked
hideSendButton                       whenSendButtonIsClicked
clickSendButton
enableSendButton
disableSendButton
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
<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>
Testable, Object-Oriented JavaScript
Slides: http://jonkruger.com/blog
JSView: http://github.com/jonkruger/jsview
Jasmine: http://pivotal.github.com/jasmine

        mail: jon@jonkruger.com
          Twitter: @JonKruger
     Blog: http://jonkruger.com/blog
Ad

Recommended

Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Jon Kruger
 
Solid Software Design Principles
Solid Software Design Principles
Jon Kruger
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
History of jQuery
History of jQuery
jeresig
 
jQuery from the very beginning
jQuery from the very beginning
Anis Ahmad
 
Node.js in action
Node.js in action
Simon Su
 
SOLID PRINCIPLES
SOLID PRINCIPLES
Luciano Queiroz
 
Backbone js
Backbone js
rstankov
 
Why ruby
Why ruby
rstankov
 
Web Crawling with NodeJS
Web Crawling with NodeJS
Sylvain Zimmer
 
Ruby/Rails
Ruby/Rails
rstankov
 
05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards
Denis Ristic
 
jQuery
jQuery
Dileep Mishra
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
baygross
 
jQuery in 15 minutes
jQuery in 15 minutes
Simon Willison
 
06 jQuery #burningkeyboards
06 jQuery #burningkeyboards
Denis Ristic
 
jQuery
jQuery
Mohammed Arif
 
Javascript Frameworks for Joomla
Javascript Frameworks for Joomla
Luke Summerfield
 
JavaScript Promises
JavaScript Promises
L&T Technology Services Limited
 
Joe Walker Interactivewebsites Cometand Dwr
Joe Walker Interactivewebsites Cometand Dwr
deimos
 
jQuery Best Practice
jQuery Best Practice
chandrashekher786
 
Remy Sharp The DOM scripting toolkit jQuery
Remy Sharp The DOM scripting toolkit jQuery
deimos
 
J query training
J query training
FIS - Fidelity Information Services
 
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Thomas Fuchs
 
JavaScript Promise
JavaScript Promise
Joseph Chiang
 
Jquery
Jquery
Girish Srivastava
 
jQuery
jQuery
Jay Poojara
 
Basic Tutorial of React for Programmers
Basic Tutorial of React for Programmers
David Rodenas
 
jQuery secrets
jQuery secrets
Bastian Feder
 
Introducing jQuery
Introducing jQuery
Wildan Maulana
 

More Related Content

What's hot (20)

Why ruby
Why ruby
rstankov
 
Web Crawling with NodeJS
Web Crawling with NodeJS
Sylvain Zimmer
 
Ruby/Rails
Ruby/Rails
rstankov
 
05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards
Denis Ristic
 
jQuery
jQuery
Dileep Mishra
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
baygross
 
jQuery in 15 minutes
jQuery in 15 minutes
Simon Willison
 
06 jQuery #burningkeyboards
06 jQuery #burningkeyboards
Denis Ristic
 
jQuery
jQuery
Mohammed Arif
 
Javascript Frameworks for Joomla
Javascript Frameworks for Joomla
Luke Summerfield
 
JavaScript Promises
JavaScript Promises
L&T Technology Services Limited
 
Joe Walker Interactivewebsites Cometand Dwr
Joe Walker Interactivewebsites Cometand Dwr
deimos
 
jQuery Best Practice
jQuery Best Practice
chandrashekher786
 
Remy Sharp The DOM scripting toolkit jQuery
Remy Sharp The DOM scripting toolkit jQuery
deimos
 
J query training
J query training
FIS - Fidelity Information Services
 
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Thomas Fuchs
 
JavaScript Promise
JavaScript Promise
Joseph Chiang
 
Jquery
Jquery
Girish Srivastava
 
jQuery
jQuery
Jay Poojara
 
Basic Tutorial of React for Programmers
Basic Tutorial of React for Programmers
David Rodenas
 
Web Crawling with NodeJS
Web Crawling with NodeJS
Sylvain Zimmer
 
Ruby/Rails
Ruby/Rails
rstankov
 
05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards
Denis Ristic
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
baygross
 
06 jQuery #burningkeyboards
06 jQuery #burningkeyboards
Denis Ristic
 
Javascript Frameworks for Joomla
Javascript Frameworks for Joomla
Luke Summerfield
 
Joe Walker Interactivewebsites Cometand Dwr
Joe Walker Interactivewebsites Cometand Dwr
deimos
 
Remy Sharp The DOM scripting toolkit jQuery
Remy Sharp The DOM scripting toolkit jQuery
deimos
 
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Thomas Fuchs
 
Basic Tutorial of React for Programmers
Basic Tutorial of React for Programmers
David Rodenas
 

Similar to Testable, Object-Oriented JavaScript (20)

jQuery secrets
jQuery secrets
Bastian Feder
 
Introducing jQuery
Introducing jQuery
Wildan Maulana
 
Automated testing for client-side - Adam Klein, 500 Tech
Automated testing for client-side - Adam Klein, 500 Tech
Codemotion Tel Aviv
 
Client side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karma
Adam Klein
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
Knoldus Inc.
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
jQuery secrets
jQuery secrets
Bastian Feder
 
Introduction to jQuery
Introduction to jQuery
Nagaraju Sangam
 
amsterdamjs - jQuery 1.5
amsterdamjs - jQuery 1.5
mennovanslooten
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
EPAM Systems
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
adamlogic
 
jQuery
jQuery
Ivano Malavolta
 
Kakunin E2E framework showcase
Kakunin E2E framework showcase
The Software House
 
Magento2&amp;java script (2)
Magento2&amp;java script (2)
EvgeniyKapelko1
 
Javascript - Beyond-jQuery
Javascript - Beyond-jQuery
Tanner Moushey ❖ Mission Lab - WordPress Agency
 
Web Components Everywhere
Web Components Everywhere
Ilia Idakiev
 
Jquery plugin development
Jquery plugin development
Faruk Hossen
 
J Query The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
rsnarayanan
 
Clean Javascript
Clean Javascript
Ryunosuke SATO
 
Unit3.pptx
Unit3.pptx
AnamikaRai59
 
Automated testing for client-side - Adam Klein, 500 Tech
Automated testing for client-side - Adam Klein, 500 Tech
Codemotion Tel Aviv
 
Client side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karma
Adam Klein
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
Knoldus Inc.
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
amsterdamjs - jQuery 1.5
amsterdamjs - jQuery 1.5
mennovanslooten
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
EPAM Systems
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
adamlogic
 
Kakunin E2E framework showcase
Kakunin E2E framework showcase
The Software House
 
Magento2&amp;java script (2)
Magento2&amp;java script (2)
EvgeniyKapelko1
 
Web Components Everywhere
Web Components Everywhere
Ilia Idakiev
 
Jquery plugin development
Jquery plugin development
Faruk Hossen
 
J Query The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
rsnarayanan
 
Ad

More from Jon Kruger (7)

The Business of You: 10 Steps To Run Your Career Like a Business
The Business of You: 10 Steps To Run Your Career Like a Business
Jon Kruger
 
Developing an Automated Testing Strategy
Developing an Automated Testing Strategy
Jon Kruger
 
A Whole Team Approach To Testing
A Whole Team Approach To Testing
Jon Kruger
 
An ATDD Case Study
An ATDD Case Study
Jon Kruger
 
Productivity Boosters for .NET Developers
Productivity Boosters for .NET Developers
Jon Kruger
 
Test-Driven Development In Action
Test-Driven Development In Action
Jon Kruger
 
Advanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID Principles
Jon Kruger
 
The Business of You: 10 Steps To Run Your Career Like a Business
The Business of You: 10 Steps To Run Your Career Like a Business
Jon Kruger
 
Developing an Automated Testing Strategy
Developing an Automated Testing Strategy
Jon Kruger
 
A Whole Team Approach To Testing
A Whole Team Approach To Testing
Jon Kruger
 
An ATDD Case Study
An ATDD Case Study
Jon Kruger
 
Productivity Boosters for .NET Developers
Productivity Boosters for .NET Developers
Jon Kruger
 
Test-Driven Development In Action
Test-Driven Development In Action
Jon Kruger
 
Advanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID Principles
Jon Kruger
 
Ad

Recently uploaded (20)

Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
The Future of Data, AI, and AR: Innovation Inspired by You.pdf
The Future of Data, AI, and AR: Innovation Inspired by You.pdf
Safe Software
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
The Future of Data, AI, and AR: Innovation Inspired by You.pdf
The Future of Data, AI, and AR: Innovation Inspired by You.pdf
Safe Software
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 

Testable, Object-Oriented JavaScript

  • 6. Object oriented programming  Readable code  Small classes/files  Loosely coupled  Unit testing F
  • 7. Cucumber, SpecFlow, Selenium, Watir, etc.
  • 8. 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"); }); });
  • 9. ?????????????????
  • 11. Tests a small unit of functionality  Must run fast  Isolate external dependencies
  • 12. How do we deal with the DOM  How do we deal with AJAX calls  How do we run the tests
  • 13. DOM JavaScript AJAX Server
  • 14. DOM JavaScript AJAX Server
  • 15. DOM ??????? JavaScript AJAX Server
  • 16. DOM Fake DOM ??????? JavaScript AJAX Server
  • 19. 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 whenUsernameChanges setUsername whenUsernameClicked showUsername whenUsernameIsClicked hideUsername whenUsernameGainsFocus clickUsername whenUsernameLosesFocus pressKeyInUsername whenKeyIsPressedInUsername keyDownInUsername whenKeyDownInUsername enableUsername disableUsername
  • 20. 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 whenSendButtonClicked hideSendButton whenSendButtonIsClicked clickSendButton enableSendButton disableSendButton
  • 21. 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
  • 22. <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>
  • 24. Slides: http://jonkruger.com/blog JSView: http://github.com/jonkruger/jsview Jasmine: http://pivotal.github.com/jasmine mail: [email protected] Twitter: @JonKruger Blog: http://jonkruger.com/blog

Editor's Notes

  • #2: How many of you like writing JavaScript as much as you do your primary language?Why don’t you like it as much?How many of you write tests for your JavaScript? - What kinds of things are you testing? (logic code, injecting HTML, DOM interactions, ajax, etc.)How many of you have written JavaScript with OO objects?-Why/why not?
  • #3: - Browser inconsistencies (e.g. document.getElementById(), document.all)- We used to not have jQuery
  • #4: Many more single-page client-side rich applications written in JavaScriptYou use these and think they are awesome (Gmail, Google Maps, Outlook Web Access newer versions)HTML5 will allow you to do things that you couldn’t do before in the browser (store data locally, richer graphics, etc.)Mobile websites – you don’t need to build apps for 4 mobile platformsIf you had to do one of these, how would you do it?
  • #5: -What problems do we have when we write JavaScript like this?-Hard to read-Breaks when you change DOM attributes (id, class, etc.)Maybe you move the mess into a separate file, in which case it’s still a mess (you just don’t have to look at it as much)You can do this for simple JavaScript and get away with it, of course (as long as you don’t want to test it with a JavaScript test)
  • #6: What problems would you have if you wanted to write tests for this code?-You would have to have the DOM for the selector to work!-You have to know specifics about what your DOM is going to look like (to some extent)-We might not be able to use an ID selector because we might need to have something else use the id property (e.g. 3rd party controls, old WebForms)
  • #7: We need to do something about this:
  • #8: -These tools are good for end-to-end testing-They won’t help you design your JavaScript-Sometimes it’s easier to test scenarios by testing JavaScript (same reason you write unit tests in server-side code vs. using the browser tools)
  • #10: We need a better way. I want to be able to write my tests first, before I even know what my HTML page looks like.
  • #11: -Reads like RSpec-Ruby gem-Community involvement
  • #14: We have an interface between JavaScript and the server (AJAX), but what is the interface between the JavaScript and the DOM?
  • #15: Typical JavaScript tends to have the JavaScript very tightly coupled to the DOM.
  • #16: How do we separate JavaScript and the DOM?
  • #17: How can we write JavaScript code that will run against the real DOM in production and against a fake DOM in our tests?What things do we typically do with the DOM in JavaScript?Find elementsGet/set textEnable/disable elementsShow/hide elementsEtc.
  • #18: The abstraction layer between your JavaScript and jQuery/the DOMConvention over configurationMakes testing first easier
  • #23: Link DOM elements to the registered properties