From 665abc501c6f46c989bc2055991e5c9a5b87eaf2 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 11 Sep 2012 08:54:50 -0700 Subject: [PATCH] Use Konacha for testing JavaScript --- Gemfile | 6 ++ Gemfile.lock | 34 +++++++-- config/initializers/konacha.rb | 5 ++ test/javascripts/osm_test.js | 135 +++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 config/initializers/konacha.rb create mode 100644 test/javascripts/osm_test.js diff --git a/Gemfile b/Gemfile index 084dd239f..d98a2a1fc 100644 --- a/Gemfile +++ b/Gemfile @@ -86,3 +86,9 @@ group :test do gem 'timecop' gem 'minitest', '~> 4.7.0', :platforms => [:ruby_19, :ruby_20] end + +# Needed in development as well so rake can see konacha tasks +group :development, :test do + gem 'konacha' + gem 'poltergeist' +end diff --git a/Gemfile.lock b/Gemfile.lock index 130b57b5b..d477fcfca 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,11 +29,18 @@ GEM thread_safe (~> 0.1) tzinfo (~> 0.3.37) arel (4.0.2) - atomic (1.1.14) + atomic (1.1.15) bigdecimal (1.1.0) builder (3.1.4) + capybara (2.2.1) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (~> 2.0) climate_control (0.0.3) activesupport (>= 3.0) + cliver (0.3.2) cocaine (0.5.3) climate_control (>= 0.0.3, < 1.0) coffee-rails (4.0.1) @@ -43,6 +50,7 @@ GEM coffee-script-source execjs coffee-script-source (1.7.0) + colorize (0.6.0) composite_primary_keys (6.0.1) activerecord (>= 4.0.0) dalli (2.7.0) @@ -70,6 +78,12 @@ GEM jwt (0.1.11) multi_json (>= 1.5) kgio (2.9.2) + konacha (3.1.0) + actionpack (>= 3.1, < 5) + capybara + colorize + railties (>= 3.1, < 5) + sprockets libxml-ruby (2.7.0) mail (2.5.4) mime-types (~> 1.16) @@ -98,12 +112,17 @@ GEM rack-openid (~> 1.3) openstreetmap-i18n-js (3.0.0.rc5.3) i18n - paperclip (4.1.0) + paperclip (4.1.1) activemodel (>= 3.0.0) activesupport (>= 3.0.0) cocaine (~> 0.5.3) mime-types pg (0.17.1) + poltergeist (1.5.0) + capybara (~> 2.1) + cliver (~> 0.3.1) + multi_json (~> 1.0) + websocket-driver (>= 0.2.0) polyglot (0.3.4) psych (2.0.4) r2 (0.2.2) @@ -142,7 +161,7 @@ GEM sass (>= 3.1.10) sprockets-rails (~> 2.0.0) soap4r-ruby1.9 (2.0.5) - sprockets (2.10.1) + sprockets (2.11.0) hike (~> 1.2) multi_json (~> 1.0) rack (~> 1.0) @@ -152,8 +171,8 @@ GEM activesupport (>= 3.0) sprockets (~> 2.8) thor (0.18.1) - thread_safe (0.1.3) - atomic + thread_safe (0.2.0) + atomic (>= 1.1.7, < 2) tilt (1.4.1) timecop (0.7.1) treetop (1.4.15) @@ -165,6 +184,9 @@ GEM json (>= 1.8.0) validates_email_format_of (1.5.3) vendorer (0.1.14) + websocket-driver (0.3.2) + xpath (2.0.0) + nokogiri (~> 1.3) PLATFORMS ruby @@ -186,6 +208,7 @@ DEPENDENCIES json jsonify-rails kgio + konacha libxml-ruby (>= 2.0.5) minitest (~> 4.7.0) oauth-plugin (>= 0.5.1) @@ -193,6 +216,7 @@ DEPENDENCIES openstreetmap-i18n-js (>= 3.0.0.rc5.3) paperclip (~> 4.0) pg + poltergeist psych r2 rack-cors diff --git a/config/initializers/konacha.rb b/config/initializers/konacha.rb new file mode 100644 index 000000000..2924efab6 --- /dev/null +++ b/config/initializers/konacha.rb @@ -0,0 +1,5 @@ +Konacha.configure do |config| + require 'capybara/poltergeist' + config.spec_dir = "test/javascripts" + config.driver = :poltergeist +end if defined?(Konacha) diff --git a/test/javascripts/osm_test.js b/test/javascripts/osm_test.js new file mode 100644 index 000000000..84ef47cc4 --- /dev/null +++ b/test/javascripts/osm_test.js @@ -0,0 +1,135 @@ +//= require jquery +//= require jquery.cookie +//= require osm + +describe("OSM", function () { + describe(".apiUrl", function () { + it("returns a URL for a way", function () { + expect(OSM.apiUrl({type: "way", id: 10})).to.eq("/api/0.6/way/10/full"); + }); + + it("returns a URL for a node", function () { + expect(OSM.apiUrl({type: "node", id: 10})).to.eq("/api/0.6/node/10"); + }); + + it("returns a URL for a specific version", function () { + expect(OSM.apiUrl({type: "node", id: 10, version: 2})).to.eq("/api/0.6/node/10/2"); + }); + }); + + describe(".mapParams", function () { + beforeEach(function () { + delete OSM.home; + delete OSM.location; + document.cookie = "_osm_location=; expires=Thu, 01 Jan 1970 00:00:00 GMT"; + + // Test with another cookie set. + document.cookie = "_osm_session=deadbeef"; + }); + + it("parses marker params", function () { + var params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845"); + expect(params).to.have.property("mlat", 57.6247); + expect(params).to.have.property("mlon", -3.6845); + expect(params).to.have.property("marker", true); + }); + + it("parses object params", function () { + var params = OSM.mapParams("?node=1"); + expect(params).to.have.property("object"); + expect(params).to.have.property("object_zoom", true); + expect(params.object).to.eql({type: "node", id: 1}); + + params = OSM.mapParams("?way=1"); + expect(params).to.have.property("object"); + expect(params).to.have.property("object_zoom", true); + expect(params.object).to.eql({type: "way", id: 1}); + + params = OSM.mapParams("?relation=1"); + expect(params).to.have.property("object"); + expect(params).to.have.property("object_zoom", true); + expect(params.object).to.eql({type: "relation", id: 1}); + }); + + it("parses bbox params", function () { + var params = OSM.mapParams("?bbox=-3.6845,57.6247,-3.7845,57.7247"); + expect(params).to.have.property("bbox", true); + expect(params).to.have.property("minlon", -3.6845); + expect(params).to.have.property("minlat", 57.6247); + expect(params).to.have.property("maxlon", -3.7845); + expect(params).to.have.property("maxlat", 57.7247); + expect(params).to.have.property("box", false); + + params = OSM.mapParams("?minlon=-3.6845&minlat=57.6247&maxlon=-3.7845&maxlat=57.7247"); + expect(params).to.have.property("bbox", true); + expect(params).to.have.property("minlon", -3.6845); + expect(params).to.have.property("minlat", 57.6247); + expect(params).to.have.property("maxlon", -3.7845); + expect(params).to.have.property("maxlat", 57.7247); + expect(params).to.have.property("box", false); + + params = OSM.mapParams("?bbox=-3.6845,57.6247,-3.7845,57.7247&box=yes"); + expect(params).to.have.property("box", true); + + params = OSM.mapParams("?minlon=-3.6845&minlat=57.6247&maxlon=-3.7845&maxlat=57.7247&box=yes"); + expect(params).to.have.property("box", true); + }); + + it("infers lat/long from bbox", function () { + var params = OSM.mapParams("?bbox=-3.6845,57.6247,-3.7845,57.7247"); + expect(params).to.have.property("lat", 57.6747); + expect(params).to.have.property("lon", -3.7344999999999997); + + params = OSM.mapParams("?minlon=-3.6845&minlat=57.6247&maxlon=-3.7845&maxlat=57.7247"); + expect(params).to.have.property("lat", 57.6747); + expect(params).to.have.property("lon", -3.7344999999999997); + }); + + it("parses lat/lon params", function () { + var params = OSM.mapParams("?lat=57.6247&lon=-3.6845"); + expect(params).to.have.property("lat", 57.6247); + expect(params).to.have.property("lon", -3.6845); + + params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845"); + expect(params).to.have.property("lat", 57.6247); + expect(params).to.have.property("lon", -3.6845); + }); + + it("sets lat/lon from OSM.home", function () { + OSM.home = {lat: 57.6247, lon: -3.6845}; + var params = OSM.mapParams("?"); + expect(params).to.have.property("lat", 57.6247); + expect(params).to.have.property("lon", -3.6845); + }); + + it("sets bbox from OSM.location", function () { + OSM.location = {minlon: -3.6845, minlat: 57.6247, maxlon: -3.7845, maxlat: 57.7247}; + var params = OSM.mapParams("?"); + expect(params).to.have.property("bbox", true); + expect(params).to.have.property("minlon", -3.6845); + expect(params).to.have.property("minlat", 57.6247); + expect(params).to.have.property("maxlon", -3.7845); + expect(params).to.have.property("maxlat", 57.7247); + }); + + it("parses params from the _osm_location cookie", function () { + document.cookie = "_osm_location=-3.6845|57.6247|5|M"; + var params = OSM.mapParams("?"); + expect(params).to.have.property("lat", 57.6247); + expect(params).to.have.property("lon", -3.6845); + expect(params).to.have.property("zoom", 5); + expect(params).to.have.property("layers", "M"); + }); + + it("defaults lat/lon to London", function () { + var params = OSM.mapParams("?"); + expect(params).to.have.property("lat", 51.5); + expect(params).to.have.property("lon", -0.1); + }); + + it("parses layers param", function () { + var params = OSM.mapParams("?layers=M"); + expect(params).to.have.property("layers", "M"); + }); + }); +}); -- 2.39.5