-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathroute.test.js
1394 lines (1203 loc) · 54.8 KB
/
route.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @vitest-environment jsdom
import { beforeAll, beforeEach, describe, expect, test } from 'vitest';
import { route } from '../../src/js/index.js';
// import { route } from '../../dist/index.esm.js';
// import { route } from '../../dist/index.js';
// import route from '../../dist/route.umd.js';
const defaultWindow = {
location: {
host: 'ziggy.dev',
},
};
const defaultZiggy = {
url: 'https://ziggy.dev',
port: null,
defaults: { locale: 'en' },
routes: {
home: {
uri: '/',
methods: ['GET', 'HEAD'],
},
'posts.index': {
uri: 'posts',
methods: ['GET', 'HEAD'],
},
'posts.show': {
uri: 'posts/{post}',
methods: ['GET', 'HEAD'],
bindings: {
post: 'id',
},
},
'posts.update': {
uri: 'posts/{post}',
methods: ['PUT'],
bindings: {
post: 'id',
},
},
'postComments.show': {
uri: 'posts/{post}/comments/{comment}',
methods: ['GET', 'HEAD'],
bindings: {
post: 'id',
comment: 'uuid',
},
},
'translatePosts.index': {
uri: '{locale}/posts',
methods: ['GET', 'HEAD'],
},
'translatePosts.show': {
uri: '{locale}/posts/{id}',
methods: ['GET', 'HEAD'],
},
'translatePosts.update': {
uri: '{locale}/posts/{post}',
methods: ['PUT', 'PATCH'],
},
'events.venues-index': {
uri: 'events/{event}/venues-index',
methods: ['GET', 'HEAD'],
bindings: {
event: 'id',
},
},
'events.venues.index': {
uri: 'events/{event}/venues',
methods: ['GET', 'HEAD'],
bindings: {
event: 'id',
},
},
'events.venues.show': {
uri: 'events/{event}/venues/{venue}',
methods: ['GET', 'HEAD'],
bindings: {
event: 'id',
venue: 'id',
},
},
'events.venues.update': {
uri: 'events/{event}/venues/{venue}',
methods: ['PUT', 'PATCH'],
},
'translateEvents.venues.show': {
uri: '{locale}/events/{event}/venues/{venue}',
methods: ['GET', 'HEAD'],
bindings: {
event: 'id',
venue: 'id',
},
},
'conversations.show': {
uri: 'subscribers/{subscriber}/conversations/{type}/{conversation_id?}',
methods: ['GET', 'HEAD'],
},
optional: {
uri: 'optional/{id}/{slug?}',
methods: ['GET', 'HEAD'],
},
optionalId: {
uri: 'optionalId/{type}/{id?}',
methods: ['GET', 'HEAD'],
},
'team.user.show': {
uri: 'users/{id}',
methods: ['GET', 'HEAD'],
domain: '{team}.ziggy.dev',
},
'translateTeam.user.show': {
uri: '{locale}/users/{id}',
methods: ['GET', 'HEAD'],
domain: '{team}.ziggy.dev',
},
'products.show': {
uri: '{country?}/{language?}/products/{id}',
methods: ['GET', 'HEAD'],
},
'hosting-contacts.index': {
uri: 'hosting-contacts',
methods: ['GET', 'HEAD'],
},
'pages.optional': {
uri: 'optionalpage/{page?}',
methods: ['GET', 'HEAD'],
},
'pages.optionalExtension': {
uri: 'download/file{extension?}',
methods: ['GET', 'HEAD'],
},
'pages.requiredExtension': {
uri: 'strict-download/file{extension}',
methods: ['GET', 'HEAD'],
},
'pages.optionalWhere': {
uri: 'where/optionalpage/{page?}',
methods: ['GET', 'HEAD'],
wheres: {
page: '[0-9]+',
},
},
'pages.optionalExtensionWhere': {
uri: 'where/download/file{extension?}',
methods: ['GET', 'HEAD'],
wheres: {
extension: '\\.(php|html)',
},
},
'pages.requiredExtensionWhere': {
uri: 'where/strict-download/file{extension}',
methods: ['GET', 'HEAD'],
wheres: {
extension: '\\.(php|html)',
},
},
'pages.complexWhere': {
uri: 'where/{word}-{digit}/{required}/{optional?}/file{extension?}',
methods: ['GET', 'HEAD'],
wheres: {
word: '[a-z_-]+',
digit: '[0-9]+',
required: 'required',
optional: 'optional',
extension: '\\.(php|html)',
},
},
'pages.complexWhereConflict1': {
uri: 'where/{digit}-{word}/{required}/{optional?}/file{extension?}',
methods: ['GET', 'HEAD'],
wheres: {
word: '[a-z_-]+',
digit: '[0-9]+',
required: 'required',
optional: 'optional',
extension: '\\.(php|html)',
},
},
'pages.complexWhereConflict2': {
uri: 'where/complex-{digit}/{required}/{optional?}/file{extension?}',
methods: ['GET', 'HEAD'],
wheres: {
digit: '[0-9]+',
required: 'different_but_required',
optional: 'optional',
extension: '\\.(php|html)',
},
},
statistics: {
uri: 'статистика',
methods: ['GET', 'HEAD'],
},
pages: {
uri: '{page}',
methods: ['GET', 'HEAD'],
},
slashes: {
uri: 'slashes/{encoded}/{slug}',
methods: ['GET', 'HEAD'],
wheres: {
slug: '.*',
},
},
slashesOtherRegex: {
uri: 'slashes/{encoded}/{slug}',
methods: ['GET', 'HEAD'],
wheres: {
slug: '.+',
},
},
},
};
beforeAll(() => {
delete window.location;
window.location = {};
});
beforeEach(() => {
window.location = { ...defaultWindow.location };
global.window.location = window.location;
global.Ziggy = { ...defaultZiggy };
});
describe('route()', () => {
test('can generate a URL with no parameters', () => {
expect(route('posts.index')).toBe('https://ziggy.dev/posts');
});
test('can generate a URL with default parameters', () => {
expect(route('translatePosts.index')).toBe('https://ziggy.dev/en/posts');
});
test('can generate a relative URL by passing absolute = false', () => {
expect(route('posts.index', [], false)).toBe('/posts');
});
test('can generate a URL with filled optional parameters', () => {
expect(
route('conversations.show', {
type: 'email',
subscriber: 123,
conversation_id: 1234,
}),
).toBe('https://ziggy.dev/subscribers/123/conversations/email/1234');
});
test('can generate a relative URL with filled optional parameters', () => {
expect(
route(
'conversations.show',
{
type: 'email',
subscriber: 123,
conversation_id: 1234,
},
false,
),
).toBe('/subscribers/123/conversations/email/1234');
});
test('can generate a relative URL with default parameters', () => {
expect(route('translatePosts.index', [], false)).toBe('/en/posts');
});
test('can error if a required parameter is not provided', () => {
expect(() => route('posts.show')).toThrow(/'post' parameter is required/);
});
test('can error if a required parameter is not provided to a route with default parameters', () => {
expect(() => route('translatePosts.show')).toThrow(/'id' parameter is required/);
});
test('can error if a required parameter with a default has no default value', () => {
global.Ziggy.defaults = {};
expect(() => route('translatePosts.index')).toThrow(/'locale' parameter is required/);
});
test('can generate a URL using an integer', () => {
// route with required parameters
expect(route('posts.show', 1)).toBe('https://ziggy.dev/posts/1');
// route with default parameters
expect(route('translatePosts.show', 1)).toBe('https://ziggy.dev/en/posts/1');
});
test('can generate a URL using a string', () => {
// route with required parameters
expect(route('posts.show', 'my-first-post')).toBe('https://ziggy.dev/posts/my-first-post');
// route with default parameters
expect(route('translatePosts.show', 'my-first-post')).toBe(
'https://ziggy.dev/en/posts/my-first-post',
);
});
test('can generate a URL using an object', () => {
// routes with required parameters
expect(route('posts.show', { id: 1 })).toBe('https://ziggy.dev/posts/1');
expect(route('events.venues.show', { event: 1, venue: 2 })).toBe(
'https://ziggy.dev/events/1/venues/2',
);
// route with optional parameters
expect(route('optionalId', { type: 'model', id: 1 })).toBe(
'https://ziggy.dev/optionalId/model/1',
);
// route with both required and default parameters
expect(route('translateEvents.venues.show', { event: 1, venue: 2 })).toBe(
'https://ziggy.dev/en/events/1/venues/2',
);
});
test('can generate a URL using an array', () => {
// routes with required parameters
expect(route('posts.show', [1])).toBe('https://ziggy.dev/posts/1');
expect(route('events.venues.show', [1, 2])).toBe('https://ziggy.dev/events/1/venues/2');
expect(route('events.venues.show', [1, 'coliseum'])).toBe(
'https://ziggy.dev/events/1/venues/coliseum',
);
// route with default parameters
expect(route('translatePosts.show', [1])).toBe('https://ziggy.dev/en/posts/1');
// route with both required and default parameters
expect(route('translateEvents.venues.show', [1, 2])).toBe(
'https://ziggy.dev/en/events/1/venues/2',
);
});
test('can generate a URL using an array of objects', () => {
const event = { id: 1, name: 'World Series' };
const venue = { id: 2, name: 'Rogers Centre' };
// route with required parameters
expect(route('events.venues.show', [event, venue])).toBe(
'https://ziggy.dev/events/1/venues/2',
);
// route with required and default parameters
expect(route('translateEvents.venues.show', [event, venue])).toBe(
'https://ziggy.dev/en/events/1/venues/2',
);
});
test('can generate a URL using an array of integers and objects', () => {
const venue = { id: 2, name: 'Rogers Centre' };
// route with required parameters
expect(route('events.venues.show', [1, venue])).toBe('https://ziggy.dev/events/1/venues/2');
// route with required and default parameters
expect(route('translateEvents.venues.show', [1, venue])).toBe(
'https://ziggy.dev/en/events/1/venues/2',
);
});
test('can generate a URL for a route with domain parameters', () => {
// route with required domain parameters
expect(route('team.user.show', { team: 'tighten', id: 1 })).toBe(
'https://tighten.ziggy.dev/users/1',
);
// route with required domain parameters and default parameters
expect(route('translateTeam.user.show', { team: 'tighten', id: 1 })).toBe(
'https://tighten.ziggy.dev/en/users/1',
);
});
test('can generate a URL for a route with a custom route model binding scope', () => {
expect(
route('postComments.show', [
{ id: 1, title: 'Post' },
{ uuid: 12345, title: 'Comment' },
]),
).toBe('https://ziggy.dev/posts/1/comments/12345');
expect(route('postComments.show', [1, { uuid: 'correct-horse-etc-etc' }])).toBe(
'https://ziggy.dev/posts/1/comments/correct-horse-etc-etc',
);
});
test("can fall back to an 'id' key if an object is passed for a parameter with no registered bindings", () => {
expect(route('translatePosts.update', { id: 14 })).toBe('https://ziggy.dev/en/posts/14');
expect(route('translatePosts.update', [{ id: 14 }])).toBe('https://ziggy.dev/en/posts/14');
expect(route('events.venues.update', [{ id: 10 }, { id: 1 }])).toBe(
'https://ziggy.dev/events/10/venues/1',
);
});
test('can generate a URL for an app installed in a subfolder', () => {
global.Ziggy.url = 'https://ziggy.dev/subfolder';
expect(route('postComments.show', [1, { uuid: 'correct-horse-etc-etc' }])).toBe(
'https://ziggy.dev/subfolder/posts/1/comments/correct-horse-etc-etc',
);
});
test('can error if a route model binding key is missing', () => {
expect(() => route('postComments.show', [1, { count: 20 }])).toThrow(
/Ziggy error: object passed as 'comment' parameter is missing route model binding key 'uuid'\./,
);
});
test('can return base URL if path is "/"', () => {
expect(route('home')).toBe('https://ziggy.dev');
});
test('can generate a relative URL to a root path', () => {
expect(route('home', undefined, false)).toBe('/');
});
// @todo duplicate
test('can ignore an optional parameter', () => {
expect(route('optional', { id: 123 })).toBe('https://ziggy.dev/optional/123');
expect(route('optional', { id: 123, slug: 'news' })).toBe(
'https://ziggy.dev/optional/123/news',
);
expect(route('optional', { id: 123, slug: null })).toBe('https://ziggy.dev/optional/123');
});
test('can ignore a single optional parameter', () => {
expect(route('pages.optional')).toBe('https://ziggy.dev/optionalpage');
expect(route('pages.optional', {})).toBe('https://ziggy.dev/optionalpage');
expect(route('pages.optional', undefined)).toBe('https://ziggy.dev/optionalpage');
expect(route('pages.optional', null)).toBe('https://ziggy.dev/optionalpage');
});
test('missing optional parameter in first path segment', () => {
expect(route('products.show', { country: 'ca', language: 'fr', id: 1 })).toBe(
'https://ziggy.dev/ca/fr/products/1',
);
// These URLs aren't valid but this matches the behavior of Laravel's PHP `route()` helper
expect(route('products.show', { country: 'ca', id: 1 })).toBe(
'https://ziggy.dev/ca//products/1',
);
expect(route('products.show', { id: 1 })).toBe('https://ziggy.dev//products/1');
// First param is handled correctly
expect(route('products.show', { language: 'fr', id: 1 })).toBe(
'https://ziggy.dev/fr/products/1',
);
});
test('can error if a route name doesn’t exist', () => {
expect(() => route('unknown-route')).toThrow(
/Ziggy error: route 'unknown-route' is not in the route list\./,
);
});
test('can automatically append extra parameter values as a query string', () => {
expect(
route('events.venues.show', {
event: 1,
venue: 2,
search: 'rogers',
page: 2,
}),
).toBe('https://ziggy.dev/events/1/venues/2?search=rogers&page=2');
expect(
route('events.venues.show', {
id: 2,
event: 1,
venue: 2,
search: 'rogers',
}),
).toBe('https://ziggy.dev/events/1/venues/2?id=2&search=rogers');
// ignore values explicitly set to `null`
expect(route('posts.index', { filled: 'filling', empty: null })).toBe(
'https://ziggy.dev/posts?filled=filling',
);
});
test('can cast boolean query parameters to integers', () => {
expect(route('posts.show', { post: 1, preview: true })).toBe(
'https://ziggy.dev/posts/1?preview=1',
);
});
test('can explicitly append query parameters using _query parameter', () => {
expect(
route('events.venues.show', {
event: 1,
venue: 2,
_query: {
event: 4,
venue: 2,
},
}),
).toBe('https://ziggy.dev/events/1/venues/2?event=4&venue=2');
expect(
route('events.venues.show', {
event: { id: 4, name: 'Fun Event' },
_query: {
event: 9,
id: 12,
},
venue: 2,
}),
).toBe('https://ziggy.dev/events/4/venues/2?event=9&id=12');
});
test('can generate a URL with a port', () => {
global.Ziggy.url = 'https://ziggy.dev:81';
global.Ziggy.port = 81;
// route with no parameters
expect(route('posts.index')).toBe('https://ziggy.dev:81/posts');
// route with required domain parameters
expect(route('team.user.show', { team: 'tighten', id: 1 })).toBe(
'https://tighten.ziggy.dev:81/users/1',
);
});
test('can handle trailing path segments in the base URL', () => {
global.Ziggy.url = 'https://test.thing/ab/cd';
expect(route('events.venues.index', 1)).toBe('https://test.thing/ab/cd/events/1/venues');
});
test('URL-encode query parameters', () => {
global.Ziggy.url = 'https://test.thing/ab/cd';
expect(route('events.venues.index', { event: 'Fun&Games' })).toBe(
'https://test.thing/ab/cd/events/Fun&Games/venues',
);
expect(
route('events.venues.index', {
event: 'Fun&Games',
location: 'Blues&Clues',
}),
).toBe('https://test.thing/ab/cd/events/Fun&Games/venues?location=Blues%26Clues');
});
test('can format an array of query parameters', () => {
expect(
route('events.venues.index', {
event: 'test',
guests: ['a', 'b', 'c'],
}),
).toBe('https://ziggy.dev/events/test/venues?guests[0]=a&guests[1]=b&guests[2]=c');
});
test('can handle a parameter explicity set to `0`', () => {
expect(route('posts.update', 0)).toBe('https://ziggy.dev/posts/0');
});
test('can accept a custom Ziggy configuration object', () => {
const config = {
url: 'http://notYourAverage.dev',
port: null,
defaults: { locale: 'en' },
routes: {
'tightenDev.packages.index': {
uri: 'tightenDev/{dev}/packages',
methods: ['GET', 'HEAD'],
},
},
};
expect(route('tightenDev.packages.index', { dev: 1 }, true, config)).toBe(
'http://notYourAverage.dev/tightenDev/1/packages',
);
});
test('can extract parameters for an app installed in a subfolder', () => {
global.Ziggy.url = 'https://ziggy.dev/subfolder';
global.window.location.href = 'https://ziggy.dev/subfolder/ph/en/products/4';
global.window.location.host = 'ziggy.dev';
global.window.location.pathname = '/subfolder/ph/en/products/4';
expect(route().params).toStrictEqual({ country: 'ph', language: 'en', id: '4' });
});
test('can extract parameters for an app installed in nested subfolders', () => {
global.Ziggy.url = 'https://ziggy.dev/nested/subfolder';
global.window.location.href = 'https://ziggy.dev/nested/subfolder/ph/en/products/4';
global.window.location.host = 'ziggy.dev';
global.window.location.pathname = '/nested/subfolder/ph/en/products/4';
expect(route().params).toStrictEqual({ country: 'ph', language: 'en', id: '4' });
});
test('can extract domain parameters from the current URL', () => {
global.window.location.href = 'https://tighten.ziggy.dev/users/1';
global.window.location.host = 'tighten.ziggy.dev';
global.window.location.pathname = '/users/1';
expect(route().params).toStrictEqual({ team: 'tighten', id: '1' });
});
test('can extract named parameters from the current URL', () => {
global.window.location.href = 'https://ziggy.dev/posts/1';
global.window.location.host = 'ziggy.dev';
global.window.location.pathname = '/posts/1';
expect(route().params).toStrictEqual({ post: '1' });
global.window.location.href = 'https://ziggy.dev/events/1/venues/2';
global.window.location.pathname = '/events/1/venues/2';
expect(route().params).toStrictEqual({ event: '1', venue: '2' });
});
test('can decode parameters in the current URL', () => {
global.window.location.href = 'https://ziggy.dev/events/1/venues/1%2B2%263';
global.window.location.pathname = '/events/1/venues/1%2B2%263';
expect(route().params).toStrictEqual({ event: '1', venue: '1+2&3' });
});
test('can extract query parameters from the current URL', () => {
global.window.location.href = 'https://ziggy.dev/posts/1?guest[name]=Taylor';
global.window.location.host = 'ziggy.dev';
global.window.location.pathname = '/posts/1';
global.window.location.search = '?guest[name]=Taylor';
expect(route().params).toStrictEqual({ post: '1', guest: { name: 'Taylor' } });
global.window.location.href = 'https://ziggy.dev/events/1/venues/2?id=5&vip=1%2B2%263';
global.window.location.pathname = '/events/1/venues/2';
global.window.location.search = '?id=5&vip=1%2B2%263';
expect(route().params).toStrictEqual({
event: '1',
venue: '2',
id: '5',
vip: '1+2&3',
});
});
test("can append 'extra' string/number parameter to query", () => {
// 'posts.index' has no parameters
expect(route('posts.index', 'extra')).toBe('https://ziggy.dev/posts?extra=');
expect(route('posts.index', [{ extra: 2 }])).toBe('https://ziggy.dev/posts?extra=2');
expect(route('posts.index', 1)).toBe('https://ziggy.dev/posts?1=');
});
test("can append 'extra' elements in array of parameters to query", () => {
// 'posts.show' has exactly one parameter
expect(route('posts.show', [1, 2])).toBe('https://ziggy.dev/posts/1?2=');
expect(route('posts.show', ['my-first-post', 'foo', 'bar'])).toBe(
'https://ziggy.dev/posts/my-first-post?foo=&bar=',
);
expect(route('posts.show', ['my-first-post', 'foo', { bar: 'baz' }])).toBe(
'https://ziggy.dev/posts/my-first-post?foo=&bar=baz',
);
});
test("can automatically append object with only 'extra' parameters to query", () => {
// Route has no parameters, the entire parameters object is 'extra' and should be used as the query string
expect(route('hosting-contacts.index', { filter: { name: 'Dwyer' } })).toBe(
'https://ziggy.dev/hosting-contacts?filter[name]=Dwyer',
);
});
test("can append 'extra' object parameter to query", () => {
expect(route('posts.show', { post: 2, filter: { name: 'Dwyer' } })).toBe(
'https://ziggy.dev/posts/2?filter[name]=Dwyer',
);
});
test('can generate a URL for a route with parameters inside individual segments', () => {
expect(route('pages.requiredExtension', 'x')).toBe(
'https://ziggy.dev/strict-download/filex',
);
expect(route('pages.requiredExtension', '.html')).toBe(
'https://ziggy.dev/strict-download/file.html',
);
expect(route('pages.requiredExtension', { extension: '.pdf' })).toBe(
'https://ziggy.dev/strict-download/file.pdf',
);
});
test('can generate a URL for a route with optional parameters inside individual segments', () => {
expect(route('pages.optionalExtension')).toBe('https://ziggy.dev/download/file');
expect(route('pages.optionalExtension', '.html')).toBe(
'https://ziggy.dev/download/file.html',
);
expect(route('pages.optionalExtension', { extension: '.pdf' })).toBe(
'https://ziggy.dev/download/file.pdf',
);
});
test('can generate a URL for a route with optional parameters inside individual segments respecting where requirements', () => {
expect(route('pages.optionalExtensionWhere')).toBe('https://ziggy.dev/where/download/file');
expect(route('pages.optionalExtensionWhere', '.html')).toBe(
'https://ziggy.dev/where/download/file.html',
);
expect(() => route('pages.optionalExtensionWhere', { extension: '.pdf' })).toThrow(
/'extension' parameter does not match required format/,
);
});
test('can generate a URL for a route with parameters inside individual segments', () => {
expect(route('pages.requiredExtensionWhere', '.html')).toBe(
'https://ziggy.dev/where/strict-download/file.html',
);
expect(() => route('pages.requiredExtensionWhere', 'x')).toThrow(
/'extension' parameter does not match required format/,
);
expect(() => route('pages.requiredExtensionWhere', { extension: '.pdf' })).toThrow(
/'extension' parameter does not match required format/,
);
});
test('skip encoding slashes inside last parameter when explicitly allowed', () => {
expect(route('slashes', ['one/two', 'three/four'])).toBe(
'https://ziggy.dev/slashes/one/two/three/four',
);
expect(route('slashes', ['one/two', 'Fun&Games/venues'])).toBe(
'https://ziggy.dev/slashes/one/two/Fun&Games/venues',
);
expect(route('slashes', ['one/two/three', 'Fun&Games/venues/outdoors'])).toBe(
'https://ziggy.dev/slashes/one/two/three/Fun&Games/venues/outdoors',
);
expect(route('slashesOtherRegex', ['one/two', 'three/four'])).toBe(
'https://ziggy.dev/slashes/one/two/three/four',
);
expect(route('slashesOtherRegex', ['one/two', 'Fun&Games/venues'])).toBe(
'https://ziggy.dev/slashes/one/two/Fun&Games/venues',
);
expect(route('slashesOtherRegex', ['one/two/three', 'Fun&Games/venues/outdoors'])).toBe(
'https://ziggy.dev/slashes/one/two/three/Fun&Games/venues/outdoors',
);
});
test('skip encoding some characters in route parameters', () => {
// Laravel doesn't encode these characters in route parameters: / @ : ; , = + ! * | ? & # %
expect(route('pages', 'a/b')).toBe('https://ziggy.dev/a/b');
expect(route('pages', 'a@b')).toBe('https://ziggy.dev/a@b');
expect(route('pages', 'a:b')).toBe('https://ziggy.dev/a:b');
expect(route('pages', 'a;b')).toBe('https://ziggy.dev/a;b');
expect(route('pages', 'a,b')).toBe('https://ziggy.dev/a,b');
expect(route('pages', 'a=b')).toBe('https://ziggy.dev/a=b');
expect(route('pages', 'a+b')).toBe('https://ziggy.dev/a+b');
expect(route('pages', 'a!b')).toBe('https://ziggy.dev/a!b');
expect(route('pages', 'a*b')).toBe('https://ziggy.dev/a*b');
expect(route('pages', 'a|b')).toBe('https://ziggy.dev/a|b');
expect(route('pages', 'a?b')).toBe('https://ziggy.dev/a?b');
expect(route('pages', 'a&b')).toBe('https://ziggy.dev/a&b');
expect(route('pages', 'a#b')).toBe('https://ziggy.dev/a#b');
expect(route('pages', 'a%b')).toBe('https://ziggy.dev/a%b');
// Laravel does encode '$', but encodeURI() doesn't
expect(route('pages', 'a$b')).toBe('https://ziggy.dev/a%24b');
});
});
describe('has()', () => {
test('can check if given named route exists', () => {
expect(route().has('posts.show')).toBe(true);
expect(route().has('non.existing.route')).toBe(false);
});
});
describe('current()', () => {
test('can get the current route name', () => {
global.window.location.pathname = '/events/1/venues/2';
expect(route().current()).toBe('events.venues.show');
});
test('can get the current route name on a route with multiple allowed HTTP methods', () => {
global.window.location.pathname = '/posts/1';
expect(route().current()).toBe('posts.show');
});
test('can get the current route name with a missing protocol', () => {
global.window.location.pathname = '/events/1/venues/';
global.window.location.protocol = '';
expect(route().current()).toBe('events.venues.index');
});
test('can get the current route name at the domain root', () => {
global.window.location.href = 'https://ziggy.dev';
global.window.location.host = 'ziggy.dev';
global.window.location.pathname = '/';
expect(route().current()).toBe('home');
expect(route().current('home')).toBe(true);
});
test('can ignore query string when getting current route name', () => {
global.window.location.pathname = '/events/1/venues?foo=2';
expect(route().current()).toBe('events.venues.index');
});
test('can ignore domain when getting current route name and absolute is false', () => {
global.window.location.href = 'https://tighten.ziggy.dev/events/1/venues?foo=2';
global.window.location.host = 'tighten.ziggy.dev';
global.window.location.pathname = '/events/1/venues?foo=2';
expect(route(undefined, undefined, false).current()).toBe('events.venues.index');
global.window.location.href = 'https://example.com/events/1/venues?foo=2';
global.window.location.host = 'example.com';
global.window.location.pathname = '/events/1/venues?foo=2';
expect(route(undefined, undefined, false).current()).toBe('events.venues.index');
});
test('can ignore domain when getting current route name, absolute is false, and app is in a subfolder', () => {
global.Ziggy.url = 'https://tighten.ziggy.dev/subfolder';
global.window.location.href = 'https://tighten.ziggy.dev/subfolder/events/1/venues?foo=2';
global.window.location.pathname = '/subfolder/events/1/venues?foo=2';
expect(route(undefined, undefined, false).current()).toBe('events.venues.index');
global.Ziggy.url = 'https://example.com/nested/subfolder';
global.window.location.href = 'https://example.com/nested/subfolder/events/1/venues?foo=2';
global.window.location.pathname = '/nested/subfolder/events/1/venues?foo=2';
expect(route(undefined, undefined, false).current()).toBe('events.venues.index');
});
test('can get the current route name with a custom Ziggy object', () => {
global.Ziggy = undefined;
global.window.location.pathname = '/events/';
const config = {
url: 'https://ziggy.dev',
port: null,
routes: {
'events.index': {
uri: 'events',
methods: ['GET', 'HEAD'],
},
},
};
expect(route(undefined, undefined, undefined, config).current()).toBe('events.index');
});
test('can return undefined when getting the current route name on an unknown URL', () => {
global.window.location.pathname = '/unknown/path';
expect(route().current()).toBe(undefined);
});
test('can check the current route name on a route/URL made up entirely of a single parameter', () => {
global.window.location.pathname = '/some-page';
expect(route().current()).toBe('pages');
expect(route().current('pages', { page: 'some-page' })).toBe(true);
});
test('can check the current route name on a route/URL made up entirely of a single optional parameter', () => {
global.window.location.pathname = '/optionalpage/foo';
expect(route().current()).toBe('pages.optional');
expect(route().current('pages.optional', '')).toBe(false);
expect(route().current('pages.optional', [''])).toBe(false);
// expect(route().current('pages.optional', [])).toBe(false); // Not supported
expect(route().current('pages.optional', { page: '' })).toBe(false);
expect(route().current('pages.optional', { page: undefined })).toBe(false);
expect(route().current('pages.optional', { page: 'foo' })).toBe(true);
});
test('can check the current route name on a route/URL made up entirely of a single optional parameter and with that parameter missing', () => {
global.window.location.pathname = '/optionalpage';
expect(route().current()).toBe('pages.optional');
expect(route().current('pages.optional', '')).toBe(true);
// expect(route().current('pages.optional', [])).toBe(true); // Not supported
expect(route().current('pages.optional', [''])).toBe(true);
expect(route().current('pages.optional', { page: '' })).toBe(true);
expect(route().current('pages.optional', { page: undefined })).toBe(true);
expect(route().current('pages.optional', { page: 'foo' })).toBe(false);
global.window.location.pathname = '/where/optionalpage';
expect(route().current('pages.optionalWhere', { page: undefined })).toBe(true);
expect(route().current('pages.optionalWhere', { page: 'foo' })).toBe(false);
global.window.location.pathname = '/where/optionalpage/23';
expect(route().current('pages.optionalWhere', { page: 23 })).toBe(true);
expect(route().current('pages.optionalWhere', { page: 22 })).toBe(false);
});
test('can check current route with complex requirements without conflicts', () => {
global.window.location.pathname = '/where/word-12/required/file';
expect(route().current('pages.complexWhere')).toBe(true);
expect(route().current('pages.complexWhereConflict1')).toBe(false);
global.window.location.pathname = '/where/complex-12/required/file';
expect(
route().current('pages.complexWhere', {
word: 'complex',
digit: '12',
required: 'required',
}),
).toBe(true);
expect(route().current('pages.complexWhereConflict1')).toBe(false);
global.window.location.pathname = '/where/123-abc/required/file.html';
expect(route().current('pages.complexWhereConflict1')).toBe(true);
expect(route().current('pages.complexWhere')).toBe(false);
global.window.location.pathname = '/where/complex-12/different_but_required/optional/file';
expect(route().current('pages.complexWhereConflict2')).toBe(true);
expect(route().current('pages.complexWhere')).toBe(false);
});
test('can current route with complex requirements is dehydrated correctly', () => {
global.window.location.pathname = '/where/word-12/required/file';
expect(route().params).toStrictEqual({
digit: '12',
word: 'word',
required: 'required',
optional: undefined,
extension: undefined,
});
global.window.location.pathname = '/where/complex-12/required/optional/file';
expect(route().params).toStrictEqual({
digit: '12',
word: 'complex',
required: 'required',
optional: 'optional',
extension: undefined,
});
global.window.location.pathname = '/where/123-abc/required/file.html';
expect(route().params).toStrictEqual({
digit: '123',
word: 'abc',
required: 'required',
optional: undefined,
extension: '.html',
});
global.window.location.pathname = '/where/complex-12/different_but_required/optional/file';
expect(route().params).toStrictEqual({
digit: '12',
required: 'different_but_required',
optional: 'optional',
extension: undefined,
});
global.window.location.search = '?ab=cd&ef=1&dd';
expect(route().params).toStrictEqual({
digit: '12',
required: 'different_but_required',
optional: 'optional',
extension: undefined,
ab: 'cd',
ef: '1',
dd: '',
});
});
test('can strip regex start and end of string tokens from wheres', () => {
global.Ziggy = undefined;
global.window.location.pathname = '/workspace/processes';
const config = {
url: 'https://ziggy.dev',
port: null,
routes: {
'workspaces.processes.index': {
uri: '{workspace}/processes',
methods: ['GET', 'HEAD'],
wheres: {
workspace: '^(?!api|nova-api|horizon).*$',
},
},
},
};
expect(route(undefined, undefined, undefined, config).current()).toBe(
'workspaces.processes.index',
);
});
test('can check the current route name at a URL with a non-delimited parameter', () => {
global.window.location.pathname = '/strict-download/file.html';
expect(route().current()).toBe('pages.requiredExtension');
expect(route().current('pages.requiredExtension', '')).toBe(false);
expect(route().current('pages.requiredExtension*', '')).toBe(false);
expect(route().current('pages.requiredExtension', '.html')).toBe(true);
expect(route().current('pages.requiredExtension*', '.html')).toBe(true);
expect(route().current('pages.requiredExtension', [''])).toBe(false);
expect(route().current('pages.requiredExtension*', [''])).toBe(false);
expect(route().current('pages.requiredExtension', ['.html'])).toBe(true);
expect(route().current('pages.requiredExtension*', ['.html'])).toBe(true);
expect(route().current('pages.requiredExtension', { extension: '' })).toBe(false);
expect(route().current('pages.requiredExtension*', { extension: '' })).toBe(false);
expect(route().current('pages.requiredExtension', { extension: '.pdf' })).toBe(false);
expect(route().current('pages.requiredExtension*', { extension: '.pdf' })).toBe(false);
expect(route().current('pages.requiredExtension', { extension: '.html' })).toBe(true);
expect(route().current('pages.requiredExtension*', { extension: '.html' })).toBe(true);
});
test('can check the current route name at a URL with a missing non-delimited optional parameter', () => {
global.window.location.pathname = '/download/file';
expect(route().current()).toBe('pages.optionalExtension');
expect(route().current('pages.optionalExtension', '')).toBe(true);
expect(route().current('pages.optionalExtension*', '')).toBe(true);
expect(route().current('pages.optionalExtension', [''])).toBe(true);
expect(route().current('pages.optionalExtension*', [''])).toBe(true);