SlideShare ist ein Scribd-Unternehmen logo
1 von 24
by Jon Kruger
$("#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");
  });
});
   ?????????????????
   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>
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

Weitere ähnliche Inhalte

Was ist angesagt?

Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
baygross
 
Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for Joomla
Luke Summerfield
 
Joe Walker Interactivewebsites Cometand Dwr
Joe Walker Interactivewebsites Cometand DwrJoe Walker Interactivewebsites Cometand Dwr
Joe Walker Interactivewebsites Cometand Dwr
deimos
 
Remy Sharp The DOM scripting toolkit jQuery
Remy Sharp The DOM scripting toolkit jQueryRemy 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 2KZepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Thomas Fuchs
 

Was ist angesagt? (20)

Why ruby
Why rubyWhy ruby
Why ruby
 
Web Crawling with NodeJS
Web Crawling with NodeJSWeb Crawling with NodeJS
Web Crawling with NodeJS
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards
 
jQuery
jQueryjQuery
jQuery
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
 
jQuery in 15 minutes
jQuery in 15 minutesjQuery in 15 minutes
jQuery in 15 minutes
 
06 jQuery #burningkeyboards
06 jQuery  #burningkeyboards06 jQuery  #burningkeyboards
06 jQuery #burningkeyboards
 
jQuery
jQueryjQuery
jQuery
 
Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for Joomla
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Joe Walker Interactivewebsites Cometand Dwr
Joe Walker Interactivewebsites Cometand DwrJoe Walker Interactivewebsites Cometand Dwr
Joe Walker Interactivewebsites Cometand Dwr
 
jQuery Best Practice
jQuery Best Practice jQuery Best Practice
jQuery Best Practice
 
Remy Sharp The DOM scripting toolkit jQuery
Remy Sharp The DOM scripting toolkit jQueryRemy Sharp The DOM scripting toolkit jQuery
Remy Sharp The DOM scripting toolkit jQuery
 
J query training
J query trainingJ query training
J query training
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2KZepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
 
JavaScript Promise
JavaScript PromiseJavaScript Promise
JavaScript Promise
 
Jquery
JqueryJquery
Jquery
 
jQuery
jQueryjQuery
jQuery
 

Ähnlich wie Testable, Object-Oriented JavaScript

international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a TreejQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
adamlogic
 
J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
rsnarayanan
 

Ähnlich wie Testable, Object-Oriented JavaScript (20)

jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
Introducing jQuery
Introducing jQueryIntroducing jQuery
Introducing jQuery
 
Automated testing for client-side - Adam Klein, 500 Tech
Automated testing for client-side - Adam Klein, 500 TechAutomated testing for client-side - Adam Klein, 500 Tech
Automated testing for client-side - Adam Klein, 500 Tech
 
Client side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karmaClient side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karma
 
Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
amsterdamjs - jQuery 1.5
amsterdamjs - jQuery 1.5amsterdamjs - jQuery 1.5
amsterdamjs - jQuery 1.5
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a TreejQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
 
jQuery
jQueryjQuery
jQuery
 
Kakunin E2E framework showcase
Kakunin E2E framework showcaseKakunin E2E framework showcase
Kakunin E2E framework showcase
 
Magento2&amp;java script (2)
Magento2&amp;java script (2)Magento2&amp;java script (2)
Magento2&amp;java script (2)
 
Javascript - Beyond-jQuery
Javascript - Beyond-jQueryJavascript - Beyond-jQuery
Javascript - Beyond-jQuery
 
Web Components Everywhere
Web Components EverywhereWeb Components Everywhere
Web Components Everywhere
 
Jquery plugin development
Jquery plugin developmentJquery plugin development
Jquery plugin development
 
J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
 
Clean Javascript
Clean JavascriptClean Javascript
Clean Javascript
 
Unit3.pptx
Unit3.pptxUnit3.pptx
Unit3.pptx
 

Mehr von 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 BusinessThe 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
 
Developing an Automated Testing Strategy
Developing an Automated Testing StrategyDeveloping an Automated Testing Strategy
Developing an Automated Testing Strategy
 
A Whole Team Approach To Testing
A Whole Team Approach To TestingA Whole Team Approach To Testing
A Whole Team Approach To Testing
 
An ATDD Case Study
An ATDD Case StudyAn ATDD Case Study
An ATDD Case Study
 
Productivity Boosters for .NET Developers
Productivity Boosters for .NET DevelopersProductivity Boosters for .NET Developers
Productivity Boosters for .NET Developers
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Advanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID PrinciplesAdvanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID Principles
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Kürzlich hochgeladen (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Testable, Object-Oriented JavaScript

  • 2.
  • 3.
  • 4.
  • 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. ?????????????????
  • 10.
  • 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>
  • 23.
  • 24. 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

Hinweis der Redaktion

  1. 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?
  2. - Browser inconsistencies (e.g. document.getElementById(), document.all)- We used to not have jQuery
  3. 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?
  4. -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)
  5. 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)
  6. We need to do something about this:
  7. -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)
  8. 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.
  9. -Reads like RSpec-Ruby gem-Community involvement
  10. We have an interface between JavaScript and the server (AJAX), but what is the interface between the JavaScript and the DOM?
  11. Typical JavaScript tends to have the JavaScript very tightly coupled to the DOM.
  12. How do we separate JavaScript and the DOM?
  13. 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.
  14. The abstraction layer between your JavaScript and jQuery/the DOMConvention over configurationMakes testing first easier
  15. Link DOM elements to the registered properties