Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 6b4c964

Browse files
committed
Add Requirejs::Config tests
Improve tests around build_config, run_config, and user_config. user_config now rejects baseUrl in the configuration with Requirejs::ConfigError.
1 parent 230a43c commit 6b4c964

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

lib/requirejs/rails/config.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def initialize
2626
self.driver_template_path = Pathname.new(__FILE__+'/../rjs_driver.js.erb').cleanpath
2727
self.driver_path = self.tmp_dir + 'rjs_driver.js'
2828

29+
self.user_config = {}
30+
2931
self.run_config_whitelist = %w{
3032
baseUrl
3133
callback
@@ -115,6 +117,13 @@ def run_config
115117
run_config.merge!(self.user_config).slice(*self.run_config_whitelist)
116118
end
117119

120+
def user_config=(cfg)
121+
if url = cfg.delete('baseUrl')
122+
raise Requirejs::ConfigError, "baseUrl is not needed or permitted in the configuration"
123+
end
124+
self[:user_config] = cfg
125+
end
126+
118127
def module_name_for(mod)
119128
case self.loader
120129
when :almond

test/requirejs-rails_test.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,57 @@ def setup
4444
@cfg.logical_asset_filter += [/\.frobnitz$/]
4545
assert_equal true, @cfg.asset_allowed?('bar.frobnitz')
4646
end
47+
48+
test "should have a default empty user_config" do
49+
assert_kind_of Hash, @cfg.user_config
50+
end
51+
52+
test "user_config should reject baseUrl" do
53+
exc = assert_raises Requirejs::ConfigError do
54+
@cfg.user_config = { "baseUrl" => "/frobnitz" }
55+
end
56+
assert_match /baseUrl is not needed/, exc.message
57+
end
58+
59+
test "run_config should inherit user_config settings" do
60+
@cfg.user_config = { 'paths' => { 'jquery' => 'lib/jquery-1.7.2.min' } }
61+
refute_nil @cfg.run_config['paths']
62+
assert_kind_of Hash, @cfg.run_config['paths']
63+
assert_equal 'lib/jquery-1.7.2.min', @cfg.run_config['paths']['jquery']
64+
end
65+
66+
test "build_config should inherit user_config settings" do
67+
@cfg.user_config = { 'paths' => { 'jquery' => 'lib/jquery-1.7.2.min' } }
68+
refute_nil @cfg.build_config['paths']
69+
assert_kind_of Hash, @cfg.build_config['paths']
70+
assert_equal 'lib/jquery-1.7.2.min', @cfg.build_config['paths']['jquery']
71+
end
72+
73+
test "run_config should reject irrelevant settings" do
74+
@cfg.user_config = { 'optimize' => 'none' }
75+
assert_nil @cfg.run_config['optimize']
76+
end
77+
78+
test "build_config should reject irrelevant settings" do
79+
@cfg.user_config = { 'priority' => %w{ foo bar baz } }
80+
assert_nil @cfg.build_config['priority']
81+
end
82+
83+
## Almond tests
84+
test "build_config with almond should accept one module" do
85+
@cfg.loader = :almond
86+
@cfg.user_config = { 'modules' => [ { 'name' => 'foo' } ] }
87+
assert_match 'almond', @cfg.build_config['modules'][0]['name']
88+
end
89+
90+
test "build_config with almond must reject more than one module" do
91+
@cfg.loader = :almond
92+
@cfg.user_config = { 'modules' => [ { 'name' => 'foo' }, { 'name' => 'bar' } ] }
93+
exc = assert_raises Requirejs::ConfigError do
94+
@cfg.build_config
95+
end
96+
assert_match /requires exactly one module/, exc.message
97+
end
4798
end
4899

49100
class RequirejsHelperTest < ActionView::TestCase

0 commit comments

Comments
 (0)