js-routes 2.2.8 → 2.2.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -9
  3. data/Readme.md +60 -45
  4. data/lib/js_routes/configuration.rb +80 -34
  5. data/lib/js_routes/engine.rb +2 -0
  6. data/lib/js_routes/generators/base.rb +19 -0
  7. data/lib/js_routes/generators/middleware.rb +6 -8
  8. data/lib/js_routes/generators/webpacker.rb +1 -3
  9. data/lib/js_routes/instance.rb +50 -15
  10. data/lib/js_routes/middleware.rb +16 -3
  11. data/lib/js_routes/route.rb +60 -17
  12. data/lib/js_routes/types.rb +38 -0
  13. data/lib/js_routes/version.rb +2 -1
  14. data/lib/js_routes.rb +25 -7
  15. metadata +28 -45
  16. data/.document +0 -5
  17. data/.eslintrc.js +0 -15
  18. data/.github/workflows/ci.yml +0 -36
  19. data/.gitignore +0 -65
  20. data/.nvmrc +0 -1
  21. data/.rspec +0 -1
  22. data/Appraisals +0 -17
  23. data/Gemfile +0 -4
  24. data/Rakefile +0 -37
  25. data/VERSION_2_UPGRADE.md +0 -66
  26. data/gemfiles/rails50_sprockets_3.gemfile +0 -8
  27. data/gemfiles/rails51_sprockets_3.gemfile +0 -8
  28. data/gemfiles/rails52_sprockets_3.gemfile +0 -8
  29. data/gemfiles/rails70_sprockets_4.gemfile +0 -8
  30. data/js-routes.gemspec +0 -39
  31. data/package.json +0 -38
  32. data/spec/dummy/app/assets/config/manifest.js +0 -2
  33. data/spec/dummy/app/assets/javascripts/.gitkeep +0 -0
  34. data/spec/dummy/config/routes.rb +0 -55
  35. data/spec/js_routes/default_serializer_spec.rb +0 -31
  36. data/spec/js_routes/module_types/amd_spec.rb +0 -35
  37. data/spec/js_routes/module_types/cjs_spec.rb +0 -15
  38. data/spec/js_routes/module_types/dts/routes.spec.d.ts +0 -115
  39. data/spec/js_routes/module_types/dts/test.spec.ts +0 -56
  40. data/spec/js_routes/module_types/dts_spec.rb +0 -111
  41. data/spec/js_routes/module_types/esm_spec.rb +0 -45
  42. data/spec/js_routes/module_types/nil_spec.rb +0 -87
  43. data/spec/js_routes/module_types/umd_spec.rb +0 -85
  44. data/spec/js_routes/options_spec.rb +0 -508
  45. data/spec/js_routes/rails_routes_compatibility_spec.rb +0 -473
  46. data/spec/js_routes/route_specification_spec.rb +0 -37
  47. data/spec/js_routes/zzz_sprockets_spec.rb +0 -152
  48. data/spec/spec_helper.rb +0 -135
  49. data/spec/support/routes.rb +0 -81
  50. data/spec/tsconfig.json +0 -4
  51. data/tsconfig.json +0 -28
  52. data/yarn.lock +0 -2457
@@ -1,473 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe JsRoutes, "compatibility with Rails" do
4
-
5
- before(:each) do
6
- evallib(module_type: nil, namespace: 'Routes')
7
- end
8
-
9
- it "should generate collection routing" do
10
- expectjs("Routes.inboxes_path()").to eq(test_routes.inboxes_path())
11
- end
12
-
13
- it "should generate member routing" do
14
- expectjs("Routes.inbox_path(1)").to eq(test_routes.inbox_path(1))
15
- end
16
-
17
- it "should raise error if required argument is not passed", :aggregate_failures do
18
- expect { evaljs("Routes.thing_path()") }
19
- .to raise_error(/Route missing required keys: id/)
20
- expect { evaljs("Routes.search_path()") }
21
- .to raise_error(/Route missing required keys: q/)
22
- expect { evaljs("Routes.book_path()") }
23
- .to raise_error(/Route missing required keys: section, title/)
24
- expect { evaljs("Routes.book_title_path()") }
25
- .to raise_error(/Route missing required keys: title/)
26
-
27
- expectjs("try {Routes.thing_path()} catch (e) { e.name }") .to eq('ParametersMissing')
28
- expectjs("try {Routes.thing_path()} catch (e) { e.keys }") .to eq(['id'])
29
- end
30
-
31
- it "should produce error stacktraces including function names" do
32
- stacktrace = evaljs("
33
- (function(){
34
- try {
35
- Routes.thing_path()
36
- } catch(e) {
37
- return e.stack;
38
- }
39
- })()
40
- ")
41
- expect(stacktrace).to include "thing_path"
42
- end
43
-
44
- it "should support 0 as a member parameter" do
45
- expectjs("Routes.inbox_path(0)").to eq(test_routes.inbox_path(0))
46
- end
47
-
48
- it "should generate nested routing with one parameter" do
49
- expectjs("Routes.inbox_messages_path(1)").to eq(test_routes.inbox_messages_path(1))
50
- end
51
-
52
- it "should generate nested routing" do
53
- expectjs("Routes.inbox_message_path(1,2)").to eq(test_routes.inbox_message_path(1, 2))
54
- end
55
-
56
- it "should generate routing with format" do
57
- expectjs("Routes.inbox_path(1, {format: 'json'})").to eq(test_routes.inbox_path(1, :format => "json"))
58
- end
59
-
60
- it "should support routes with reserved javascript words as parameters" do
61
- expectjs("Routes.object_path(1, 2)").to eq(test_routes.object_path(1,2))
62
- end
63
-
64
- it "should support routes with trailing_slash" do
65
- expectjs("Routes.inbox_path(1, {trailing_slash: true})").to eq(test_routes.inbox_path(1, trailing_slash: true))
66
- end
67
-
68
- it "should support url anchor given as parameter" do
69
- expectjs("Routes.inbox_path(1, {anchor: 'hello'})").to eq(test_routes.inbox_path(1, :anchor => "hello"))
70
- end
71
-
72
- it "should support url anchor and get parameters" do
73
- expectjs("Routes.inbox_path(1, {expanded: true, anchor: 'hello'})").to eq(test_routes.inbox_path(1, :expanded => true, :anchor => "hello"))
74
- end
75
-
76
- it "should support required parameters given as options hash" do
77
- expectjs("Routes.search_path({q: 'hello'})").to eq(test_routes.search_path(:q => 'hello'))
78
- end
79
-
80
- it "should use irregular ActiveSupport pluralizations" do
81
- expectjs("Routes.budgies_path()").to eq(test_routes.budgies_path)
82
- expectjs("Routes.budgie_path(1)").to eq(test_routes.budgie_path(1))
83
- expectjs("Routes.budgy_path").to eq(nil)
84
- expectjs("Routes.budgie_descendents_path(1)").to eq(test_routes.budgie_descendents_path(1))
85
- end
86
-
87
- describe "url parameters encoding" do
88
-
89
- it "should support route with parameters containing symbols that need URI-encoding", :aggregate_failures do
90
- expectjs("Routes.inbox_path('#hello')").to eq(test_routes.inbox_path('#hello'))
91
- expectjs("Routes.inbox_path('some param')").to eq(test_routes.inbox_path('some param'))
92
- expectjs("Routes.inbox_path('some param with more & more encode symbols')").to eq(test_routes.inbox_path('some param with more & more encode symbols'))
93
- end
94
-
95
- it "should support route with parameters containing symbols not need URI-encoding", :aggregate_failures do
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('.+'))
98
- end
99
-
100
- it "supports emoji characters", :aggregate_failures do
101
- expectjs("Routes.inbox_path('💗')").to eq(test_routes.inbox_path('💗'))
102
- end
103
- end
104
-
105
- describe "when route has defaults" do
106
- it "should support route default format" do
107
- expectjs("Routes.api_purchases_path()").to eq(test_routes.api_purchases_path)
108
- end
109
-
110
- it 'should support route default subdomain' do
111
- expectjs("Routes.backend_root_path()").to eq(test_routes.backend_root_path)
112
- end
113
-
114
- it "should support default format override" do
115
- expectjs("Routes.api_purchases_path({format: 'xml'})").to eq(test_routes.api_purchases_path(format: 'xml'))
116
- end
117
-
118
- it "should support default format override by passing it in args" do
119
- expectjs("Routes.api_purchases_path('xml')").to eq(test_routes.api_purchases_path('xml'))
120
- end
121
-
122
- it "doesn't apply defaults to path" do
123
- expectjs("Routes.with_defaults_path()").to eq(test_routes.with_defaults_path)
124
- expectjs("Routes.with_defaults_path({format: 'json'})").to eq(test_routes.with_defaults_path(format: 'json'))
125
- end
126
- end
127
-
128
- context "with rails engines" do
129
- it "should support simple route" do
130
- expectjs("Routes.blog_app_posts_path()").to eq(blog_routes.posts_path())
131
- end
132
-
133
- it "should support root route" do
134
- expectjs("Routes.blog_app_path()").to eq(test_routes.blog_app_path())
135
- end
136
-
137
- it "should support route with parameters" do
138
- expectjs("Routes.blog_app_post_path(1)").to eq(blog_routes.post_path(1))
139
- end
140
- it "should support root path" do
141
- expectjs("Routes.blog_app_root_path()").to eq(blog_routes.root_path)
142
- end
143
- it "should support single route mapping" do
144
- expectjs("Routes.support_path({page: 3})").to eq(test_routes.support_path(:page => 3))
145
- end
146
-
147
- it 'works' do
148
- expectjs("Routes.planner_manage_path({locale: 'ua'})").to eq(planner_routes.manage_path(locale: 'ua'))
149
- expectjs("Routes.planner_manage_path()").to eq(planner_routes.manage_path)
150
- end
151
- end
152
-
153
- it "shouldn't require the format" do
154
- expectjs("Routes.json_only_path({format: 'json'})").to eq(test_routes.json_only_path(:format => 'json'))
155
- end
156
-
157
- it "should serialize object with empty string value" do
158
- expectjs("Routes.inboxes_path({a: '', b: 1})").to eq(test_routes.inboxes_path(:a => '', :b => 1))
159
- end
160
-
161
- it "should support utf-8 route" do
162
- expectjs("Routes.hello_path()").to eq(test_routes.hello_path)
163
- end
164
-
165
- it "should support root_path" do
166
- expectjs("Routes.root_path()").to eq(test_routes.root_path)
167
- end
168
-
169
- describe "params parameter" do
170
- it "works" do
171
- expectjs("Routes.inboxes_path({params: {key: 'value'}})").to eq(test_routes.inboxes_path(params: {key: 'value'}))
172
- end
173
-
174
- it "allows keyword key as a query parameter" do
175
- expectjs("Routes.inboxes_path({params: {anchor: 'a', params: 'p'}})").to eq(test_routes.inboxes_path(params: {anchor: 'a', params: 'p'}))
176
- end
177
-
178
- it "throws when value is not an object" do
179
- expect {
180
- evaljs("Routes.inboxes_path({params: 1})")
181
- }.to raise_error(js_error_class)
182
- end
183
- end
184
-
185
- describe "get parameters" do
186
- it "should support simple get parameters" do
187
- expectjs("Routes.inbox_path(1, {format: 'json', lang: 'ua', q: 'hello'})").to eq(test_routes.inbox_path(1, :lang => "ua", :q => "hello", :format => "json"))
188
- end
189
-
190
- it "should support array get parameters" do
191
- expectjs("Routes.inbox_path(1, {hello: ['world', 'mars']})").to eq(test_routes.inbox_path(1, :hello => [:world, :mars]))
192
- end
193
-
194
- context "object without prototype" do
195
- before(:each) do
196
- evaljs("let params = Object.create(null); params.q = 'hello';")
197
- evaljs("let inbox = Object.create(null); inbox.to_param = 1;")
198
- end
199
-
200
- it "should still work correctly" do
201
- expectjs("Routes.inbox_path(inbox, params)").to eq(
202
- test_routes.inbox_path(1, q: "hello")
203
- )
204
- end
205
- end
206
-
207
- it "should support nested get parameters" do
208
- expectjs("Routes.inbox_path(1, {format: 'json', env: 'test', search: { category_ids: [2,5], q: 'hello'}})").to eq(
209
- test_routes.inbox_path(1, :env => 'test', :search => {:category_ids => [2,5], :q => "hello"}, :format => "json")
210
- )
211
- end
212
-
213
- it "should support null and undefined parameters" do
214
- expectjs("Routes.inboxes_path({uri: null, key: undefined})").to eq(test_routes.inboxes_path(:uri => nil, :key => nil))
215
- end
216
-
217
- it "should escape get parameters" do
218
- expectjs("Routes.inboxes_path({uri: 'http://example.com'})").to eq(test_routes.inboxes_path(:uri => 'http://example.com'))
219
- end
220
-
221
- it "should support nested object null parameters" do
222
- expectjs("Routes.inboxes_path({hello: {world: null}})").to eq(test_routes.inboxes_path(:hello => {:world => nil}))
223
- end
224
- end
225
-
226
-
227
- context "routes globbing" do
228
- it "should be supported as parameters" do
229
- expectjs("Routes.book_path('thrillers', 1)").to eq(test_routes.book_path('thrillers', 1))
230
- end
231
-
232
- it "should support routes globbing as array" do
233
- expectjs("Routes.book_path(['thrillers'], 1)").to eq(test_routes.book_path(['thrillers'], 1))
234
- end
235
-
236
- it "should support routes globbing as array" do
237
- expectjs("Routes.book_path([1, 2, 3], 1)").to eq(test_routes.book_path([1, 2, 3], 1))
238
- end
239
-
240
- it "should support routes globbing with slash" do
241
- expectjs("Routes.book_path('a_test/b_test/c_test', 1)").to eq(test_routes.book_path('a_test/b_test/c_test', 1))
242
- end
243
-
244
- it "should support routes globbing as hash" do
245
- expectjs("Routes.book_path('a%b', 1)").to eq(test_routes.book_path('a%b', 1))
246
- end
247
-
248
- it "should support routes globbing as array with optional params" do
249
- expectjs("Routes.book_path([1, 2, 3, 5], 1, {c: '1'})").to eq(test_routes.book_path([1, 2, 3, 5], 1, { :c => "1" }))
250
- end
251
-
252
- it "should support routes globbing in book_title route as array" do
253
- expectjs("Routes.book_title_path('john', ['thrillers', 'comedian'])").to eq(test_routes.book_title_path('john', ['thrillers', 'comedian']))
254
- end
255
-
256
- it "should support routes globbing in book_title route as array with optional params" do
257
- 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'}))
258
- end
259
- end
260
-
261
- context "using optional path fragments" do
262
- context "including not optional parts" do
263
- it "should include everything that is not optional" do
264
- expectjs("Routes.foo_path()").to eq(test_routes.foo_path)
265
- end
266
- end
267
-
268
- context "but not including them" do
269
- it "should not include the optional parts" do
270
- expectjs("Routes.things_path()").to eq(test_routes.things_path)
271
- expectjs("Routes.things_path({ q: 'hello' })").to eq(test_routes.things_path(q: 'hello'))
272
- end
273
-
274
- it "treats false as absent optional part" do
275
- if Rails.version < "7.0"
276
- pending("https://github.com/rails/rails/issues/42280")
277
- end
278
- expectjs("Routes.things_path(false)").to eq(test_routes.things_path(false))
279
- end
280
-
281
- it "treats false as absent optional part when default is specified" do
282
- expectjs("Routes.campaigns_path(false)").to eq(test_routes.campaigns_path(false))
283
- end
284
-
285
- it "should not require the optional parts as arguments" do
286
- expectjs("Routes.thing_path(null, 5)").to eq(test_routes.thing_path(nil, 5))
287
- end
288
-
289
- it "should treat undefined as non-given optional part" do
290
- expectjs("Routes.thing_path(5, {optional_id: undefined})").to eq(test_routes.thing_path(5, :optional_id => nil))
291
- end
292
-
293
- it "should raise error when passing non-full list of arguments and some query params" do
294
- expect { evaljs("Routes.thing_path(5, {q: 'hello'})") }
295
- .to raise_error(/Route missing required keys: id/)
296
- end
297
-
298
- it "should treat null as non-given optional part" do
299
- expectjs("Routes.thing_path(5, {optional_id: null})").to eq(test_routes.thing_path(5, :optional_id => nil))
300
- end
301
-
302
- it "should work when passing required params in options" do
303
- expectjs("Routes.thing_deep_path({second_required: 1, third_required: 2})").to eq(test_routes.thing_deep_path(second_required: 1, third_required: 2))
304
- end
305
-
306
- it "should skip leading and trailing optional parts" do
307
- expectjs("Routes.thing_deep_path(1, 2)").to eq(test_routes.thing_deep_path(1, 2))
308
- end
309
- end
310
-
311
- context "and including them" do
312
- it "should fail when insufficient arguments are given" do
313
- expect { evaljs("Routes.thing_deep_path(2)") }.to raise_error(/Route missing required keys: third_required/)
314
- end
315
-
316
- it "should include the optional parts" do
317
- expectjs("Routes.things_path({optional_id: 5})").to eq(test_routes.things_path(:optional_id => 5))
318
- expectjs("Routes.things_path(5)").to eq(test_routes.things_path(5))
319
- 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))
320
- 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))
321
- 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))
322
- 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))
323
- 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))
324
- 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))
325
- expectjs("Routes.thing_deep_path(2, 3)").to eq(test_routes.thing_deep_path(2, 3))
326
- expectjs("Routes.thing_deep_path(1, 2, { third_required: 3 })").to eq(test_routes.thing_deep_path(1, 2, third_required: 3))
327
- 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'}))
328
- 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))
329
- expectjs("Routes.thing_deep_path(1, 3, { second_required: 2 })").to eq(test_routes.thing_deep_path(1, 3, second_required: 2))
330
- 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))
331
- expectjs("Routes.thing_deep_path(2, 3, { first_optional: 1 })").to eq(test_routes.thing_deep_path(2, 3, first_optional: 1))
332
- 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))
333
- 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))
334
- 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))
335
- expectjs("Routes.thing_deep_path(1, 2, 3)").to eq(test_routes.thing_deep_path(1, 2, 3))
336
- expectjs("Routes.thing_deep_path(1, 2, 3, { forth_optional: 4 })").to eq(test_routes.thing_deep_path(1, 2, 3, forth_optional: 4))
337
- expectjs("Routes.thing_deep_path(1, 2, 4, { third_required: 3 })").to eq(test_routes.thing_deep_path(1, 2, 4, third_required: 3))
338
- expectjs("Routes.thing_deep_path(1, 3, 4, { second_required: 2 })").to eq(test_routes.thing_deep_path(1, 3, 4, second_required: 2))
339
- expectjs("Routes.thing_deep_path(2, 3, 4, { first_optional: 1 })").to eq(test_routes.thing_deep_path(2, 3, 4, first_optional: 1))
340
- expectjs("Routes.thing_deep_path(1, 2, 3, 4)").to eq(test_routes.thing_deep_path(1, 2, 3, 4))
341
-
342
- end
343
-
344
- context "on nested optional parts" do
345
- if Rails.version <= "5.0.0"
346
- # this type of routing is deprecated
347
- it "should include everything that is not optional" do
348
- expectjs("Routes.classic_path({controller: 'classic', action: 'edit'})").to eq(test_routes.classic_path(controller: :classic, action: :edit))
349
- end
350
- end
351
- end
352
- end
353
- end
354
-
355
- context "when wrong parameters given" do
356
-
357
- it "should throw Exception if not enough parameters" do
358
- expect {
359
- evaljs("Routes.inbox_path()")
360
- }.to raise_error(js_error_class)
361
- end
362
-
363
- it "should throw Exception if required parameter is not defined" do
364
- expect {
365
- evaljs("Routes.inbox_path(null)")
366
- }.to raise_error(js_error_class)
367
- end
368
-
369
- it "should throw Exception if required parameter is not defined" do
370
- expect {
371
- evaljs("Routes.inbox_path(undefined)")
372
- }.to raise_error(js_error_class)
373
- end
374
-
375
- it "should throw Exceptions if when there is too many parameters" do
376
- expect {
377
- evaljs("Routes.inbox_path(1,2,3)")
378
- }.to raise_error(js_error_class)
379
- end
380
- end
381
-
382
- context "when javascript engine without Array#indexOf is used" do
383
- before(:each) do
384
- evaljs("Array.prototype.indexOf = null")
385
- end
386
- it "should still work correctly" do
387
- expectjs("Routes.inboxes_path()").to eq(test_routes.inboxes_path())
388
- end
389
- end
390
-
391
- context "when arguments are objects" do
392
-
393
- let(:klass) { Struct.new(:id, :to_param) }
394
- let(:inbox) { klass.new(1,"my") }
395
-
396
- it "should throw Exceptions if when pass id with null" do
397
- expect {
398
- evaljs("Routes.inbox_path({id: null})")
399
- }.to raise_error(js_error_class)
400
- end
401
-
402
- it "should throw Exceptions if when pass to_param with null" do
403
- expect {
404
- evaljs("Routes.inbox_path({to_param: null})")
405
- }.to raise_error(js_error_class)
406
- end
407
- it "should support 0 as a to_param option" do
408
- expectjs("Routes.inbox_path({to_param: 0})").to eq(test_routes.inbox_path(Struct.new(:to_param).new('0')))
409
- end
410
-
411
- it "should check for options special key" do
412
- expectjs("Routes.inbox_path({id: 7, q: 'hello', _options: true})").to eq(test_routes.inbox_path(id: 7, q: 'hello'))
413
- expect {
414
- evaljs("Routes.inbox_path({to_param: 7, _options: true})")
415
- }.to raise_error(js_error_class)
416
- expectjs("Routes.inbox_message_path(5, {id: 7, q: 'hello', _options: true})").to eq(test_routes.inbox_message_path(5, id: 7, q: 'hello'))
417
- end
418
-
419
- it "should support 0 as an id option" do
420
- expectjs("Routes.inbox_path({id: 0})").to eq(test_routes.inbox_path(0))
421
- end
422
-
423
- it "should use id property of the object in path" do
424
- expectjs("Routes.inbox_path({id: 1})").to eq(test_routes.inbox_path(1))
425
- end
426
-
427
- it "should prefer to_param property over id property" do
428
- expectjs("Routes.inbox_path({id: 1, to_param: 'my'})").to eq(test_routes.inbox_path(inbox))
429
- end
430
-
431
- it "should call to_param if it is a function" do
432
- expectjs("Routes.inbox_path({id: 1, to_param: function(){ return 'my';}})").to eq(test_routes.inbox_path(inbox))
433
- end
434
-
435
- it "should call id if it is a function" do
436
- expectjs("Routes.inbox_path({id: function() { return 1;}})").to eq(test_routes.inbox_path(1))
437
- end
438
-
439
- it "should support options argument" do
440
- expectjs(
441
- "Routes.inbox_message_path({id:1, to_param: 'my'}, {id:2}, {custom: true, format: 'json'})"
442
- ).to eq(test_routes.inbox_message_path(inbox, 2, :custom => true, :format => "json"))
443
- end
444
-
445
- it "supports camel case property name" do
446
- expectjs("Routes.inbox_path({id: 1, toParam: 'my'})").to eq(test_routes.inbox_path(inbox))
447
- end
448
-
449
- it "supports camel case method name" do
450
- expectjs("Routes.inbox_path({id: 1, toParam: function(){ return 'my';}})").to eq(test_routes.inbox_path(inbox))
451
- end
452
-
453
- context "when globbing" do
454
- it "should prefer to_param property over id property" do
455
- expectjs("Routes.book_path({id: 1, to_param: 'my'}, 1)").to eq(test_routes.book_path(inbox, 1))
456
- end
457
-
458
- it "should call to_param if it is a function" do
459
- expectjs("Routes.book_path({id: 1, to_param: function(){ return 'my';}}, 1)").to eq(test_routes.book_path(inbox, 1))
460
- end
461
-
462
- it "should call id if it is a function" do
463
- expectjs("Routes.book_path({id: function() { return 'technical';}}, 1)").to eq(test_routes.book_path('technical', 1))
464
- end
465
-
466
- it "should support options argument" do
467
- expectjs(
468
- "Routes.book_path({id:1, to_param: 'my'}, {id:2}, {custom: true, format: 'json'})"
469
- ).to eq(test_routes.book_path(inbox, 2, :custom => true, :format => "json"))
470
- end
471
- end
472
- end
473
- end
@@ -1,37 +0,0 @@
1
-
2
- require "spec_helper"
3
-
4
- describe JsRoutes, "compatibility with Rails" do
5
-
6
- before(:each) do
7
- evallib(module_type: nil, namespace: 'Routes')
8
- end
9
-
10
- context "when specs" do
11
- it "should show inbox spec" do
12
- expectjs("Routes.inbox_path.toString()").to eq('/inboxes/:id(.:format)')
13
- end
14
-
15
- it "should show inbox spec convert to string" do
16
- expectjs("'' + Routes.inbox_path").to eq('/inboxes/:id(.:format)')
17
- end
18
-
19
- it "should show inbox message spec" do
20
- expectjs("Routes.inbox_message_path.toString()").to eq('/inboxes/:inbox_id/messages/:id(.:format)')
21
- end
22
-
23
- it "should show inbox message spec convert to string" do
24
- expectjs("'' + Routes.inbox_message_path").to eq('/inboxes/:inbox_id/messages/:id(.:format)')
25
- end
26
- end
27
-
28
- describe "requiredParams" do
29
- it "should show inbox spec" do
30
- expect(evaljs("Routes.inbox_path.requiredParams()").to_a).to eq(["id"])
31
- end
32
-
33
- it "should show inbox message spec" do
34
- expect(evaljs("Routes.inbox_message_path.requiredParams()").to_a).to eq(["inbox_id", "id"])
35
- end
36
- end
37
- end
@@ -1,152 +0,0 @@
1
- # we need to run post_rails_init_spec as the latest
2
- # because it cause unrevertable changes to runtime
3
- # what is why I added "zzz_last" in the beginning
4
-
5
- require "sprockets"
6
- require "sprockets/railtie"
7
- require 'spec_helper'
8
- require "fileutils"
9
-
10
- describe "after Rails initialization", :slow do
11
- NAME = Rails.root.join('app', 'assets', 'javascripts', 'routes.js').to_s
12
-
13
- def sprockets_v3?
14
- Sprockets::VERSION.to_i >= 3
15
- end
16
-
17
- def sprockets_v4?
18
- Sprockets::VERSION.to_i >= 4
19
- end
20
-
21
- def sprockets_context(environment, name, filename)
22
- if sprockets_v3?
23
- Sprockets::Context.new(environment: environment, name: name, filename: filename.to_s, metadata: {})
24
- else
25
- Sprockets::Context.new(environment, name, filename)
26
- end
27
- end
28
-
29
- def evaluate(ctx, file)
30
- if sprockets_v3?
31
- ctx.load(ctx.environment.find_asset(file, pipeline: :default).uri).to_s
32
- else
33
- ctx.evaluate(file)
34
- end
35
- end
36
-
37
- before(:each) do
38
- FileUtils.rm_rf Rails.root.join('tmp/cache')
39
- FileUtils.rm_f NAME
40
- JsRoutes.generate!(NAME)
41
- end
42
-
43
- before(:all) do
44
- JsRoutes::Engine.install_sprockets!
45
- Rails.configuration.eager_load = false
46
- Rails.application.initialize!
47
- end
48
-
49
- it "should generate routes file" do
50
- expect(File.exist?(NAME)).to be_truthy
51
- end
52
-
53
- it "should not rewrite routes file if nothing changed" do
54
- routes_file_mtime = File.mtime(NAME)
55
- JsRoutes.generate!(NAME)
56
- expect(File.mtime(NAME)).to eq(routes_file_mtime)
57
- end
58
-
59
- it "should rewrite routes file if file content changed" do
60
- # Change content of existed routes file (add space to the end of file).
61
- File.open(NAME, 'a') { |f| f << ' ' }
62
- routes_file_mtime = File.mtime(NAME)
63
- sleep(1)
64
- JsRoutes.generate!(NAME)
65
- expect(File.mtime(NAME)).not_to eq(routes_file_mtime)
66
- end
67
-
68
- context "JsRoutes::Engine" do
69
- TEST_ASSET_PATH = Rails.root.join('app','assets','javascripts','test.js')
70
-
71
- before(:all) do
72
- File.open(TEST_ASSET_PATH,'w') do |f|
73
- f.puts "function() {}"
74
- end
75
- end
76
- after(:all) do
77
- FileUtils.rm_f(TEST_ASSET_PATH)
78
- end
79
-
80
- context "the preprocessor" do
81
- before(:each) do
82
- path = Rails.root.join('config','routes.rb').to_s
83
- if sprockets_v3? || sprockets_v4?
84
- expect_any_instance_of(Sprockets::Context).to receive(:depend_on).with(path)
85
- else
86
- expect(ctx).to receive(:depend_on).with(path)
87
- end
88
- end
89
- let!(:ctx) do
90
- sprockets_context(Rails.application.assets,
91
- 'js-routes.js',
92
- Pathname.new('js-routes.js'))
93
- end
94
-
95
- context "when dealing with js-routes.js" do
96
- context "with Rails" do
97
- context "and initialize on precompile" do
98
- before(:each) do
99
- Rails.application.config.assets.initialize_on_precompile = true
100
- end
101
- it "should render some javascript" do
102
- expect(evaluate(ctx, 'js-routes.js')).to match(/Utils\.define_module/)
103
- end
104
- end
105
- context "and not initialize on precompile" do
106
- before(:each) do
107
- Rails.application.config.assets.initialize_on_precompile = false
108
- end
109
- it "should raise an exception if 3 version" do
110
- expect(evaluate(ctx, 'js-routes.js')).to match(/Utils\.define_module/)
111
- end
112
- end
113
-
114
- end
115
- end
116
-
117
-
118
- end
119
- context "when not dealing with js-routes.js" do
120
- it "should not depend on routes.rb" do
121
- ctx = sprockets_context(Rails.application.assets,
122
- 'test.js',
123
- TEST_ASSET_PATH)
124
- expect(ctx).not_to receive(:depend_on)
125
- evaluate(ctx, 'test.js')
126
- end
127
- end
128
- end
129
- end
130
-
131
- describe "JSRoutes thread safety", :slow do
132
- before do
133
- begin
134
- Rails.application.initialize!
135
- rescue
136
- end
137
- end
138
-
139
- it "can produce the routes from multiple threads" do
140
- threads = 2.times.map do
141
- Thread.start do
142
- 10.times {
143
- expect { JsRoutes.generate }.to_not raise_error
144
- }
145
- end
146
- end
147
-
148
- threads.each do |thread|
149
- thread.join
150
- end
151
- end
152
- end