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.
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");
});
});
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
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?
- Browser inconsistencies (e.g. document.getElementById(), document.all)- We used to not have jQuery
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?
-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)
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)
We need to do something about this:
-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)
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.
-Reads like RSpec-Ruby gem-Community involvement
We have an interface between JavaScript and the server (AJAX), but what is the interface between the JavaScript and the DOM?
Typical JavaScript tends to have the JavaScript very tightly coupled to the DOM.
How do we separate JavaScript and the DOM?
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.
The abstraction layer between your JavaScript and jQuery/the DOMConvention over configurationMakes testing first easier