js-routes 2.0.8 → 2.2.0

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.
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe JsRoutes, "compatibility with NIL (legacy browser)" do
4
+ let(:generated_js) do
5
+ JsRoutes.generate(
6
+ module_type: nil,
7
+ include: /book|inboxes|inbox_message/,
8
+ **_options
9
+ )
10
+ end
11
+
12
+ let(:_options) { {} }
13
+ describe "generated js" do
14
+ subject do
15
+ generated_js
16
+ end
17
+
18
+ it "should call route function for each route" do
19
+ is_expected.to include("inboxes_path: __jsr.r(")
20
+ end
21
+ it "should have correct function without arguments signature" do
22
+ is_expected.to include('inboxes_path: __jsr.r({"format":{}}')
23
+ end
24
+ it "should have correct function with arguments signature" do
25
+ is_expected.to include('inbox_message_path: __jsr.r({"inbox_id":{"r":true},"id":{"r":true},"format":{}}')
26
+ end
27
+ it "should have correct function signature with unordered hash" do
28
+ is_expected.to include('inbox_message_attachment_path: __jsr.r({"inbox_id":{"r":true},"message_id":{"r":true},"id":{"r":true},"format":{}}')
29
+ end
30
+ end
31
+
32
+ describe "inline generation" do
33
+ let(:_options) { {namespace: nil} }
34
+ before do
35
+ evaljs("const r = #{generated_js}")
36
+ end
37
+
38
+ it "should be possible" do
39
+ expect(evaljs("r.inboxes_path()")).to eq(test_routes.inboxes_path())
40
+ end
41
+ end
42
+
43
+ describe "namespace option" do
44
+ let(:_options) { {namespace: "PHM"} }
45
+ let(:_presetup) { "" }
46
+ before do
47
+ evaljs("var window = this;")
48
+ evaljs(_presetup)
49
+ evaljs(generated_js)
50
+ end
51
+ it "should use this namespace for routing" do
52
+ expect(evaljs("window.Routes")).to be_nil
53
+ expect(evaljs("window.PHM.inboxes_path")).not_to be_nil
54
+ end
55
+
56
+ describe "is nested" do
57
+ context "and defined on client" do
58
+ let(:_presetup) { "window.PHM = {}" }
59
+ let(:_options) { {namespace: "PHM.Routes"} }
60
+
61
+ it "should use this namespace for routing" do
62
+ expect(evaljs("PHM.Routes.inboxes_path")).not_to be_nil
63
+ end
64
+ end
65
+
66
+ context "but undefined on client" do
67
+ let(:_options) { {namespace: "PHM.Routes"} }
68
+
69
+ it "should initialize namespace" do
70
+ expect(evaljs("window.PHM.Routes.inboxes_path")).not_to be_nil
71
+ end
72
+ end
73
+
74
+ context "and some parts are defined" do
75
+ let(:_presetup) { "window.PHM = { Utils: {} };" }
76
+ let(:_options) { {namespace: "PHM.Routes"} }
77
+
78
+ it "should not overwrite existing parts" do
79
+ expect(evaljs("window.PHM.Utils")).not_to be_nil
80
+ expect(evaljs("window.PHM.Routes.inboxes_path")).not_to be_nil
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+
@@ -2,7 +2,7 @@ require "active_support/core_ext/string/strip"
2
2
  require "fileutils"
3
3
  require 'spec_helper'
4
4
 
5
- describe JsRoutes do
5
+ describe JsRoutes, "compatibility with UMD" do
6
6
  describe "generated js" do
7
7
  subject do
8
8
  JsRoutes.generate(
@@ -188,55 +188,19 @@ describe JsRoutes, "options" do
188
188
  expect(evaljs("Routes.no_format_path({format: 'json'})")).to eq(test_routes.no_format_path(format: 'json'))
189
189
  end
190
190
 
191
- describe "namespace option" do
192
- let(:_options) { {:namespace => "PHM"} }
193
- it "should use this namespace for routing" do
194
- expect(evaljs("window.Routes")).to be_nil
195
- expect(evaljs("PHM.inbox_path")).not_to be_nil
196
- end
197
-
198
- context "is nil" do
199
- let(:_options) { {:namespace => nil, include: /^inbox$/} }
200
- it "should use this namespace for routing" do
201
- evaljs("window.zz = #{generated_js}")
202
- expect(evaljs("window.zz.inbox_path")).not_to be_nil
203
- end
204
-
205
- end
206
-
207
- describe "is nested" do
208
- context "and defined on client" do
209
- let(:_presetup) { "window.PHM = {}" }
210
- let(:_options) { {:namespace => "PHM.Routes"} }
211
- it "should use this namespace for routing" do
212
- expect(evaljs("PHM.Routes.inbox_path")).not_to be_nil
213
- end
214
- end
215
-
216
- context "but undefined on client" do
217
- let(:_options) { {:namespace => "PHM.Routes"} }
218
- it "should initialize namespace" do
219
- expect(evaljs("window.PHM.Routes.inbox_path")).not_to be_nil
220
- end
221
- end
222
-
223
- context "and some parts are defined" do
224
- let(:_presetup) { "window.PHM = { Utils: {} };" }
225
- let(:_options) { {:namespace => "PHM.Routes"} }
226
- it "should not overwrite existing parts" do
227
- expect(evaljs("window.PHM.Utils")).not_to be_nil
228
- expect(evaljs("window.PHM.Routes.inbox_path")).not_to be_nil
229
- end
230
- end
231
- end
232
- end
233
-
234
191
  describe "default_url_options" do
235
192
  context "with optional route parts" do
236
- context "provided" do
193
+ context "provided by the default_url_options" do
237
194
  let(:_options) { { :default_url_options => { :optional_id => "12", :format => "json" } } }
238
- it "should use this opions to fill optional parameters" do
239
- expect(evaljs("Routes.things_path()")).to eq(test_routes.things_path)
195
+ it "should use this options to fill optional parameters" do
196
+ expect(evaljs("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
+ expect(evaljs("Routes.things_path({ optional_id: 34 })")).to eq(test_routes.things_path(optional_id: 34))
240
204
  end
241
205
  end
242
206
 
@@ -249,12 +213,25 @@ describe JsRoutes, "options" do
249
213
  end
250
214
 
251
215
  context "with required route parts" do
252
- let(:_options) { {:default_url_options => {:inbox_id => "12"}} }
253
- it "should use this opions to fill optional parameters" do
216
+ let(:_options) { { :default_url_options => { :inbox_id => "12" } } }
217
+ it "should use this options to fill optional parameters" do
254
218
  expect(evaljs("Routes.inbox_messages_path()")).to eq(test_routes.inbox_messages_path)
255
219
  end
256
220
  end
257
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
+ expect(evaljs("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
+ expect(evaljs("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
+
258
235
  context "when overwritten on JS level" do
259
236
  let(:_options) { { :default_url_options => { :format => "json" } } }
260
237
  it "uses JS defined value" do
@@ -421,8 +398,14 @@ describe JsRoutes, "options" do
421
398
  host
422
399
  end
423
400
 
424
- before do
425
- jscontext.eval("window = {'location': {'protocol': '#{current_protocol}', 'hostname': '#{current_hostname}', 'port': '#{current_port}', 'host': '#{current_host}'}}")
401
+ let(:_presetup) do
402
+ window = {location: {
403
+ protocol: current_protocol,
404
+ hostname: current_hostname,
405
+ port: current_port,
406
+ host: current_host,
407
+ }}
408
+ "const window = #{ActiveSupport::JSON.encode(window)}"
426
409
  end
427
410
 
428
411
  context "without specifying a default host" do
@@ -434,7 +417,6 @@ describe JsRoutes, "options" do
434
417
  expect(evaljs("Routes.inbox_url(1)")).to eq("http://current.example.com#{test_routes.inbox_path(1)}")
435
418
  expect(evaljs("Routes.inbox_url(1, { test_key: \"test_val\" })")).to eq("http://current.example.com#{test_routes.inbox_path(1, :test_key => "test_val")}")
436
419
  expect(evaljs("Routes.new_session_url()")).to eq("https://current.example.com#{test_routes.new_session_path}")
437
-
438
420
  end
439
421
 
440
422
  it "doesn't use current when specified in the route" do
@@ -5,7 +5,7 @@
5
5
  require 'spec_helper'
6
6
  require "fileutils"
7
7
 
8
- describe "after Rails initialization" do
8
+ describe "after Rails initialization", :slow do
9
9
  NAME = Rails.root.join('app', 'assets', 'javascripts', 'routes.js').to_s
10
10
 
11
11
  def sprockets_v3?
@@ -122,7 +122,7 @@ describe "after Rails initialization" do
122
122
  end
123
123
  end
124
124
 
125
- describe "JSRoutes thread safety" do
125
+ describe "JSRoutes thread safety", :slow do
126
126
  before do
127
127
  begin
128
128
  Rails.application.initialize!
data/spec/spec_helper.rb CHANGED
@@ -108,11 +108,10 @@ RSpec.configure do |config|
108
108
  end
109
109
 
110
110
  config.before :each do
111
- evaljs("var window = this;", {force: true})
112
-
113
111
  log = proc do |*values|
114
112
  puts values.map(&:inspect).join(", ")
115
113
  end
114
+
116
115
  if defined?(JRUBY_VERSION)
117
116
  jscontext[:"console.log"] = lambda do |context, *values|
118
117
  log(*values)
@@ -62,7 +62,7 @@ def draw_routes
62
62
 
63
63
  resources :portals, :port => 8080, only: [:index]
64
64
 
65
- get '/with_defaults' => 'foo#foo', defaults: { bar: 'tested', format: :json }, format: :true
65
+ get '/with_defaults' => 'foo#foo', defaults: { bar: 'tested', format: :json }, format: true
66
66
 
67
67
  namespace :api, format: true, defaults: {format: 'json'} do
68
68
  get "/purchases" => "purchases#index"
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../tsconfig.json",
3
+ "include": ["./**/*.spec.ts"]
4
+ }
data/tsconfig.json CHANGED
@@ -3,14 +3,10 @@
3
3
  "target": "es2019",
4
4
  "lib": ["es2020", "dom"],
5
5
  "module": "commonjs",
6
- "outDir": "lib",
7
6
  "sourceMap": false,
8
7
  "declaration": true,
9
8
  "allowSyntheticDefaultImports": true,
10
- "resolveJsonModule": true,
11
- "typeRoots": ["node_modules/@types", "./@types"],
12
-
13
- "strict": true,
9
+ "resolveJsonModule": false,
14
10
  "strictNullChecks": true,
15
11
  "noImplicitAny": true,
16
12
  "noImplicitThis": true,
@@ -22,11 +18,11 @@
22
18
  "forceConsistentCasingInFileNames": true,
23
19
  "emitDecoratorMetadata": true,
24
20
  "experimentalDecorators": true
21
+
25
22
  },
26
23
  "ts-node": {
27
24
  "files": true,
28
25
  "transpileOnly": false
29
26
  },
30
- "include": ["./**/*.ts", "lib/**/*.json", "./**/*.tsx"],
31
- "exclude": ["node_modules", "**/thorn", "**/support", "**/tmp"]
27
+ "include": ["lib/*.ts"]
32
28
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js-routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.8
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdan Gusiev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-21 00:00:00.000000000 Z
11
+ date: 2021-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 3.0.0
47
+ version: 3.10.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 3.0.0
54
+ version: 3.10.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -171,11 +171,17 @@ files:
171
171
  - lib/js-routes.rb
172
172
  - lib/js_routes.rb
173
173
  - lib/js_routes/engine.rb
174
+ - lib/js_routes/generators/middleware.rb
175
+ - lib/js_routes/generators/webpacker.rb
176
+ - lib/js_routes/middleware.rb
174
177
  - lib/js_routes/version.rb
175
178
  - lib/routes.d.ts
176
179
  - lib/routes.js
177
180
  - lib/routes.ts
178
181
  - lib/tasks/js_routes.rake
182
+ - lib/templates/erb.js
183
+ - lib/templates/initializer.rb
184
+ - lib/templates/routes.js.erb
179
185
  - package.json
180
186
  - spec/dummy/app/assets/config/manifest.js
181
187
  - spec/dummy/app/assets/javascripts/.gitkeep
@@ -183,13 +189,18 @@ files:
183
189
  - spec/js_routes/default_serializer_spec.rb
184
190
  - spec/js_routes/module_types/amd_spec.rb
185
191
  - spec/js_routes/module_types/cjs_spec.rb
192
+ - spec/js_routes/module_types/dts/routes.spec.d.ts
193
+ - spec/js_routes/module_types/dts/test.spec.ts
194
+ - spec/js_routes/module_types/dts_spec.rb
186
195
  - spec/js_routes/module_types/esm_spec.rb
196
+ - spec/js_routes/module_types/nil_spec.rb
187
197
  - spec/js_routes/module_types/umd_spec.rb
188
198
  - spec/js_routes/options_spec.rb
189
199
  - spec/js_routes/rails_routes_compatibility_spec.rb
190
200
  - spec/js_routes/zzz_last_post_rails_init_spec.rb
191
201
  - spec/spec_helper.rb
192
202
  - spec/support/routes.rb
203
+ - spec/tsconfig.json
193
204
  - tsconfig.json
194
205
  - yarn.lock
195
206
  homepage: http://github.com/railsware/js-routes