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