js-routes 2.2.8 → 2.2.10

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.
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,508 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe JsRoutes, "options" do
4
-
5
- let(:generated_js) do
6
- JsRoutes.generate(
7
- module_type: nil,
8
- namespace: 'Routes',
9
- **_options
10
- )
11
- end
12
-
13
- before(:each) do
14
- evaljs(_presetup) if _presetup
15
- with_warnings(_warnings) do
16
- evaljs(generated_js)
17
- App.routes.default_url_options = _options[:default_url_options] || {}
18
- end
19
- end
20
-
21
- after(:each) do
22
- App.routes.default_url_options = {}
23
- end
24
-
25
- let(:_presetup) { nil }
26
- let(:_options) { {} }
27
- let(:_warnings) { true }
28
-
29
- describe "serializer" do
30
- context "when specified" do
31
- # define custom serializer
32
- # this is a nonsense serializer, which always returns foo=bar
33
- # for all inputs
34
- let(:_presetup){ %q(function myCustomSerializer(object, prefix) { return "foo=bar"; }) }
35
- let(:_options) { {:serializer => "myCustomSerializer"} }
36
-
37
- it "should set configurable serializer" do
38
- # expect the nonsense serializer above to have appened foo=bar
39
- # to the end of the path
40
- expectjs(%q(Routes.inboxes_path())).to eql("/inboxes?foo=bar")
41
- end
42
- end
43
-
44
- context "when specified, but not function" do
45
- let(:_presetup){ %q(var myCustomSerializer = 1) }
46
- let(:_options) { {:serializer => "myCustomSerializer"} }
47
-
48
- it "should throw error" do
49
- expect {
50
- evaljs(%q(Routes.inboxes_path({a: 1})))
51
- }.to raise_error(js_error_class)
52
- end
53
- end
54
-
55
- context "when configured in js" do
56
- let(:_options) { {:serializer =>%q(function (object, prefix) { return "foo=bar"; })} }
57
-
58
- it "uses JS serializer" do
59
- evaljs("Routes.configure({serializer: function (object, prefix) { return 'bar=baz'; }})")
60
- expectjs(%q(Routes.inboxes_path({a: 1}))).to eql("/inboxes?bar=baz")
61
- end
62
- end
63
- end
64
-
65
- context "when exclude is specified" do
66
-
67
- let(:_options) { {:exclude => /^admin_/} }
68
-
69
- it "should exclude specified routes from file" do
70
- expectjs("Routes.admin_users_path").to be_nil
71
- end
72
-
73
- it "should not exclude routes not under specified pattern" do
74
- expectjs("Routes.inboxes_path()").not_to be_nil
75
- end
76
-
77
- context "for rails engine" do
78
- let(:_options) { {:exclude => /^blog_app_posts/} }
79
-
80
- it "should exclude specified engine route" do
81
- expectjs("Routes.blog_app_posts_path").to be_nil
82
- end
83
- end
84
- end
85
-
86
- context "when include is specified" do
87
-
88
- let(:_options) { {:include => /^admin_/} }
89
-
90
- it "should exclude specified routes from file" do
91
- expectjs("Routes.admin_users_path()").not_to be_nil
92
- end
93
-
94
- it "should not exclude routes not under specified pattern" do
95
- expectjs("Routes.inboxes_path").to be_nil
96
- end
97
-
98
- context "with camel_case option" do
99
- let(:_options) { {include: /^admin_/, camel_case: true} }
100
-
101
- it "should exclude specified routes from file" do
102
- expectjs("Routes.adminUsersPath()").not_to be_nil
103
- end
104
-
105
- it "should not exclude routes not under specified pattern" do
106
- expectjs("Routes.inboxesPath").to be_nil
107
- end
108
- end
109
-
110
- context "for rails engine" do
111
- let(:_options) { {:include => /^blog_app_posts/} }
112
-
113
- it "should include specified engine route" do
114
- expectjs("Routes.blog_app_posts_path()").not_to be_nil
115
- end
116
- end
117
- end
118
-
119
- context "when prefix with trailing slash is specified" do
120
-
121
- let(:_options) { {:prefix => "/myprefix/" } }
122
-
123
- it "should render routing with prefix" do
124
- expectjs("Routes.inbox_path(1)").to eq("/myprefix#{test_routes.inbox_path(1)}")
125
- end
126
-
127
- it "should render routing with prefix set in JavaScript" do
128
- evaljs("Routes.configure({prefix: '/newprefix/'})")
129
- expectjs("Routes.config().prefix").to eq("/newprefix/")
130
- expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}")
131
- end
132
-
133
- end
134
-
135
- context "when prefix with http:// is specified" do
136
-
137
- let(:_options) { {:prefix => "http://localhost:3000" } }
138
-
139
- it "should render routing with prefix" do
140
- expectjs("Routes.inbox_path(1)").to eq(_options[:prefix] + test_routes.inbox_path(1))
141
- end
142
- end
143
-
144
- context "when prefix without trailing slash is specified" do
145
-
146
- let(:_options) { {:prefix => "/myprefix" } }
147
-
148
- it "should render routing with prefix" do
149
- expectjs("Routes.inbox_path(1)").to eq("/myprefix#{test_routes.inbox_path(1)}")
150
- end
151
-
152
- it "should render routing with prefix set in JavaScript" do
153
- evaljs("Routes.configure({prefix: '/newprefix/'})")
154
- expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}")
155
- end
156
-
157
- end
158
-
159
- context "when default format is specified" do
160
- let(:_options) { {:default_url_options => {format: "json"}} }
161
- let(:_warnings) { nil }
162
-
163
- if Rails.version >= "5"
164
- it "should render routing with default_format" do
165
- expectjs("Routes.inbox_path(1)").to eq(test_routes.inbox_path(1))
166
- end
167
-
168
- it "should render routing with default_format and zero object" do
169
- expectjs("Routes.inbox_path(0)").to eq(test_routes.inbox_path(0))
170
- end
171
- end
172
-
173
- it "should override default_format when spefified implicitly" do
174
- expectjs("Routes.inbox_path(1, {format: 'xml'})").to eq(test_routes.inbox_path(1, :format => "xml"))
175
- end
176
-
177
- it "should override nullify implicitly when specified implicitly" do
178
- expectjs("Routes.inbox_path(1, {format: null})").to eq(test_routes.inbox_path(1, format: nil))
179
- end
180
-
181
- it "shouldn't require the format" do
182
- expectjs("Routes.json_only_path()").to eq(test_routes.json_only_path)
183
- end
184
- end
185
-
186
- it "shouldn't include the format when {:format => false} is specified" do
187
- expectjs("Routes.no_format_path()").to eq(test_routes.no_format_path())
188
- expectjs("Routes.no_format_path({format: 'json'})").to eq(test_routes.no_format_path(format: 'json'))
189
- end
190
-
191
- describe "default_url_options" do
192
- context "with optional route parts" do
193
- context "provided by the default_url_options" do
194
- let(:_options) { { :default_url_options => { :optional_id => "12", :format => "json" } } }
195
- it "should use this options to fill optional parameters" do
196
- expectjs("Routes.things_path()").to eq(test_routes.things_path(12))
197
- end
198
- end
199
-
200
- context "provided inline by the method parameters" do
201
- let(:options) { { :default_url_options => { :optional_id => "12" } } }
202
- it "should overwrite the default_url_options" do
203
- expectjs("Routes.things_path({ optional_id: 34 })").to eq(test_routes.things_path(optional_id: 34))
204
- end
205
- end
206
-
207
- context "not provided" do
208
- let(:_options) { { :default_url_options => { :format => "json" } } }
209
- it "breaks" do
210
- expectjs("Routes.foo_all_path()").to eq(test_routes.foo_all_path)
211
- end
212
- end
213
- end
214
-
215
- context "with required route parts" do
216
- let(:_options) { { :default_url_options => { :inbox_id => "12" } } }
217
- it "should use this options to fill optional parameters" do
218
- expectjs("Routes.inbox_messages_path()").to eq(test_routes.inbox_messages_path)
219
- end
220
- end
221
-
222
- context "with optional and required route parts" do
223
- let(:_options) { {:default_url_options => { :optional_id => "12" } } }
224
- it "should use this options to fill the optional parameters" do
225
- expectjs("Routes.thing_path(1)").to eq test_routes.thing_path(1, { optional_id: "12" })
226
- end
227
-
228
- context "when passing options that do not have defaults" do
229
- it "should use this options to fill the optional parameters" do
230
- expectjs("Routes.thing_path(1, { format: 'json' })").to eq test_routes.thing_path(1, { optional_id: "12", format: "json" } ) # test_routes.thing_path needs optional_id here to generate the correct route. Not sure why.
231
- end
232
- end
233
- end
234
-
235
- context "when overwritten on JS level" do
236
- let(:_options) { { :default_url_options => { :format => "json" } } }
237
- it "uses JS defined value" do
238
- evaljs("Routes.configure({default_url_options: {format: 'xml'}})")
239
- expectjs("Routes.inboxes_path()").to eq(test_routes.inboxes_path(format: 'xml'))
240
- end
241
- end
242
- end
243
-
244
- describe "trailing_slash" do
245
- context "with default option" do
246
- let(:_options) { Hash.new }
247
- it "should working in params" do
248
- expectjs("Routes.inbox_path(1, {trailing_slash: true})").to eq(test_routes.inbox_path(1, :trailing_slash => true))
249
- end
250
-
251
- it "should working with additional params" do
252
- expectjs("Routes.inbox_path(1, {trailing_slash: true, test: 'params'})").to eq(test_routes.inbox_path(1, :trailing_slash => true, :test => 'params'))
253
- end
254
- end
255
-
256
- context "with default_url_options option" do
257
- let(:_options) { {:default_url_options => {:trailing_slash => true}} }
258
- it "should working" do
259
- expectjs("Routes.inbox_path(1, {test: 'params'})").to eq(test_routes.inbox_path(1, :trailing_slash => true, :test => 'params'))
260
- end
261
-
262
- it "should remove it by params" do
263
- expectjs("Routes.inbox_path(1, {trailing_slash: false})").to eq(test_routes.inbox_path(1, trailing_slash: false))
264
- end
265
- end
266
-
267
- context "with disabled default_url_options option" do
268
- let(:_options) { {:default_url_options => {:trailing_slash => false}} }
269
- it "should not use trailing_slash" do
270
- expectjs("Routes.inbox_path(1, {test: 'params'})").to eq(test_routes.inbox_path(1, :test => 'params'))
271
- end
272
-
273
- it "should use it by params" do
274
- expectjs("Routes.inbox_path(1, {trailing_slash: true})").to eq(test_routes.inbox_path(1, :trailing_slash => true))
275
- end
276
- end
277
- end
278
-
279
- describe "camel_case" do
280
- context "with default option" do
281
- let(:_options) { Hash.new }
282
- it "should use snake case routes" do
283
- expectjs("Routes.inbox_path(1)").to eq(test_routes.inbox_path(1))
284
- expectjs("Routes.inboxPath").to be_nil
285
- end
286
- end
287
-
288
- context "with true" do
289
- let(:_options) { { :camel_case => true } }
290
- it "should generate camel case routes" do
291
- expectjs("Routes.inbox_path").to be_nil
292
- expectjs("Routes.inboxPath").not_to be_nil
293
- expectjs("Routes.inboxPath(1)").to eq(test_routes.inbox_path(1))
294
- expectjs("Routes.inboxMessagesPath(10)").to eq(test_routes.inbox_messages_path(:inbox_id => 10))
295
- end
296
- end
297
- end
298
-
299
- describe "url_links" do
300
- context "with default option" do
301
- let(:_options) { Hash.new }
302
- it "should generate only path links" do
303
- expectjs("Routes.inbox_path(1)").to eq(test_routes.inbox_path(1))
304
- expectjs("Routes.inbox_url").to be_nil
305
- end
306
- end
307
-
308
- context "when configuring with default_url_options" do
309
- context "when only host option is specified" do
310
- let(:_options) { { :url_links => true, :default_url_options => {:host => "example.com"} } }
311
-
312
- it "uses the specified host, defaults protocol to http, defaults port to 80 (leaving it blank)" do
313
- expectjs("Routes.inbox_url(1)").to eq("http://example.com#{test_routes.inbox_path(1)}")
314
- end
315
-
316
- it "does not override protocol when specified in route" do
317
- expectjs("Routes.new_session_url()").to eq("https://example.com#{test_routes.new_session_path}")
318
- end
319
-
320
- it "does not override host when specified in route" do
321
- expectjs("Routes.sso_url()").to eq(test_routes.sso_url)
322
- end
323
-
324
- it "does not override port when specified in route" do
325
- expectjs("Routes.portals_url()").to eq("http://example.com:8080#{test_routes.portals_path}")
326
- end
327
- end
328
-
329
- context "when default host and protocol are specified" do
330
- let(:_options) { { :url_links => true, :default_url_options => {:host => "example.com", :protocol => "ftp"} } }
331
-
332
- it "uses the specified protocol and host, defaults port to 80 (leaving it blank)" do
333
- expectjs("Routes.inbox_url(1)").to eq("ftp://example.com#{test_routes.inbox_path(1)}")
334
- end
335
-
336
- it "does not override protocol when specified in route" do
337
- expectjs("Routes.new_session_url()").to eq("https://example.com#{test_routes.new_session_path}")
338
- end
339
-
340
- it "does not override host when host is specified in route" do
341
- expectjs("Routes.sso_url()").to eq("ftp://sso.example.com#{test_routes.sso_path}")
342
- end
343
-
344
- it "does not override port when specified in route" do
345
- expectjs("Routes.portals_url()").to eq("ftp://example.com:8080#{test_routes.portals_path}")
346
- end
347
- end
348
-
349
- context "when default host and port are specified" do
350
- let(:_options) { { :url_links => true, :default_url_options => {:host => "example.com", :port => 3000} } }
351
-
352
- it "uses the specified host and port, defaults protocol to http" do
353
- expectjs("Routes.inbox_url(1)").to eq("http://example.com:3000#{test_routes.inbox_path(1)}")
354
- end
355
-
356
- it "does not override protocol when specified in route" do
357
- expectjs("Routes.new_session_url()").to eq("https://example.com:3000#{test_routes.new_session_path}")
358
- end
359
-
360
- it "does not override host, protocol, or port when host is specified in route" do
361
- expectjs("Routes.sso_url()").to eq("http://sso.example.com:3000" + test_routes.sso_path)
362
- end
363
-
364
- it "does not override parts when specified in route" do
365
- expectjs("Routes.secret_root_url()").to eq(test_routes.secret_root_url)
366
- end
367
- end
368
-
369
- context "with camel_case option" do
370
- let(:_options) { { :camel_case => true, :url_links => true, :default_url_options => {:host => "example.com"} } }
371
- it "should generate path and url links" do
372
- expectjs("Routes.inboxUrl(1)").to eq("http://example.com#{test_routes.inbox_path(1)}")
373
- end
374
- end
375
-
376
- context "with prefix option" do
377
- let(:_options) { { :prefix => "/api", :url_links => true, :default_url_options => {:host => 'example.com'} } }
378
- it "should generate path and url links" do
379
- expectjs("Routes.inbox_url(1)").to eq("http://example.com/api#{test_routes.inbox_path(1)}")
380
- end
381
- end
382
-
383
- context "with compact option" do
384
- let(:_options) { { :compact => true, :url_links => true, :default_url_options => {:host => 'example.com'} } }
385
- it "does not affect url helpers" do
386
- expectjs("Routes.inbox_url(1)").to eq("http://example.com#{test_routes.inbox_path(1)}")
387
- end
388
- end
389
- end
390
-
391
- context 'when window.location is present' do
392
- let(:current_protocol) { 'http:' } # window.location.protocol includes the colon character
393
- let(:current_hostname) { 'current.example.com' }
394
- let(:current_port){ '' } # an empty string means port 80
395
- let(:current_host) do
396
- host = "#{current_hostname}"
397
- host += ":#{current_port}" unless current_port == ''
398
- host
399
- end
400
-
401
- let(:_presetup) do
402
- location = {
403
- protocol: current_protocol,
404
- hostname: current_hostname,
405
- port: current_port,
406
- host: current_host,
407
- }
408
- [
409
- "const window = this;",
410
- "window.location = #{ActiveSupport::JSON.encode(location)};",
411
- ].join("\n")
412
- end
413
-
414
- context "without specifying a default host" do
415
- let(:_options) { { :url_links => true } }
416
-
417
- it "uses the current host" do
418
- expectjs("Routes.inbox_path").not_to be_nil
419
- expectjs("Routes.inbox_url").not_to be_nil
420
- expectjs("Routes.inbox_url(1)").to eq("http://current.example.com#{test_routes.inbox_path(1)}")
421
- expectjs("Routes.inbox_url(1, { test_key: \"test_val\" })").to eq("http://current.example.com#{test_routes.inbox_path(1, :test_key => "test_val")}")
422
- expectjs("Routes.new_session_url()").to eq("https://current.example.com#{test_routes.new_session_path}")
423
- end
424
-
425
- it "doesn't use current when specified in the route" do
426
- expectjs("Routes.sso_url()").to eq(test_routes.sso_url)
427
- end
428
-
429
- it "uses host option as an argument" do
430
- expectjs("Routes.secret_root_url({host: 'another.com'})").to eq(test_routes.secret_root_url(host: 'another.com'))
431
- end
432
-
433
- it "uses port option as an argument" do
434
- expectjs("Routes.secret_root_url({host: 'localhost', port: 8080})").to eq(test_routes.secret_root_url(host: 'localhost', port: 8080))
435
- end
436
-
437
- it "uses protocol option as an argument" do
438
- expectjs("Routes.secret_root_url({host: 'localhost', protocol: 'https'})").to eq(test_routes.secret_root_url(protocol: 'https', host: 'localhost'))
439
- end
440
-
441
- it "uses subdomain option as an argument" do
442
- expectjs("Routes.secret_root_url({subdomain: 'custom'})").to eq(test_routes.secret_root_url(subdomain: 'custom'))
443
- end
444
- end
445
- end
446
-
447
- context 'when window.location is not present' do
448
- context 'without specifying a default host' do
449
- let(:_options) { { url_links: true } }
450
-
451
- it 'generates path' do
452
- expectjs("Routes.inbox_url(1)").to eq test_routes.inbox_path(1)
453
- expectjs("Routes.new_session_url()").to eq test_routes.new_session_path
454
- end
455
- end
456
- end
457
- end
458
-
459
- describe "when the compact mode is enabled" do
460
- let(:_options) { { :compact => true } }
461
- it "removes _path suffix from path helpers" do
462
- expectjs("Routes.inbox_path").to be_nil
463
- expectjs("Routes.inboxes()").to eq(test_routes.inboxes_path())
464
- expectjs("Routes.inbox(2)").to eq(test_routes.inbox_path(2))
465
- end
466
-
467
- context "with url_links option" do
468
- around(:each) do |example|
469
- ActiveSupport::Deprecation.silence do
470
- example.run
471
- end
472
- end
473
-
474
- let(:_options) { { :compact => true, :url_links => true, default_url_options: {host: 'localhost'} } }
475
- it "should not strip urls" do
476
- expectjs("Routes.inbox(1)").to eq(test_routes.inbox_path(1))
477
- expectjs("Routes.inbox_url(1)").to eq("http://localhost#{test_routes.inbox_path(1)}")
478
- end
479
- end
480
- end
481
-
482
- describe "special_options_key" do
483
- let(:_options) { { special_options_key: :__options__ } }
484
- it "can be redefined" do
485
- expect {
486
- expectjs("Routes.inbox_message_path({inbox_id: 1, id: 2, _options: true})").to eq("")
487
- }.to raise_error(js_error_class)
488
- expectjs("Routes.inbox_message_path({inbox_id: 1, id: 2, __options__: true})").to eq(test_routes.inbox_message_path(inbox_id: 1, id: 2))
489
- end
490
- end
491
-
492
- describe "when application is specified" do
493
- let(:_options) { {:application => BlogEngine::Engine} }
494
-
495
- it "should include specified engine route" do
496
- expectjs("Routes.posts_path()").not_to be_nil
497
- end
498
- end
499
-
500
- describe "documentation option" do
501
- let(:_options) { {documentation: false} }
502
-
503
- it "disables documentation generation" do
504
- expect(generated_js).not_to include("@param")
505
- expect(generated_js).not_to include("@returns")
506
- end
507
- end
508
- end