holygrail 0.5 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest CHANGED
@@ -6,6 +6,8 @@ Rakefile
6
6
  docs.watchr
7
7
  holygrail.gemspec
8
8
  lib/holygrail.rb
9
+ public/javascripts/application.js
10
+ public/javascripts/foo.js
9
11
  rails/init.rb
10
12
  specs.watchr
11
13
  test/holygrail_test.rb
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "holygrail"
3
- s.version = "0.5"
3
+ s.version = "0.5.1"
4
4
  s.summary = "Harmony plugin for Ruby on Rails tests"
5
5
  s.description = "The Holy Grail of testing for front-end development; execute browser-less, console-based, javascript + DOM code right from within your Rails test suite."
6
6
  s.author = "mynyml"
@@ -41,10 +41,28 @@ module ActionController
41
41
  # javascript code exception
42
42
  #
43
43
  def js(code)
44
- @__page ||= Harmony::Page.new(@response.body.to_s)
44
+ @__page ||= Harmony::Page.new(rewrite_script_paths(@response.body.to_s))
45
45
  @__page.execute_js(code)
46
46
  end
47
47
  alias :execute_javascript :js
48
+
49
+ private
50
+
51
+ # Rewrite relative src paths in <script> tags
52
+ #
53
+ # <script src> tags point to js files relative to public/ directory.
54
+ # Harmony needs paths to the local files instead so that it can load
55
+ # them.
56
+ #
57
+ # @param [String] body
58
+ # document for which to rewrite script paths
59
+ #
60
+ # @return [String]
61
+ # updated body
62
+ #
63
+ def rewrite_script_paths(body)
64
+ body.gsub(%r%src=("|')/?javascripts/(.*)("|')%) { %|src=#{$1}%s#{$1}"| % Rails.root.join("public/javascripts/#{$2}") }
65
+ end
48
66
  end
49
67
  end
50
68
  end
@@ -0,0 +1 @@
1
+ function holy() { return 'grail'; }
@@ -0,0 +1 @@
1
+ function foo() { return 'foo'; }
@@ -1,7 +1,14 @@
1
1
  require 'test/test_helper'
2
2
 
3
+ class Rails
4
+ def self.root
5
+ Pathname(__FILE__).dirname.parent.expand_path
6
+ end
7
+ end
8
+
3
9
  ActionController::Routing::Routes.draw do |map|
4
- map.root :controller => 'holy_grails', :action => 'foo'
10
+ map.connect '/foo', :controller => 'holy_grails', :action => 'foo'
11
+ map.connect '/bar', :controller => 'holy_grails', :action => 'bar'
5
12
  end
6
13
 
7
14
  class HolyGrailsController < ActionController::Base
@@ -18,6 +25,20 @@ class HolyGrailsController < ActionController::Base
18
25
  </html>
19
26
  HTML
20
27
  end
28
+
29
+ def bar
30
+ render :text => <<-HTML
31
+ <html>
32
+ <head>
33
+ <script src="/javascripts/application.js"></script>
34
+ <script src='javascripts/foo.js'></script>
35
+ </head>
36
+ <body>
37
+ <a href="/javascripts/application.js">local uri</a>
38
+ </body>
39
+ </html>
40
+ HTML
41
+ end
21
42
  end
22
43
 
23
44
  class HolyGrailsControllerTest < ActionController::TestCase
@@ -52,5 +73,15 @@ class HolyGrailsControllerTest < ActionController::TestCase
52
73
  assert_equal 'Foo', js("document.title")
53
74
  assert_equal 2, js("document.getElementsByTagName('div').length")
54
75
  end
76
+
77
+ test "resolves <script scr> URIs" do
78
+ get :bar
79
+ assert_equal 'grail', js("holy()") #src with double quotes + absolute path
80
+ assert_equal 'foo', js("foo()") #src with single quotes + relative path
81
+
82
+ # other paths should be left intact
83
+ assert_equal '/javascripts/application.js',
84
+ js("document.getElementsByTagName('a')[0].href")
85
+ end
55
86
  end
56
87
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: holygrail
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.5"
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - mynyml
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-11 00:00:00 -05:00
12
+ date: 2010-02-15 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -49,6 +49,8 @@ files:
49
49
  - docs.watchr
50
50
  - holygrail.gemspec
51
51
  - lib/holygrail.rb
52
+ - public/javascripts/application.js
53
+ - public/javascripts/foo.js
52
54
  - rails/init.rb
53
55
  - specs.watchr
54
56
  - test/holygrail_test.rb