@@ -9,7 +9,7 @@ class RequirejsRailsTest < ActiveSupport::TestCase
9
9
assert_kind_of Module , Requirejs ::Rails
10
10
assert_kind_of Class , Requirejs ::Rails ::Engine
11
11
end
12
-
12
+
13
13
test "require.js version" do
14
14
require_js = Pathname . new ( __FILE__ +'/../../vendor/assets/javascripts/require.js' ) . cleanpath . read
15
15
context = ExecJS . compile ( require_js )
@@ -51,13 +51,13 @@ def setup
51
51
52
52
test "user_config should reject baseUrl" do
53
53
exc = assert_raises Requirejs ::ConfigError do
54
- @cfg . user_config = { 'baseUrl' => '/frobnitz' }
54
+ @cfg . user_config = { 'baseUrl' => '/frobnitz' }
55
55
end
56
56
assert_match /baseUrl is not needed/ , exc . message
57
57
end
58
58
59
59
test "run_config should inherit user_config settings" do
60
- @cfg . user_config = { 'paths' => { 'jquery' => 'lib/jquery-1.7.2.min' } }
60
+ @cfg . user_config = { 'paths' => { 'jquery' => 'lib/jquery-1.7.2.min' } }
61
61
refute_nil @cfg . run_config [ 'paths' ]
62
62
assert_kind_of Hash , @cfg . run_config [ 'paths' ]
63
63
assert_equal 'lib/jquery-1.7.2.min' , @cfg . run_config [ 'paths' ] [ 'jquery' ]
@@ -69,32 +69,32 @@ def setup
69
69
end
70
70
71
71
test "build_config should inherit user_config settings" do
72
- @cfg . user_config = { 'paths' => { 'jquery' => 'lib/jquery-1.7.2.min' } }
72
+ @cfg . user_config = { 'paths' => { 'jquery' => 'lib/jquery-1.7.2.min' } }
73
73
refute_nil @cfg . build_config [ 'paths' ]
74
74
assert_kind_of Hash , @cfg . build_config [ 'paths' ]
75
75
assert_equal 'lib/jquery-1.7.2.min' , @cfg . build_config [ 'paths' ] [ 'jquery' ]
76
76
end
77
77
78
78
test "run_config should reject irrelevant settings" do
79
- @cfg . user_config = { 'optimize' => 'none' }
80
- assert_nil @cfg . run_config [ 'optimize' ]
79
+ @cfg . user_config = { 'optimize' => 'none' }
80
+ assert_nil @cfg . run_config [ 'optimize' ]
81
81
end
82
82
83
83
test "build_config should reject irrelevant settings" do
84
- @cfg . user_config = { 'priority' => %w{ foo bar baz } }
85
- assert_nil @cfg . build_config [ 'priority' ]
84
+ @cfg . user_config = { 'priority' => %w{ foo bar baz } }
85
+ assert_nil @cfg . build_config [ 'priority' ]
86
86
end
87
87
88
88
## Almond tests
89
89
test "build_config with almond should accept one module" do
90
90
@cfg . loader = :almond
91
- @cfg . user_config = { 'modules' => [ { 'name' => 'foo' } ] }
91
+ @cfg . user_config = { 'modules' => [ { 'name' => 'foo' } ] }
92
92
assert_match 'almond' , @cfg . build_config [ 'modules' ] [ 0 ] [ 'name' ]
93
93
end
94
94
95
95
test "build_config with almond must reject more than one module" do
96
96
@cfg . loader = :almond
97
- @cfg . user_config = { 'modules' => [ { 'name' => 'foo' } , { 'name' => 'bar' } ] }
97
+ @cfg . user_config = { 'modules' => [ { 'name' => 'foo' } , { 'name' => 'bar' } ] }
98
98
exc = assert_raises Requirejs ::ConfigError do
99
99
@cfg . build_config
100
100
end
@@ -103,7 +103,6 @@ def setup
103
103
end
104
104
105
105
class RequirejsHelperTest < ActionView ::TestCase
106
-
107
106
def setup
108
107
controller . requirejs_included = false
109
108
Rails . application . config . requirejs . user_config = { }
@@ -112,67 +111,88 @@ def setup
112
111
end
113
112
114
113
def with_cdn
115
- Rails . application . config . requirejs . user_config = { 'paths' =>
116
- { 'jquery' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' }
114
+ Rails . application . config . requirejs . user_config = {
115
+ "paths" => {
116
+ "jquery" => "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
117
+ }
117
118
}
118
119
end
119
-
120
+
120
121
def wrap ( tag )
121
122
"<html><head>#{ tag } </head></html>"
122
123
end
123
-
124
+
124
125
test "requirejs_include_tag" do
125
- render :text => wrap ( requirejs_include_tag )
126
- assert_select "script:first-of-type" , :text => /var require =/
127
- assert_select "script:last-of-type[src^=/javascripts/require.js]" , :count => 1
126
+ render text : wrap ( requirejs_include_tag )
127
+
128
+ assert_select "script:first-of-type[src=\" /javascripts/require.js\" ]" , count : 1
129
+ assert_select "script:last-of-type" , text : "require.config({\" baseUrl\" :\" /assets\" });"
128
130
end
129
-
131
+
130
132
test "requirejs_include_tag_with_param" do
131
- render :text => wrap ( requirejs_include_tag ( "application" ) )
132
- assert_select "script:last-of-type[src^=/javascripts/require.js][data-main^=javascripts/application]" , :count => 1
133
+ render text : wrap ( requirejs_include_tag ( "application" ) )
134
+
135
+ assert_select "script:first-of-type[src=\" /javascripts/require.js\" ]" \
136
+ "[data-main=\" javascripts/application\" ]" , count : 1
133
137
end
134
-
138
+
135
139
test "requirejs_include_tag_with_block" do
136
- test_block = Proc . new do | controller |
137
- { ' class' => controller . class . to_s . demodulize }
138
- end
140
+ render text : wrap ( requirejs_include_tag ( "application" ) do
141
+ { " class" => controller . class . name . demodulize }
142
+ end )
139
143
140
- render :text => wrap ( requirejs_include_tag ( "application" , & test_block ) )
141
- assert_select "script:last-of-type[src^=/javascripts/require.js][ data-main^= javascripts/application]" , :count => 1
142
- assert_select "script:last-of-type[src^=/javascripts/require.js][ data-class^= TestController]", : count => 1
144
+ assert_select "script:first-of-type[src= \" /javascripts/require.js \" ]" \
145
+ "[ data-main= \" javascripts/application\" ]" \
146
+ "[ data-class= \" TestController\" ]", count : 1
143
147
end
144
148
145
149
test "requirejs_include_tag can appear only once" do
146
150
assert_raises Requirejs ::MultipleIncludeError do
147
- render :text => "#{ requirejs_include_tag } \n #{ requirejs_include_tag } "
151
+ requirejs_include_tag
152
+ requirejs_include_tag
148
153
end
149
154
end
150
155
151
- test "requirejs_include_tag with digested asset paths" do
156
+ test "requirejs_include_tag with digestified asset paths" do
152
157
begin
158
+ Rails . application . config . requirejs . user_config = {
159
+ "modules" => [
160
+ { "name" => "foo" }
161
+ ]
162
+ }
163
+
153
164
saved_digest = Rails . application . config . assets . digest
154
165
Rails . application . config . assets . digest = true
155
- Rails . application . config . requirejs . user_config = { 'modules' => [ { 'name' => 'foo' } ] }
156
- render :text => wrap ( requirejs_include_tag )
157
- assert_select "script:first-of-type" , :text => %r[var require =.*"paths":{"foo":"/javascripts/foo"}]
166
+
167
+ render text : wrap ( requirejs_include_tag )
168
+
169
+ assert_select "script:last-of-type" ,
170
+ text : Regexp . new ( "\\ Arequire\\ .config\\ ({.*\" paths\" :{.*\" foo\" :\" /javascripts/foo\" .*}.*}\\ );\\ z" )
158
171
ensure
159
172
Rails . application . config . assets . digest = saved_digest
160
173
end
161
174
end
162
175
163
176
test "requirejs_include_tag with CDN asset in paths" do
164
177
with_cdn
165
- render :text => wrap ( requirejs_include_tag )
166
- assert_select "script:first-of-type" , :text => %r{var require =.*paths.*http://ajax}
178
+
179
+ render text : wrap ( requirejs_include_tag )
180
+
181
+ assert_select "script:last-of-type" ,
182
+ text : Regexp . new ( "\\ Arequire\\ .config\\ ({.*\" paths\" :{.*\" http://ajax\\ ..*\" .*}.*}\\ );\\ z" )
167
183
end
168
184
169
- test "requirejs_include_tag with CDN asset and digested asset paths" do
185
+ test "requirejs_include_tag with CDN asset and digestified asset paths" do
170
186
begin
171
187
with_cdn
188
+
172
189
saved_digest = Rails . application . config . assets . digest
173
190
Rails . application . config . assets . digest = true
174
- render :text => wrap ( requirejs_include_tag )
175
- assert_select "script:first-of-type" , :text => %r{var require =.*paths.*http://ajax}
191
+
192
+ render text : wrap ( requirejs_include_tag )
193
+
194
+ assert_select "script:last-of-type" ,
195
+ text : Regexp . new ( "\\ Arequire\\ .config\\ ({.*\" paths\" :{.*\" http://ajax\\ ..*\" .*}.*}\\ );\\ z" )
176
196
ensure
177
197
Rails . application . config . assets . digest = saved_digest
178
198
end
0 commit comments