javascript ui for rest services

23
JavaScript UI for REST services Backbone, Require.js, Jasmine, HtmlUnit JUG #5 București @Cegeka

Upload: ioan-stan

Post on 15-Jan-2015

2.091 views

Category:

Technology


3 download

DESCRIPTION

Presented at Bucharest Java User Group, http://www.bjug.ro/editii/5.html . Project source code available at: https://github.com/bucharest-jug/dropwizard-todo

TRANSCRIPT

Page 1: Javascript ui for rest services

JavaScript UI for REST services

Backbone, Require.js, Jasmine, HtmlUnitJUG #5 București @Cegeka

Page 2: Javascript ui for rest services

Ioan Eugen Stan

Partner @AxemblrApache James PMC memberDebianist

Connect with me on Linkedin

Page 3: Javascript ui for rest services

DisclaimerOne solution out of many. Focus on some

best practices.

Page 4: Javascript ui for rest services

REST services are nice but we need an UI that is:● easy to develop against the API● easy to test (cross-browser)● brakes when API evolves - but informs us

what changed● can have continuous integration with our

service

Why?

Page 5: Javascript ui for rest services

● Backbone - MVC for the Browser● Require.js - modules for JS - put some order

in that chaos !!● Jasmine - JS BDD and unit testing● HtmlUnit - "GUI-Less browser for Java

programs"● jasmine-maven-plugin - combines Jasmine,

HtmlUnit and Requirejs into a Maven plugin

Tools

Page 6: Javascript ui for rest services

Backbone.js

● gives structure to web applications● provides: models, collections and views● binds view to collections/models by custom

events● declarative event handling

● views can be rendered detached from DOM!!

Page 7: Javascript ui for rest services

Sample Backbone Model

Todo.Item = Backbone.Model.extend({ defaults:function () { return { title:"", created:-1 } } })

Page 8: Javascript ui for rest services

Sample Backbone View

var TodoView = Backbone.View.extend({ events:{ "click .delete":"deleteTodo" }, render:function () { this.$el.html(Table) return this;

}, deleteTodo:function (eventName) {

this.model.delete() } })

Page 9: Javascript ui for rest services

Backbone conclusionsAllows us stay away from DOM

manipulations so we can test things in isolation

Page 10: Javascript ui for rest services

Is a file and module loader for JavaScript and:

● puts structure in to the web application● fights against namespace pollution => less

weird bugs !!● has some nice optimization features

Require.js

Page 11: Javascript ui for rest services

Require.js App File Structure

project-directory/● index.html● js/

○ libs/■ require.js■ backbone.js■ jquery.js

○ model/ ...○ view/ ...○ tpl/ - templates○ config.js - require.js config file

Page 12: Javascript ui for rest services

Index page with Require.js

<html> <head> <!-- title, CSS definitions, etc--> </head><body>

<div id="container" class="container"></div>

<script data-main="/assets/js/config.js" src="/assets/js/libs/require.js"></script>

</body></html

Page 13: Javascript ui for rest services

JasmineBehavior-driven development framework for

testing JavaScript code.

Page 14: Javascript ui for rest services

● does not depend on DOM● has very nice semantics● can use Spyes to inspect/mock calls● comes with jQuery support● you can even test for events like: click, blur,

etc.

Jasmine features

Page 15: Javascript ui for rest services

describe("mode/todo Todo Collection", function () { var collection

beforeEach(function () {// initialize collection before each test case

})

it("loads data from the proper url", function () { expect(collection.length).toBe(1) }) })

Sample Jasmine Spec

Page 16: Javascript ui for rest services

HtmlUnit"GUI-Less browser for Java programs"

Page 17: Javascript ui for rest services

● support for HTTP/HTTPS● cookies● forms, clicking links, ● most important HTTP: POST, GET, etc.● JavaScript

Use it when you need to test code that runs in the browser!

HtmlUnit provides:

Page 18: Javascript ui for rest services

Jasmine Maven PluginTest drive your JavaScript

Page 19: Javascript ui for rest services

● built on HtmlUnit with Jasmine, + Require.js● it helps you practice TDD/BDD as you write

JavaScript● it can run as part of your build with no added

configuration on your part● also supports CoffeScript

Jasmine Maven Plugin - esentials

Page 20: Javascript ui for rest services

<configuration> <browserVersion>FIREFOX_3</browserVersion> <junitXmlReportFileName>TEST-FIREFOX_3-jasmine.xml</junitXmlReportFileName> <jsSrcDir>${project.basedir}/src/main/resources/assets</jsSrcDir> <jsTestSrcDir>${project.basedir}/src/test/javascript/specs</jsTestSrcDir>

<specRunnerTemplate>REQUIRE_JS</specRunnerTemplate></configuration>

Jasmine Maven Plugin - config

Page 21: Javascript ui for rest services

Jasmine Maven Plugin - benefits

● you can integrate with CI infrastructure (Jenkins)

● has multiple browser support => test your code against I.E. 6,7,9, Firefox, Chrome, etc.

● we can test JS models with the fixtures used in Java !!

Page 22: Javascript ui for rest services

Resources

● Demo : https://github.com/bucharest-jug/dropwizard-todo (todo-service)

● Other: ○ http://backbonejs.org/○ http://underscorejs.org/○ http://requirejs.org/○ http://pivotal.github.com/jasmine/○ http://htmlunit.sourceforge.net/○ http://searls.github.com/jasmine-maven-plugin/○ https://github.com/ieugen/brooklyn/tree/app-explore