js-routes 2.2.1 → 2.2.4

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.
@@ -2,16 +2,19 @@ require "spec_helper"
2
2
 
3
3
  describe JsRoutes, "compatibility with Rails" do
4
4
 
5
+ let(:generated_js) do
6
+ JsRoutes.generate({module_type: nil, namespace: 'Routes'})
7
+ end
5
8
  before(:each) do
6
- evaljs(JsRoutes.generate({module_type: nil, namespace: 'Routes'}))
9
+ evaljs(generated_js)
7
10
  end
8
11
 
9
12
  it "should generate collection routing" do
10
- expect(evaljs("Routes.inboxes_path()")).to eq(test_routes.inboxes_path())
13
+ expectjs("Routes.inboxes_path()").to eq(test_routes.inboxes_path())
11
14
  end
12
15
 
13
16
  it "should generate member routing" do
14
- expect(evaljs("Routes.inbox_path(1)")).to eq(test_routes.inbox_path(1))
17
+ expectjs("Routes.inbox_path(1)").to eq(test_routes.inbox_path(1))
15
18
  end
16
19
 
17
20
  it "should raise error if required argument is not passed", :aggregate_failures do
@@ -24,8 +27,8 @@ describe JsRoutes, "compatibility with Rails" do
24
27
  expect { evaljs("Routes.book_title_path()") }
25
28
  .to raise_error(/Route missing required keys: title/)
26
29
 
27
- expect( evaljs("try {Routes.thing_path()} catch (e) { e.name }") ).to eq('ParametersMissing')
28
- expect( evaljs("try {Routes.thing_path()} catch (e) { e.keys }") ).to eq(['id'])
30
+ expectjs("try {Routes.thing_path()} catch (e) { e.name }") .to eq('ParametersMissing')
31
+ expectjs("try {Routes.thing_path()} catch (e) { e.keys }") .to eq(['id'])
29
32
  end
30
33
 
31
34
  it "should produce error stacktraces including function names" do
@@ -42,129 +45,145 @@ describe JsRoutes, "compatibility with Rails" do
42
45
  end
43
46
 
44
47
  it "should support 0 as a member parameter" do
45
- expect(evaljs("Routes.inbox_path(0)")).to eq(test_routes.inbox_path(0))
48
+ expectjs("Routes.inbox_path(0)").to eq(test_routes.inbox_path(0))
46
49
  end
47
50
 
48
51
  it "should generate nested routing with one parameter" do
49
- expect(evaljs("Routes.inbox_messages_path(1)")).to eq(test_routes.inbox_messages_path(1))
52
+ expectjs("Routes.inbox_messages_path(1)").to eq(test_routes.inbox_messages_path(1))
50
53
  end
51
54
 
52
55
  it "should generate nested routing" do
53
- expect(evaljs("Routes.inbox_message_path(1,2)")).to eq(test_routes.inbox_message_path(1, 2))
56
+ expectjs("Routes.inbox_message_path(1,2)").to eq(test_routes.inbox_message_path(1, 2))
54
57
  end
55
58
 
56
59
  it "should generate routing with format" do
57
- expect(evaljs("Routes.inbox_path(1, {format: 'json'})")).to eq(test_routes.inbox_path(1, :format => "json"))
60
+ expectjs("Routes.inbox_path(1, {format: 'json'})").to eq(test_routes.inbox_path(1, :format => "json"))
58
61
  end
59
62
 
60
63
  it "should support routes with reserved javascript words as parameters" do
61
- expect(evaljs("Routes.object_path(1, 2)")).to eq(test_routes.object_path(1,2))
64
+ expectjs("Routes.object_path(1, 2)").to eq(test_routes.object_path(1,2))
62
65
  end
63
66
 
64
67
  it "should support routes with trailing_slash" do
65
- expect(evaljs("Routes.inbox_path(1, {trailing_slash: true})")).to eq(test_routes.inbox_path(1, trailing_slash: true))
68
+ expectjs("Routes.inbox_path(1, {trailing_slash: true})").to eq(test_routes.inbox_path(1, trailing_slash: true))
66
69
  end
67
70
 
68
71
  it "should support url anchor given as parameter" do
69
- expect(evaljs("Routes.inbox_path(1, {anchor: 'hello'})")).to eq(test_routes.inbox_path(1, :anchor => "hello"))
72
+ expectjs("Routes.inbox_path(1, {anchor: 'hello'})").to eq(test_routes.inbox_path(1, :anchor => "hello"))
70
73
  end
71
74
 
72
75
  it "should support url anchor and get parameters" do
73
- expect(evaljs("Routes.inbox_path(1, {expanded: true, anchor: 'hello'})")).to eq(test_routes.inbox_path(1, :expanded => true, :anchor => "hello"))
76
+ expectjs("Routes.inbox_path(1, {expanded: true, anchor: 'hello'})").to eq(test_routes.inbox_path(1, :expanded => true, :anchor => "hello"))
74
77
  end
75
78
 
76
79
  it "should support required parameters given as options hash" do
77
- expect(evaljs("Routes.search_path({q: 'hello'})")).to eq(test_routes.search_path(:q => 'hello'))
80
+ expectjs("Routes.search_path({q: 'hello'})").to eq(test_routes.search_path(:q => 'hello'))
78
81
  end
79
82
 
80
83
  it "should use irregular ActiveSupport pluralizations" do
81
- expect(evaljs("Routes.budgies_path()")).to eq(test_routes.budgies_path)
82
- expect(evaljs("Routes.budgie_path(1)")).to eq(test_routes.budgie_path(1))
83
- expect(evaljs("Routes.budgy_path")).to eq(nil)
84
- expect(evaljs("Routes.budgie_descendents_path(1)")).to eq(test_routes.budgie_descendents_path(1))
84
+ expectjs("Routes.budgies_path()").to eq(test_routes.budgies_path)
85
+ expectjs("Routes.budgie_path(1)").to eq(test_routes.budgie_path(1))
86
+ expectjs("Routes.budgy_path").to eq(nil)
87
+ expectjs("Routes.budgie_descendents_path(1)").to eq(test_routes.budgie_descendents_path(1))
85
88
  end
86
89
 
87
90
  it "should support route with parameters containing symbols that need URI-encoding", :aggregate_failures do
88
- expect(evaljs("Routes.inbox_path('#hello')")).to eq(test_routes.inbox_path('#hello'))
89
- expect(evaljs("Routes.inbox_path('some param')")).to eq(test_routes.inbox_path('some param'))
90
- expect(evaljs("Routes.inbox_path('some param with more & more encode symbols')")).to eq(test_routes.inbox_path('some param with more & more encode symbols'))
91
+ expectjs("Routes.inbox_path('#hello')").to eq(test_routes.inbox_path('#hello'))
92
+ expectjs("Routes.inbox_path('some param')").to eq(test_routes.inbox_path('some param'))
93
+ expectjs("Routes.inbox_path('some param with more & more encode symbols')").to eq(test_routes.inbox_path('some param with more & more encode symbols'))
91
94
  end
92
95
  it "should support route with parameters containing symbols not need URI-encoding", :aggregate_failures do
93
- expect(evaljs("Routes.inbox_path(':some_id')")).to eq(test_routes.inbox_path(':some_id'))
94
- expect(evaljs("Routes.inbox_path('.+')")).to eq(test_routes.inbox_path('.+'))
96
+ expectjs("Routes.inbox_path(':some_id')").to eq(test_routes.inbox_path(':some_id'))
97
+ expectjs("Routes.inbox_path('.+')").to eq(test_routes.inbox_path('.+'))
95
98
  end
96
99
 
97
100
  describe "when route has defaults" do
98
101
  it "should support route default format" do
99
- expect(evaljs("Routes.api_purchases_path()")).to eq(test_routes.api_purchases_path)
102
+ expectjs("Routes.api_purchases_path()").to eq(test_routes.api_purchases_path)
100
103
  end
101
104
 
102
105
  it 'should support route default subdomain' do
103
- expect(evaljs("Routes.backend_root_path()")).to eq(test_routes.backend_root_path)
106
+ expectjs("Routes.backend_root_path()").to eq(test_routes.backend_root_path)
104
107
  end
105
108
 
106
109
  it "should support default format override" do
107
- expect(evaljs("Routes.api_purchases_path({format: 'xml'})")).to eq(test_routes.api_purchases_path(format: 'xml'))
110
+ expectjs("Routes.api_purchases_path({format: 'xml'})").to eq(test_routes.api_purchases_path(format: 'xml'))
108
111
  end
109
112
 
110
113
  it "should support default format override by passing it in args" do
111
- expect(evaljs("Routes.api_purchases_path('xml')")).to eq(test_routes.api_purchases_path('xml'))
114
+ expectjs("Routes.api_purchases_path('xml')").to eq(test_routes.api_purchases_path('xml'))
112
115
  end
113
116
 
114
117
  it "doesn't apply defaults to path" do
115
- expect(evaljs("Routes.with_defaults_path()")).to eq(test_routes.with_defaults_path)
116
- expect(evaljs("Routes.with_defaults_path({format: 'json'})")).to eq(test_routes.with_defaults_path(format: 'json'))
118
+ expectjs("Routes.with_defaults_path()").to eq(test_routes.with_defaults_path)
119
+ expectjs("Routes.with_defaults_path({format: 'json'})").to eq(test_routes.with_defaults_path(format: 'json'))
117
120
  end
118
121
  end
119
122
 
120
123
  context "with rails engines" do
121
124
  it "should support simple route" do
122
- expect(evaljs("Routes.blog_app_posts_path()")).to eq(blog_routes.posts_path())
125
+ expectjs("Routes.blog_app_posts_path()").to eq(blog_routes.posts_path())
123
126
  end
124
127
 
125
128
  it "should support root route" do
126
- expect(evaljs("Routes.blog_app_path()")).to eq(test_routes.blog_app_path())
129
+ expectjs("Routes.blog_app_path()").to eq(test_routes.blog_app_path())
127
130
  end
128
131
 
129
132
  it "should support route with parameters" do
130
- expect(evaljs("Routes.blog_app_post_path(1)")).to eq(blog_routes.post_path(1))
133
+ expectjs("Routes.blog_app_post_path(1)").to eq(blog_routes.post_path(1))
131
134
  end
132
135
  it "should support root path" do
133
- expect(evaljs("Routes.blog_app_root_path()")).to eq(blog_routes.root_path)
136
+ expectjs("Routes.blog_app_root_path()").to eq(blog_routes.root_path)
134
137
  end
135
138
  it "should support single route mapping" do
136
- expect(evaljs("Routes.support_path({page: 3})")).to eq(test_routes.support_path(:page => 3))
139
+ expectjs("Routes.support_path({page: 3})").to eq(test_routes.support_path(:page => 3))
137
140
  end
138
141
 
139
142
  it 'works' do
140
- expect(evaljs("Routes.planner_manage_path({locale: 'ua'})")).to eq(planner_routes.manage_path(locale: 'ua'))
141
- expect(evaljs("Routes.planner_manage_path()")).to eq(planner_routes.manage_path)
143
+ expectjs("Routes.planner_manage_path({locale: 'ua'})").to eq(planner_routes.manage_path(locale: 'ua'))
144
+ expectjs("Routes.planner_manage_path()").to eq(planner_routes.manage_path)
142
145
  end
143
146
  end
144
147
 
145
148
  it "shouldn't require the format" do
146
- expect(evaljs("Routes.json_only_path({format: 'json'})")).to eq(test_routes.json_only_path(:format => 'json'))
149
+ expectjs("Routes.json_only_path({format: 'json'})").to eq(test_routes.json_only_path(:format => 'json'))
147
150
  end
148
151
 
149
152
  it "should serialize object with empty string value" do
150
- expect(evaljs("Routes.inboxes_path({a: '', b: 1})")).to eq(test_routes.inboxes_path(:a => '', :b => 1))
153
+ expectjs("Routes.inboxes_path({a: '', b: 1})").to eq(test_routes.inboxes_path(:a => '', :b => 1))
151
154
  end
152
155
 
153
156
  it "should support utf-8 route" do
154
- expect(evaljs("Routes.hello_path()")).to eq(test_routes.hello_path)
157
+ expectjs("Routes.hello_path()").to eq(test_routes.hello_path)
155
158
  end
156
159
 
157
160
  it "should support root_path" do
158
- expect(evaljs("Routes.root_path()")).to eq(test_routes.root_path)
161
+ expectjs("Routes.root_path()").to eq(test_routes.root_path)
162
+ end
163
+
164
+ describe "params parameter" do
165
+ it "works" do
166
+ expectjs("Routes.inboxes_path({params: {key: 'value'}})").to eq(test_routes.inboxes_path(params: {key: 'value'}))
167
+ end
168
+
169
+ it "allows keyword key as a query parameter" do
170
+ expectjs("Routes.inboxes_path({params: {anchor: 'a', params: 'p'}})").to eq(test_routes.inboxes_path(params: {anchor: 'a', params: 'p'}))
171
+ end
172
+
173
+ it "throws when value is not an object" do
174
+ expect {
175
+ evaljs("Routes.inboxes_path({params: 1})")
176
+ }.to raise_error(js_error_class)
177
+ end
159
178
  end
160
179
 
161
180
  describe "get parameters" do
162
181
  it "should support simple get parameters" do
163
- expect(evaljs("Routes.inbox_path(1, {format: 'json', lang: 'ua', q: 'hello'})")).to eq(test_routes.inbox_path(1, :lang => "ua", :q => "hello", :format => "json"))
182
+ expectjs("Routes.inbox_path(1, {format: 'json', lang: 'ua', q: 'hello'})").to eq(test_routes.inbox_path(1, :lang => "ua", :q => "hello", :format => "json"))
164
183
  end
165
184
 
166
185
  it "should support array get parameters" do
167
- expect(evaljs("Routes.inbox_path(1, {hello: ['world', 'mars']})")).to eq(test_routes.inbox_path(1, :hello => [:world, :mars]))
186
+ expectjs("Routes.inbox_path(1, {hello: ['world', 'mars']})").to eq(test_routes.inbox_path(1, :hello => [:world, :mars]))
168
187
  end
169
188
 
170
189
  context "object without prototype" do
@@ -174,94 +193,94 @@ describe JsRoutes, "compatibility with Rails" do
174
193
  end
175
194
 
176
195
  it "should still work correctly" do
177
- expect(evaljs("Routes.inbox_path(inbox, params)")).to eq(
196
+ expectjs("Routes.inbox_path(inbox, params)").to eq(
178
197
  test_routes.inbox_path(1, q: "hello")
179
198
  )
180
199
  end
181
200
  end
182
201
 
183
202
  it "should support nested get parameters" do
184
- expect(evaljs("Routes.inbox_path(1, {format: 'json', env: 'test', search: { category_ids: [2,5], q: 'hello'}})")).to eq(
203
+ expectjs("Routes.inbox_path(1, {format: 'json', env: 'test', search: { category_ids: [2,5], q: 'hello'}})").to eq(
185
204
  test_routes.inbox_path(1, :env => 'test', :search => {:category_ids => [2,5], :q => "hello"}, :format => "json")
186
205
  )
187
206
  end
188
207
 
189
208
  it "should support null and undefined parameters" do
190
- expect(evaljs("Routes.inboxes_path({uri: null, key: undefined})")).to eq(test_routes.inboxes_path(:uri => nil, :key => nil))
209
+ expectjs("Routes.inboxes_path({uri: null, key: undefined})").to eq(test_routes.inboxes_path(:uri => nil, :key => nil))
191
210
  end
192
211
 
193
212
  it "should escape get parameters" do
194
- expect(evaljs("Routes.inboxes_path({uri: 'http://example.com'})")).to eq(test_routes.inboxes_path(:uri => 'http://example.com'))
213
+ expectjs("Routes.inboxes_path({uri: 'http://example.com'})").to eq(test_routes.inboxes_path(:uri => 'http://example.com'))
195
214
  end
196
215
 
197
216
  it "should support nested object null parameters" do
198
- expect(evaljs("Routes.inboxes_path({hello: {world: null}})")).to eq(test_routes.inboxes_path(:hello => {:world => nil}))
217
+ expectjs("Routes.inboxes_path({hello: {world: null}})").to eq(test_routes.inboxes_path(:hello => {:world => nil}))
199
218
  end
200
219
  end
201
220
 
202
221
 
203
222
  context "routes globbing" do
204
223
  it "should be supported as parameters" do
205
- expect(evaljs("Routes.book_path('thrillers', 1)")).to eq(test_routes.book_path('thrillers', 1))
224
+ expectjs("Routes.book_path('thrillers', 1)").to eq(test_routes.book_path('thrillers', 1))
206
225
  end
207
226
 
208
227
  it "should support routes globbing as array" do
209
- expect(evaljs("Routes.book_path(['thrillers'], 1)")).to eq(test_routes.book_path(['thrillers'], 1))
228
+ expectjs("Routes.book_path(['thrillers'], 1)").to eq(test_routes.book_path(['thrillers'], 1))
210
229
  end
211
230
 
212
231
  it "should support routes globbing as array" do
213
- expect(evaljs("Routes.book_path([1, 2, 3], 1)")).to eq(test_routes.book_path([1, 2, 3], 1))
232
+ expectjs("Routes.book_path([1, 2, 3], 1)").to eq(test_routes.book_path([1, 2, 3], 1))
214
233
  end
215
234
 
216
235
  it "should support routes globbing with slash" do
217
- expect(evaljs("Routes.book_path('a_test/b_test/c_test', 1)")).to eq(test_routes.book_path('a_test/b_test/c_test', 1))
236
+ expectjs("Routes.book_path('a_test/b_test/c_test', 1)").to eq(test_routes.book_path('a_test/b_test/c_test', 1))
218
237
  end
219
238
 
220
239
  it "should support routes globbing as hash" do
221
- expect(evaljs("Routes.book_path('a%b', 1)")).to eq(test_routes.book_path('a%b', 1))
240
+ expectjs("Routes.book_path('a%b', 1)").to eq(test_routes.book_path('a%b', 1))
222
241
  end
223
242
 
224
243
  it "should support routes globbing as array with optional params" do
225
- expect(evaljs("Routes.book_path([1, 2, 3, 5], 1, {c: '1'})")).to eq(test_routes.book_path([1, 2, 3, 5], 1, { :c => "1" }))
244
+ expectjs("Routes.book_path([1, 2, 3, 5], 1, {c: '1'})").to eq(test_routes.book_path([1, 2, 3, 5], 1, { :c => "1" }))
226
245
  end
227
246
 
228
247
  it "should support routes globbing in book_title route as array" do
229
- expect(evaljs("Routes.book_title_path('john', ['thrillers', 'comedian'])")).to eq(test_routes.book_title_path('john', ['thrillers', 'comedian']))
248
+ expectjs("Routes.book_title_path('john', ['thrillers', 'comedian'])").to eq(test_routes.book_title_path('john', ['thrillers', 'comedian']))
230
249
  end
231
250
 
232
251
  it "should support routes globbing in book_title route as array with optional params" do
233
- expect(evaljs("Routes.book_title_path('john', ['thrillers', 'comedian'], {some_key: 'some_value'})")).to eq(test_routes.book_title_path('john', ['thrillers', 'comedian'], {:some_key => 'some_value'}))
252
+ expectjs("Routes.book_title_path('john', ['thrillers', 'comedian'], {some_key: 'some_value'})").to eq(test_routes.book_title_path('john', ['thrillers', 'comedian'], {:some_key => 'some_value'}))
234
253
  end
235
254
  end
236
255
 
237
256
  context "using optional path fragments" do
238
257
  context "including not optional parts" do
239
258
  it "should include everything that is not optional" do
240
- expect(evaljs("Routes.foo_path()")).to eq(test_routes.foo_path)
259
+ expectjs("Routes.foo_path()").to eq(test_routes.foo_path)
241
260
  end
242
261
  end
243
262
 
244
263
  context "but not including them" do
245
264
  it "should not include the optional parts" do
246
- expect(evaljs("Routes.things_path()")).to eq(test_routes.things_path)
247
- expect(evaljs("Routes.things_path({ q: 'hello' })")).to eq(test_routes.things_path(q: 'hello'))
265
+ expectjs("Routes.things_path()").to eq(test_routes.things_path)
266
+ expectjs("Routes.things_path({ q: 'hello' })").to eq(test_routes.things_path(q: 'hello'))
248
267
  end
249
268
 
250
269
  it "treats false as absent optional part" do
251
270
  pending("https://github.com/rails/rails/issues/42280")
252
- expect(evaljs("Routes.things_path(false)")).to eq(test_routes.things_path(false))
271
+ expectjs("Routes.things_path(false)").to eq(test_routes.things_path(false))
253
272
  end
254
273
 
255
274
  it "treats false as absent optional part when default is specified" do
256
- expect(evaljs("Routes.campaigns_path(false)")).to eq(test_routes.campaigns_path(false))
275
+ expectjs("Routes.campaigns_path(false)").to eq(test_routes.campaigns_path(false))
257
276
  end
258
277
 
259
278
  it "should not require the optional parts as arguments" do
260
- expect(evaljs("Routes.thing_path(null, 5)")).to eq(test_routes.thing_path(nil, 5))
279
+ expectjs("Routes.thing_path(null, 5)").to eq(test_routes.thing_path(nil, 5))
261
280
  end
262
281
 
263
282
  it "should treat undefined as non-given optional part" do
264
- expect(evaljs("Routes.thing_path(5, {optional_id: undefined})")).to eq(test_routes.thing_path(5, :optional_id => nil))
283
+ expectjs("Routes.thing_path(5, {optional_id: undefined})").to eq(test_routes.thing_path(5, :optional_id => nil))
265
284
  end
266
285
 
267
286
  it "should raise error when passing non-full list of arguments and some query params" do
@@ -270,15 +289,15 @@ describe JsRoutes, "compatibility with Rails" do
270
289
  end
271
290
 
272
291
  it "should treat null as non-given optional part" do
273
- expect(evaljs("Routes.thing_path(5, {optional_id: null})")).to eq(test_routes.thing_path(5, :optional_id => nil))
292
+ expectjs("Routes.thing_path(5, {optional_id: null})").to eq(test_routes.thing_path(5, :optional_id => nil))
274
293
  end
275
294
 
276
295
  it "should work when passing required params in options" do
277
- expect(evaljs("Routes.thing_deep_path({second_required: 1, third_required: 2})")).to eq(test_routes.thing_deep_path(second_required: 1, third_required: 2))
296
+ expectjs("Routes.thing_deep_path({second_required: 1, third_required: 2})").to eq(test_routes.thing_deep_path(second_required: 1, third_required: 2))
278
297
  end
279
298
 
280
299
  it "should skip leading and trailing optional parts" do
281
- expect(evaljs("Routes.thing_deep_path(1, 2)")).to eq(test_routes.thing_deep_path(1, 2))
300
+ expectjs("Routes.thing_deep_path(1, 2)").to eq(test_routes.thing_deep_path(1, 2))
282
301
  end
283
302
  end
284
303
 
@@ -288,30 +307,30 @@ describe JsRoutes, "compatibility with Rails" do
288
307
  end
289
308
 
290
309
  it "should include the optional parts" do
291
- expect(evaljs("Routes.things_path({optional_id: 5})")).to eq(test_routes.things_path(:optional_id => 5))
292
- expect(evaljs("Routes.things_path(5)")).to eq(test_routes.things_path(5))
293
- expect(evaljs("Routes.thing_deep_path(1, { third_required: 3, second_required: 2 })")).to eq(test_routes.thing_deep_path(1, third_required: 3, second_required: 2))
294
- expect(evaljs("Routes.thing_deep_path(1, { third_required: 3, second_required: 2, forth_optional: 4 })")).to eq(test_routes.thing_deep_path(1, third_required: 3, second_required: 2, forth_optional: 4))
295
- expect(evaljs("Routes.thing_deep_path(2, { third_required: 3, first_optional: 1 })")).to eq(test_routes.thing_deep_path(2, third_required: 3, first_optional: 1))
296
- expect(evaljs("Routes.thing_deep_path(3, { first_optional: 1, second_required: 2 })")).to eq(test_routes.thing_deep_path(3, first_optional: 1, second_required: 2))
297
- expect(evaljs("Routes.thing_deep_path(3, { first_optional: 1, second_required: 2, forth_optional: 4 })")).to eq(test_routes.thing_deep_path(3, first_optional: 1, second_required: 2, forth_optional: 4))
298
- expect(evaljs("Routes.thing_deep_path(4, { first_optional: 1, second_required: 2, third_required: 3 })")).to eq(test_routes.thing_deep_path(4, first_optional: 1, second_required: 2, third_required: 3))
299
- expect(evaljs("Routes.thing_deep_path(2, 3)")).to eq(test_routes.thing_deep_path(2, 3))
300
- expect(evaljs("Routes.thing_deep_path(1, 2, { third_required: 3 })")).to eq(test_routes.thing_deep_path(1, 2, third_required: 3))
301
- expect(evaljs("Routes.thing_deep_path(1,2, {third_required: 3, q: 'bogdan'})")).to eq(test_routes.thing_deep_path(1,2, {third_required: 3, q: 'bogdan'}))
302
- expect(evaljs("Routes.thing_deep_path(1, 2, { forth_optional: 4, third_required: 3 })")).to eq(test_routes.thing_deep_path(1, 2, forth_optional: 4, third_required: 3))
303
- expect(evaljs("Routes.thing_deep_path(1, 3, { second_required: 2 })")).to eq(test_routes.thing_deep_path(1, 3, second_required: 2))
304
- expect(evaljs("Routes.thing_deep_path(1, 4, { second_required: 2, third_required: 3 })")).to eq(test_routes.thing_deep_path(1, 4, second_required: 2, third_required: 3))
305
- expect(evaljs("Routes.thing_deep_path(2, 3, { first_optional: 1 })")).to eq(test_routes.thing_deep_path(2, 3, first_optional: 1))
306
- expect(evaljs("Routes.thing_deep_path(2, 3, { first_optional: 1, forth_optional: 4 })")).to eq(test_routes.thing_deep_path(2, 3, first_optional: 1, forth_optional: 4))
307
- expect(evaljs("Routes.thing_deep_path(2, 4, { first_optional: 1, third_required: 3 })")).to eq(test_routes.thing_deep_path(2, 4, first_optional: 1, third_required: 3))
308
- expect(evaljs("Routes.thing_deep_path(3, 4, { first_optional: 1, second_required: 2 })")).to eq(test_routes.thing_deep_path(3, 4, first_optional: 1, second_required: 2))
309
- expect(evaljs("Routes.thing_deep_path(1, 2, 3)")).to eq(test_routes.thing_deep_path(1, 2, 3))
310
- expect(evaljs("Routes.thing_deep_path(1, 2, 3, { forth_optional: 4 })")).to eq(test_routes.thing_deep_path(1, 2, 3, forth_optional: 4))
311
- expect(evaljs("Routes.thing_deep_path(1, 2, 4, { third_required: 3 })")).to eq(test_routes.thing_deep_path(1, 2, 4, third_required: 3))
312
- expect(evaljs("Routes.thing_deep_path(1, 3, 4, { second_required: 2 })")).to eq(test_routes.thing_deep_path(1, 3, 4, second_required: 2))
313
- expect(evaljs("Routes.thing_deep_path(2, 3, 4, { first_optional: 1 })")).to eq(test_routes.thing_deep_path(2, 3, 4, first_optional: 1))
314
- expect(evaljs("Routes.thing_deep_path(1, 2, 3, 4)")).to eq(test_routes.thing_deep_path(1, 2, 3, 4))
310
+ expectjs("Routes.things_path({optional_id: 5})").to eq(test_routes.things_path(:optional_id => 5))
311
+ expectjs("Routes.things_path(5)").to eq(test_routes.things_path(5))
312
+ expectjs("Routes.thing_deep_path(1, { third_required: 3, second_required: 2 })").to eq(test_routes.thing_deep_path(1, third_required: 3, second_required: 2))
313
+ expectjs("Routes.thing_deep_path(1, { third_required: 3, second_required: 2, forth_optional: 4 })").to eq(test_routes.thing_deep_path(1, third_required: 3, second_required: 2, forth_optional: 4))
314
+ expectjs("Routes.thing_deep_path(2, { third_required: 3, first_optional: 1 })").to eq(test_routes.thing_deep_path(2, third_required: 3, first_optional: 1))
315
+ expectjs("Routes.thing_deep_path(3, { first_optional: 1, second_required: 2 })").to eq(test_routes.thing_deep_path(3, first_optional: 1, second_required: 2))
316
+ expectjs("Routes.thing_deep_path(3, { first_optional: 1, second_required: 2, forth_optional: 4 })").to eq(test_routes.thing_deep_path(3, first_optional: 1, second_required: 2, forth_optional: 4))
317
+ expectjs("Routes.thing_deep_path(4, { first_optional: 1, second_required: 2, third_required: 3 })").to eq(test_routes.thing_deep_path(4, first_optional: 1, second_required: 2, third_required: 3))
318
+ expectjs("Routes.thing_deep_path(2, 3)").to eq(test_routes.thing_deep_path(2, 3))
319
+ expectjs("Routes.thing_deep_path(1, 2, { third_required: 3 })").to eq(test_routes.thing_deep_path(1, 2, third_required: 3))
320
+ expectjs("Routes.thing_deep_path(1,2, {third_required: 3, q: 'bogdan'})").to eq(test_routes.thing_deep_path(1,2, {third_required: 3, q: 'bogdan'}))
321
+ expectjs("Routes.thing_deep_path(1, 2, { forth_optional: 4, third_required: 3 })").to eq(test_routes.thing_deep_path(1, 2, forth_optional: 4, third_required: 3))
322
+ expectjs("Routes.thing_deep_path(1, 3, { second_required: 2 })").to eq(test_routes.thing_deep_path(1, 3, second_required: 2))
323
+ expectjs("Routes.thing_deep_path(1, 4, { second_required: 2, third_required: 3 })").to eq(test_routes.thing_deep_path(1, 4, second_required: 2, third_required: 3))
324
+ expectjs("Routes.thing_deep_path(2, 3, { first_optional: 1 })").to eq(test_routes.thing_deep_path(2, 3, first_optional: 1))
325
+ expectjs("Routes.thing_deep_path(2, 3, { first_optional: 1, forth_optional: 4 })").to eq(test_routes.thing_deep_path(2, 3, first_optional: 1, forth_optional: 4))
326
+ expectjs("Routes.thing_deep_path(2, 4, { first_optional: 1, third_required: 3 })").to eq(test_routes.thing_deep_path(2, 4, first_optional: 1, third_required: 3))
327
+ expectjs("Routes.thing_deep_path(3, 4, { first_optional: 1, second_required: 2 })").to eq(test_routes.thing_deep_path(3, 4, first_optional: 1, second_required: 2))
328
+ expectjs("Routes.thing_deep_path(1, 2, 3)").to eq(test_routes.thing_deep_path(1, 2, 3))
329
+ expectjs("Routes.thing_deep_path(1, 2, 3, { forth_optional: 4 })").to eq(test_routes.thing_deep_path(1, 2, 3, forth_optional: 4))
330
+ expectjs("Routes.thing_deep_path(1, 2, 4, { third_required: 3 })").to eq(test_routes.thing_deep_path(1, 2, 4, third_required: 3))
331
+ expectjs("Routes.thing_deep_path(1, 3, 4, { second_required: 2 })").to eq(test_routes.thing_deep_path(1, 3, 4, second_required: 2))
332
+ expectjs("Routes.thing_deep_path(2, 3, 4, { first_optional: 1 })").to eq(test_routes.thing_deep_path(2, 3, 4, first_optional: 1))
333
+ expectjs("Routes.thing_deep_path(1, 2, 3, 4)").to eq(test_routes.thing_deep_path(1, 2, 3, 4))
315
334
 
316
335
  end
317
336
 
@@ -319,7 +338,7 @@ describe JsRoutes, "compatibility with Rails" do
319
338
  if Rails.version <= "5.0.0"
320
339
  # this type of routing is deprecated
321
340
  it "should include everything that is not optional" do
322
- expect(evaljs("Routes.classic_path({controller: 'classic', action: 'edit'})")).to eq(test_routes.classic_path(controller: :classic, action: :edit))
341
+ expectjs("Routes.classic_path({controller: 'classic', action: 'edit'})").to eq(test_routes.classic_path(controller: :classic, action: :edit))
323
342
  end
324
343
  end
325
344
  end
@@ -358,7 +377,7 @@ describe JsRoutes, "compatibility with Rails" do
358
377
  evaljs("Array.prototype.indexOf = null")
359
378
  end
360
379
  it "should still work correctly" do
361
- expect(evaljs("Routes.inboxes_path()")).to eq(test_routes.inboxes_path())
380
+ expectjs("Routes.inboxes_path()").to eq(test_routes.inboxes_path())
362
381
  end
363
382
  end
364
383
 
@@ -379,98 +398,69 @@ describe JsRoutes, "compatibility with Rails" do
379
398
  }.to raise_error(js_error_class)
380
399
  end
381
400
  it "should support 0 as a to_param option" do
382
- expect(evaljs("Routes.inbox_path({to_param: 0})")).to eq(test_routes.inbox_path(Struct.new(:to_param).new('0')))
401
+ expectjs("Routes.inbox_path({to_param: 0})").to eq(test_routes.inbox_path(Struct.new(:to_param).new('0')))
383
402
  end
384
403
 
385
404
  it "should check for options special key" do
386
- expect(evaljs("Routes.inbox_path({id: 7, q: 'hello', _options: true})")).to eq(test_routes.inbox_path(id: 7, q: 'hello'))
405
+ expectjs("Routes.inbox_path({id: 7, q: 'hello', _options: true})").to eq(test_routes.inbox_path(id: 7, q: 'hello'))
387
406
  expect {
388
407
  evaljs("Routes.inbox_path({to_param: 7, _options: true})")
389
408
  }.to raise_error(js_error_class)
390
- expect(evaljs("Routes.inbox_message_path(5, {id: 7, q: 'hello', _options: true})")).to eq(test_routes.inbox_message_path(5, id: 7, q: 'hello'))
409
+ expectjs("Routes.inbox_message_path(5, {id: 7, q: 'hello', _options: true})").to eq(test_routes.inbox_message_path(5, id: 7, q: 'hello'))
391
410
  end
392
411
 
393
412
  it "should support 0 as an id option" do
394
- expect(evaljs("Routes.inbox_path({id: 0})")).to eq(test_routes.inbox_path(0))
413
+ expectjs("Routes.inbox_path({id: 0})").to eq(test_routes.inbox_path(0))
395
414
  end
396
415
 
397
416
  it "should use id property of the object in path" do
398
- expect(evaljs("Routes.inbox_path({id: 1})")).to eq(test_routes.inbox_path(1))
417
+ expectjs("Routes.inbox_path({id: 1})").to eq(test_routes.inbox_path(1))
399
418
  end
400
419
 
401
420
  it "should prefer to_param property over id property" do
402
- expect(evaljs("Routes.inbox_path({id: 1, to_param: 'my'})")).to eq(test_routes.inbox_path(inbox))
421
+ expectjs("Routes.inbox_path({id: 1, to_param: 'my'})").to eq(test_routes.inbox_path(inbox))
403
422
  end
404
423
 
405
424
  it "should call to_param if it is a function" do
406
- expect(evaljs("Routes.inbox_path({id: 1, to_param: function(){ return 'my';}})")).to eq(test_routes.inbox_path(inbox))
425
+ expectjs("Routes.inbox_path({id: 1, to_param: function(){ return 'my';}})").to eq(test_routes.inbox_path(inbox))
407
426
  end
408
427
 
409
428
  it "should call id if it is a function" do
410
- expect(evaljs("Routes.inbox_path({id: function() { return 1;}})")).to eq(test_routes.inbox_path(1))
429
+ expectjs("Routes.inbox_path({id: function() { return 1;}})").to eq(test_routes.inbox_path(1))
411
430
  end
412
431
 
413
432
  it "should support options argument" do
414
- expect(evaljs(
433
+ expectjs(
415
434
  "Routes.inbox_message_path({id:1, to_param: 'my'}, {id:2}, {custom: true, format: 'json'})"
416
- )).to eq(test_routes.inbox_message_path(inbox, 2, :custom => true, :format => "json"))
435
+ ).to eq(test_routes.inbox_message_path(inbox, 2, :custom => true, :format => "json"))
417
436
  end
418
437
 
419
438
  it "supports camel case property name" do
420
- expect(evaljs("Routes.inbox_path({id: 1, toParam: 'my'})")).to eq(test_routes.inbox_path(inbox))
439
+ expectjs("Routes.inbox_path({id: 1, toParam: 'my'})").to eq(test_routes.inbox_path(inbox))
421
440
  end
422
441
 
423
442
  it "supports camel case method name" do
424
- expect(evaljs("Routes.inbox_path({id: 1, toParam: function(){ return 'my';}})")).to eq(test_routes.inbox_path(inbox))
443
+ expectjs("Routes.inbox_path({id: 1, toParam: function(){ return 'my';}})").to eq(test_routes.inbox_path(inbox))
425
444
  end
426
445
 
427
446
  context "when globbing" do
428
447
  it "should prefer to_param property over id property" do
429
- expect(evaljs("Routes.book_path({id: 1, to_param: 'my'}, 1)")).to eq(test_routes.book_path(inbox, 1))
448
+ expectjs("Routes.book_path({id: 1, to_param: 'my'}, 1)").to eq(test_routes.book_path(inbox, 1))
430
449
  end
431
450
 
432
451
  it "should call to_param if it is a function" do
433
- expect(evaljs("Routes.book_path({id: 1, to_param: function(){ return 'my';}}, 1)")).to eq(test_routes.book_path(inbox, 1))
452
+ expectjs("Routes.book_path({id: 1, to_param: function(){ return 'my';}}, 1)").to eq(test_routes.book_path(inbox, 1))
434
453
  end
435
454
 
436
455
  it "should call id if it is a function" do
437
- expect(evaljs("Routes.book_path({id: function() { return 'technical';}}, 1)")).to eq(test_routes.book_path('technical', 1))
456
+ expectjs("Routes.book_path({id: function() { return 'technical';}}, 1)").to eq(test_routes.book_path('technical', 1))
438
457
  end
439
458
 
440
459
  it "should support options argument" do
441
- expect(evaljs(
460
+ expectjs(
442
461
  "Routes.book_path({id:1, to_param: 'my'}, {id:2}, {custom: true, format: 'json'})"
443
- )).to eq(test_routes.book_path(inbox, 2, :custom => true, :format => "json"))
462
+ ).to eq(test_routes.book_path(inbox, 2, :custom => true, :format => "json"))
444
463
  end
445
464
  end
446
-
447
- end
448
-
449
- context "when specs" do
450
- it "should show inbox spec" do
451
- expect(evaljs("Routes.inbox_path.toString()")).to eq('/inboxes/:id(.:format)')
452
- end
453
-
454
- it "should show inbox spec convert to string" do
455
- expect(evaljs("'' + Routes.inbox_path")).to eq('/inboxes/:id(.:format)')
456
- end
457
-
458
- it "should show inbox message spec" do
459
- expect(evaljs("Routes.inbox_message_path.toString()")).to eq('/inboxes/:inbox_id/messages/:id(.:format)')
460
- end
461
-
462
- it "should show inbox message spec convert to string" do
463
- expect(evaljs("'' + Routes.inbox_message_path")).to eq('/inboxes/:inbox_id/messages/:id(.:format)')
464
- end
465
- end
466
-
467
- describe "requiredParams" do
468
- it "should show inbox spec" do
469
- expect(evaljs("Routes.inbox_path.requiredParams()").to_a).to eq(["id"])
470
- end
471
-
472
- it "should show inbox message spec" do
473
- expect(evaljs("Routes.inbox_message_path.requiredParams()").to_a).to eq(["inbox_id", "id"])
474
- end
475
465
  end
476
466
  end
@@ -0,0 +1,40 @@
1
+
2
+ require "spec_helper"
3
+
4
+ describe JsRoutes, "compatibility with Rails" do
5
+
6
+ let(:generated_js) do
7
+ JsRoutes.generate({module_type: nil, namespace: 'Routes'})
8
+ end
9
+ before(:each) do
10
+ evaljs(generated_js)
11
+ end
12
+
13
+ context "when specs" do
14
+ it "should show inbox spec" do
15
+ expectjs("Routes.inbox_path.toString()").to eq('/inboxes/:id(.:format)')
16
+ end
17
+
18
+ it "should show inbox spec convert to string" do
19
+ expectjs("'' + Routes.inbox_path").to eq('/inboxes/:id(.:format)')
20
+ end
21
+
22
+ it "should show inbox message spec" do
23
+ expectjs("Routes.inbox_message_path.toString()").to eq('/inboxes/:inbox_id/messages/:id(.:format)')
24
+ end
25
+
26
+ it "should show inbox message spec convert to string" do
27
+ expectjs("'' + Routes.inbox_message_path").to eq('/inboxes/:inbox_id/messages/:id(.:format)')
28
+ end
29
+ end
30
+
31
+ describe "requiredParams" do
32
+ it "should show inbox spec" do
33
+ expect(evaljs("Routes.inbox_path.requiredParams()").to_a).to eq(["id"])
34
+ end
35
+
36
+ it "should show inbox message spec" do
37
+ expect(evaljs("Routes.inbox_message_path.requiredParams()").to_a).to eq(["inbox_id", "id"])
38
+ end
39
+ end
40
+ end
data/spec/spec_helper.rb CHANGED
@@ -42,10 +42,14 @@ end
42
42
  def evaljs(string, force: false, filename: 'context.js')
43
43
  jscontext(force).eval(string, filename: filename)
44
44
  rescue MiniRacer::ParseError => e
45
- message = e.message
46
- _, _, line, _ = message.split(':')
47
- code = line && string.split("\n")[line.to_i-1]
48
- raise "#{message}. Code: #{code.strip}";
45
+ trace = e.message
46
+ _, _, line, _ = trace.split(':')
47
+ if line
48
+ code = string.split("\n")[line.to_i-1]
49
+ raise "#{trace}. Code: #{code.strip}";
50
+ else
51
+ raise e
52
+ end
49
53
  rescue MiniRacer::RuntimeError => e
50
54
  raise e
51
55
  end
@@ -62,6 +66,14 @@ def planner_routes
62
66
  Planner::Engine.routes.url_helpers
63
67
  end
64
68
 
69
+ def log(string)
70
+ evaljs("console.log(#{string})")
71
+ end
72
+
73
+ def expectjs(string)
74
+ expect(evaljs(string))
75
+ end
76
+
65
77
  ActiveSupport::Inflector.inflections do |inflect|
66
78
  inflect.irregular "budgie", "budgies"
67
79
  end