/
special-characters-test.tsx
985 lines (885 loc) · 31.1 KB
/
special-characters-test.tsx
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
/* eslint-disable jest/expect-expect */
import { JSDOM } from "jsdom";
import * as React from "react";
import {
cleanup,
render,
fireEvent,
waitFor,
screen,
} from "@testing-library/react";
import type { Location, Params } from "react-router-dom";
import {
BrowserRouter,
HashRouter,
MemoryRouter,
Link,
Routes,
Route,
RouterProvider,
createBrowserRouter,
createHashRouter,
createMemoryRouter,
createRoutesFromElements,
useLocation,
useNavigate,
useParams,
} from "react-router-dom";
/**
* Here's all the special characters we want to test against. This list was
* generated using the following utility in the Chrome DevTools console for
* maximum accuracy. This is instead of programmatically generating during
* these tests where JSDOM or a bad URL polyfill might not be trustworthy.
*
* function generateCharDef(char) {
* return {
* char,
* pathChar: new URL('/' + char, window.location.origin).pathname.replace(/^\//, ''),
* searchChar: new URL('/?=' + char, window.location.origin).search.replace(/^\?=/, ''),
* hashChar: new URL('/#' + char, window.location.origin).hash.replace(/^#/, ''),
* };
* }
*/
let specialChars = [
// This set of characters never gets encoded by window.location
{ char: "x", pathChar: "x", searchChar: "x", hashChar: "x" },
{ char: "X", pathChar: "X", searchChar: "X", hashChar: "X" },
{ char: "~", pathChar: "~", searchChar: "~", hashChar: "~" },
{ char: "!", pathChar: "!", searchChar: "!", hashChar: "!" },
{ char: "@", pathChar: "@", searchChar: "@", hashChar: "@" },
{ char: "$", pathChar: "$", searchChar: "$", hashChar: "$" },
{ char: "*", pathChar: "*", searchChar: "*", hashChar: "*" },
{ char: "(", pathChar: "(", searchChar: "(", hashChar: "(" },
{ char: ")", pathChar: ")", searchChar: ")", hashChar: ")" },
{ char: "_", pathChar: "_", searchChar: "_", hashChar: "_" },
{ char: "-", pathChar: "-", searchChar: "-", hashChar: "-" },
{ char: "+", pathChar: "+", searchChar: "+", hashChar: "+" },
{ char: "=", pathChar: "=", searchChar: "=", hashChar: "=" },
{ char: "[", pathChar: "[", searchChar: "[", hashChar: "[" },
{ char: "]", pathChar: "]", searchChar: "]", hashChar: "]" },
{ char: ":", pathChar: ":", searchChar: ":", hashChar: ":" },
{ char: ";", pathChar: ";", searchChar: ";", hashChar: ";" },
{ char: ",", pathChar: ",", searchChar: ",", hashChar: "," },
// These chars should only get encoded when in the pathname, but JSDOM
// seems to have a bug as it does not encode them, so don't test this
// case for now
// { char: "^", pathChar: "%5E", searchChar: "^", hashChar: "^" },
// { char: "|", pathChar: "%7C", searchChar: "|", hashChar: "|" },
// These chars get conditionally encoded based on what portion of the
// URL they occur in
{ char: "{", pathChar: "%7B", searchChar: "{", hashChar: "{" },
{ char: "}", pathChar: "%7D", searchChar: "}", hashChar: "}" },
{ char: "`", pathChar: "%60", searchChar: "`", hashChar: "%60" },
{ char: "'", pathChar: "'", searchChar: "%27", hashChar: "'" },
{ char: '"', pathChar: "%22", searchChar: "%22", hashChar: "%22" },
{ char: "<", pathChar: "%3C", searchChar: "%3C", hashChar: "%3C" },
{ char: ">", pathChar: "%3E", searchChar: "%3E", hashChar: "%3E" },
// These chars get encoded in all portions of the URL
{
char: "🤯",
pathChar: "%F0%9F%A4%AF",
searchChar: "%F0%9F%A4%AF",
hashChar: "%F0%9F%A4%AF",
},
{
char: "✅",
pathChar: "%E2%9C%85",
searchChar: "%E2%9C%85",
hashChar: "%E2%9C%85",
},
{
char: "🔥",
pathChar: "%F0%9F%94%A5",
searchChar: "%F0%9F%94%A5",
hashChar: "%F0%9F%94%A5",
},
{ char: "ä", pathChar: "%C3%A4", searchChar: "%C3%A4", hashChar: "%C3%A4" },
{ char: "Ä", pathChar: "%C3%84", searchChar: "%C3%84", hashChar: "%C3%84" },
{ char: "ø", pathChar: "%C3%B8", searchChar: "%C3%B8", hashChar: "%C3%B8" },
{
char: "山",
pathChar: "%E5%B1%B1",
searchChar: "%E5%B1%B1",
hashChar: "%E5%B1%B1",
},
{
char: "人",
pathChar: "%E4%BA%BA",
searchChar: "%E4%BA%BA",
hashChar: "%E4%BA%BA",
},
{
char: "口",
pathChar: "%E5%8F%A3",
searchChar: "%E5%8F%A3",
hashChar: "%E5%8F%A3",
},
{
char: "刀",
pathChar: "%E5%88%80",
searchChar: "%E5%88%80",
hashChar: "%E5%88%80",
},
{
char: "木",
pathChar: "%E6%9C%A8",
searchChar: "%E6%9C%A8",
hashChar: "%E6%9C%A8",
},
// Add a few multi-char space use cases for good measure
{ char: "a b", pathChar: "a%20b", searchChar: "a%20b", hashChar: "a%20b" },
{ char: "a+b", pathChar: "a+b", searchChar: "a+b", hashChar: "a+b" },
];
describe("special character tests", () => {
// Mutable vars we'll use to capture the useLocation/useParams values during
// the render pass. this avoids stringifying them into the DOM and parsing
// them back out which can mess with the encoding
let renderedUseLocation: Omit<Location, "state" | "key"> | null = null;
let renderedParams: Params<string> | null = null;
function CaptureLocation() {
let location = {
...useLocation(),
state: undefined,
key: undefined,
};
let params = useParams();
renderedUseLocation = {
pathname: location.pathname,
search: location.search,
hash: location.hash,
};
renderedParams = params;
return (
<>
<p>{location.pathname}</p>
<Link to="/reset">Link to reset</Link>
</>
);
}
beforeEach(() => {
renderedUseLocation = null;
renderedParams = null;
});
describe("when matching as param values", () => {
async function testParamValues(
navigatePath: string,
expectedHeading: string,
expectedLocation: Omit<Location, "state" | "key">,
expectedParams = {}
) {
// Need to use our own custom DOM in order to get a working history
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
url: "https://remix.run/",
});
let testWindow = dom.window as unknown as Window;
testWindow.history.replaceState(null, "", navigatePath);
function Comp({ heading }) {
return (
<>
<h1>{heading}</h1>
<CaptureLocation></CaptureLocation>
</>
);
}
let routeElements = (
<>
<Route path="/path" element={<Comp heading="Static Route" />} />
<Route
path="/inline-param/:slug"
element={<Comp heading="Inline Nested Param Route" />}
/>
<Route path="/param">
<Route
path=":slug"
element={<Comp heading="Parent Nested Param Route" />}
/>
</Route>
<Route
path="/inline-splat/*"
element={<Comp heading="Inline Nested Splat Route" />}
/>
<Route path="/splat">
<Route
path="*"
element={<Comp heading="Parent Nested Splat Route" />}
/>
</Route>
<Route
path="/reset"
element={<Link to={navigatePath}>Link to path</Link>}
/>
<Route path="/*" element={<Comp heading="Root Splat Route" />} />
</>
);
// Render BrowserRouter at the initialized location and confirm we get
// the right route match, window.location, useLocation(), and useParams()
// values
let ctx = render(
<BrowserRouter window={testWindow}>
<Routes>{routeElements}</Routes>
</BrowserRouter>
);
expect(ctx.container.querySelector("h1")?.innerHTML).toBe(
expectedHeading
);
let windowLocation = {
pathname: testWindow.location.pathname,
search: testWindow.location.search,
hash: testWindow.location.hash,
};
expect(windowLocation).toEqual(expectedLocation);
expect(renderedUseLocation).toEqual(expectedLocation);
expect(renderedParams).toEqual(expectedParams);
// Now client side away to a /reset route and back to the original path to confirm that
// client-side history.push calls also match the same expectations
await fireEvent.click(screen.getByText("Link to reset"));
await waitFor(() => screen.getByText(/Link to path/));
await fireEvent.click(screen.getByText("Link to path"));
await waitFor(() => screen.getByText(/Link to reset/));
expect(ctx.container.querySelector("h1")?.innerHTML).toBe(
expectedHeading
);
windowLocation = {
pathname: testWindow.location.pathname,
search: testWindow.location.search,
hash: testWindow.location.hash,
};
expect(windowLocation).toEqual(expectedLocation);
expect(renderedUseLocation).toEqual(expectedLocation);
expect(renderedParams).toEqual(expectedParams);
// Reset state
ctx.unmount();
cleanup();
renderedUseLocation = null;
renderedParams = null;
// Now run the same initialized-location render through a data router
// and confirm all the same assertions
let routes = createRoutesFromElements(routeElements);
let router = createBrowserRouter(routes, { window: testWindow });
ctx = render(<RouterProvider router={router} />);
expect(ctx.container.querySelector("h1")?.innerHTML).toBe(
expectedHeading
);
windowLocation = {
pathname: testWindow.location.pathname,
search: testWindow.location.search,
hash: testWindow.location.hash,
};
// Assert both window.location and useLocation() match what we expect
expect(windowLocation).toEqual(expectedLocation);
expect(renderedUseLocation).toEqual(expectedLocation);
expect(renderedParams).toEqual(expectedParams);
// Now client side away to a /reset route and back to the original path to confirm that
// client-side router.navigate calls also match the same expectations
await fireEvent.click(screen.getByText("Link to reset"));
await waitFor(() => screen.getByText(/Link to path/));
await fireEvent.click(screen.getByText("Link to path"));
await waitFor(() => screen.getByText(/Link to reset/));
expect(ctx.container.querySelector("h1")!.innerHTML).toBe(
expectedHeading
);
windowLocation = {
pathname: testWindow.location.pathname,
search: testWindow.location.search,
hash: testWindow.location.hash,
};
// Assert both window.location and useLocation() match what we expect
expect(windowLocation).toEqual(expectedLocation);
expect(renderedUseLocation).toEqual(expectedLocation);
expect(renderedParams).toEqual(expectedParams);
ctx.unmount();
cleanup();
renderedUseLocation = null;
renderedParams = null;
}
it("handles special chars in inline nested param route paths", async () => {
for (let charDef of specialChars) {
let { char, pathChar } = charDef;
await testParamValues(
`/inline-param/${char}`,
"Inline Nested Param Route",
{
pathname: `/inline-param/${pathChar}`,
search: "",
hash: "",
},
{ slug: char }
);
await testParamValues(
`/inline-param/foo${char}bar`,
"Inline Nested Param Route",
{
pathname: `/inline-param/foo${pathChar}bar`,
search: "",
hash: "",
},
{ slug: `foo${char}bar` }
);
}
});
it("handles special chars in parent nested param route paths", async () => {
for (let charDef of specialChars) {
let { char, pathChar } = charDef;
await testParamValues(
`/param/${char}`,
"Parent Nested Param Route",
{
pathname: `/param/${pathChar}`,
search: "",
hash: "",
},
{ slug: char }
);
await testParamValues(
`/param/foo${char}bar`,
"Parent Nested Param Route",
{
pathname: `/param/foo${pathChar}bar`,
search: "",
hash: "",
},
{ slug: `foo${char}bar` }
);
}
});
it("handles special chars in inline nested splat routes", async () => {
for (let charDef of specialChars) {
let { char, pathChar } = charDef;
await testParamValues(
`/inline-splat/${char}`,
"Inline Nested Splat Route",
{
pathname: `/inline-splat/${pathChar}`,
search: "",
hash: "",
},
{ "*": char }
);
await testParamValues(
`/inline-splat/foo${char}bar`,
"Inline Nested Splat Route",
{
pathname: `/inline-splat/foo${pathChar}bar`,
search: "",
hash: "",
},
{ "*": `foo${char}bar` }
);
}
});
it("handles special chars in nested splat routes", async () => {
for (let charDef of specialChars) {
let { char, pathChar } = charDef;
await testParamValues(
`/splat/${char}`,
"Parent Nested Splat Route",
{
pathname: `/splat/${pathChar}`,
search: "",
hash: "",
},
{ "*": char }
);
await testParamValues(
`/splat/foo${char}bar`,
"Parent Nested Splat Route",
{
pathname: `/splat/foo${pathChar}bar`,
search: "",
hash: "",
},
{ "*": `foo${char}bar` }
);
}
});
it("handles special chars in nested splat routes with separators", async () => {
for (let charDef of specialChars) {
let { char, pathChar } = charDef;
await testParamValues(
`/splat/foo/bar${char}`,
"Parent Nested Splat Route",
{
pathname: `/splat/foo/bar${pathChar}`,
search: "",
hash: "",
},
{ "*": `foo/bar${char}` }
);
}
});
it("handles special chars in root splat routes", async () => {
for (let charDef of specialChars) {
let { char, pathChar } = charDef;
await testParamValues(
`/${char}`,
"Root Splat Route",
{
pathname: `/${pathChar}`,
search: "",
hash: "",
},
{ "*": char }
);
await testParamValues(
`/foo${char}bar`,
"Root Splat Route",
{
pathname: `/foo${pathChar}bar`,
search: "",
hash: "",
},
{ "*": `foo${char}bar` }
);
}
});
it("handles special chars in root splat routes with separators", async () => {
for (let charDef of specialChars) {
let { char, pathChar } = charDef;
await testParamValues(
`/foo/bar${char}`,
"Root Splat Route",
{
pathname: `/foo/bar${pathChar}`,
search: "",
hash: "",
},
{ "*": `foo/bar${char}` }
);
}
});
it("handles special chars in search params", async () => {
for (let charDef of specialChars) {
let { char, searchChar } = charDef;
await testParamValues(`/path?key=${char}`, "Static Route", {
pathname: `/path`,
search: `?key=${searchChar}`,
hash: "",
});
}
});
it("handles special chars in hash values", async () => {
for (let charDef of specialChars) {
let { char, hashChar } = charDef;
await testParamValues(`/path#hash-${char}`, "Static Route", {
pathname: `/path`,
search: "",
hash: `#hash-${hashChar}`,
});
}
});
});
describe("when matching as part of the defined route path", () => {
async function assertRouteMatch(
char,
path: string,
expectedHeading: string,
expectedLocation: Omit<Location, "state" | "key">,
expectedParams = {}
) {
// Need to use our own custom DOM in order to get a working history
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
url: "https://remix.run/",
});
let testWindow = dom.window as unknown as Window;
testWindow.history.replaceState(null, "", path);
let renderedUseLocation: Omit<Location, "state" | "key"> | null = null;
let renderedParams: Params<string> | null = null;
function CaptureLocation() {
let location = {
...useLocation(),
state: undefined,
key: undefined,
};
let params = useParams();
renderedUseLocation = location;
renderedParams = params;
return (
<>
<p>{location.pathname}</p>
<Link to="/reset">Link to reset</Link>
</>
);
}
function Root() {
return (
<>
<h1>Matched Root</h1>
<CaptureLocation />
</>
);
}
function StaticNested() {
return (
<>
<h1>Matched Static Nested</h1>
<CaptureLocation />
</>
);
}
function ParamNested() {
return (
<>
<h1>Matched Param Nested</h1>
<CaptureLocation />
</>
);
}
let routeElements = (
<>
<Route path={`/${char}`} element={<Root />} />
<Route path="/nested">
<Route path={char} element={<StaticNested />} />
</Route>
<Route path="/:param">
<Route path={char} element={<ParamNested />} />
</Route>
<Route path="/reset" element={<Link to={path}>Link to path</Link>} />
<Route path="*" element={<h1>Not Found</h1>} />
</>
);
// Render BrowserRouter at the initialized location and confirm we get
// the right route match, window.location, useLocation(), and useParams()
// values
let ctx = render(
<BrowserRouter window={testWindow}>
<Routes>{routeElements}</Routes>
</BrowserRouter>
);
expect(ctx.container.querySelector("h1")!.innerHTML).toBe(
expectedHeading
);
let windowLocation = {
pathname: testWindow.location.pathname,
search: testWindow.location.search,
hash: testWindow.location.hash,
};
expect(windowLocation).toEqual(expectedLocation);
expect(renderedUseLocation).toEqual(expectedLocation);
expect(renderedParams).toEqual(expectedParams);
// Reset state
ctx.unmount();
cleanup();
renderedUseLocation = null;
renderedParams = null;
// Now run the same initialized-location render through a data router
// and confirm all the same assertions
let routes = createRoutesFromElements(routeElements);
let router = createBrowserRouter(routes, { window: testWindow });
ctx = render(<RouterProvider router={router} />);
expect(ctx.container.querySelector("h1")!.innerHTML).toBe(
expectedHeading
);
windowLocation = {
pathname: testWindow.location.pathname,
search: testWindow.location.search,
hash: testWindow.location.hash,
};
expect(windowLocation).toEqual(expectedLocation);
expect(renderedUseLocation).toEqual(expectedLocation);
expect(renderedParams).toEqual(expectedParams);
// Now client side away to a /reset route and back to the original path to confirm that
// client-side router.navigate calls also match the same expectations
await fireEvent.click(screen.getByText("Link to reset"));
await waitFor(() => screen.getByText(/Link to path/));
await fireEvent.click(screen.getByText("Link to path"));
await waitFor(() => screen.getByText(/Link to reset/));
expect(ctx.container.querySelector("h1")!.innerHTML).toBe(
expectedHeading
);
windowLocation = {
pathname: testWindow.location.pathname,
search: testWindow.location.search,
hash: testWindow.location.hash,
};
expect(windowLocation).toEqual(expectedLocation);
expect(renderedUseLocation).toEqual(expectedLocation);
expect(renderedParams).toEqual(expectedParams);
ctx.unmount();
cleanup();
renderedUseLocation = null;
renderedParams = null;
}
it("handles special chars in root route paths", async () => {
for (let charDef of specialChars) {
let { char, pathChar } = charDef;
// Skip * which is just a splat route
if (char === "*") {
continue;
}
await assertRouteMatch(char, `/${char}`, "Matched Root", {
pathname: `/${pathChar}`,
search: "",
hash: "",
});
}
});
it("handles special chars in static nested route paths", async () => {
for (let charDef of specialChars) {
let { char, pathChar } = charDef;
// Skip * which is just a splat route
if (char === "*") {
continue;
}
await assertRouteMatch(
char,
`/nested/${char}`,
"Matched Static Nested",
{
pathname: `/nested/${pathChar}`,
search: "",
hash: "",
}
);
}
});
it("handles special chars in nested param route paths", async () => {
for (let charDef of specialChars) {
let { char, pathChar } = charDef;
// Skip * which is just a splat route
if (char === "*") {
continue;
}
await assertRouteMatch(
char,
`/foo/${char}`,
"Matched Param Nested",
{
pathname: `/foo/${pathChar}`,
search: "",
hash: "",
},
{
param: "foo",
}
);
}
});
});
describe("encodes characters based on history implementation", () => {
function ShowPath() {
let { pathname, search, hash } = useLocation();
return <pre>{JSON.stringify({ pathname, search, hash })}</pre>;
}
describe("memory routers", () => {
it("does not encode characters in MemoryRouter", () => {
let ctx = render(
<MemoryRouter initialEntries={["/with space"]}>
<Routes>
<Route path="/with space" element={<ShowPath />} />
</Routes>
</MemoryRouter>
);
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
it("does not encode characters in MemoryRouter (navigate)", () => {
function Start() {
let navigate = useNavigate();
// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(() => navigate("/with space"), []);
return null;
}
let ctx = render(
<MemoryRouter>
<Routes>
<Route path="/" element={<Start />} />
<Route path="/with space" element={<ShowPath />} />
</Routes>
</MemoryRouter>
);
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
it("does not encode characters in createMemoryRouter", () => {
let router = createMemoryRouter(
[{ path: "/with space", element: <ShowPath /> }],
{ initialEntries: ["/with space"] }
);
let ctx = render(<RouterProvider router={router} />);
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
it("does not encode characters in createMemoryRouter (navigate)", () => {
function Start() {
let navigate = useNavigate();
// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(() => navigate("/with space"), []);
return null;
}
let router = createMemoryRouter([
{ path: "/", element: <Start /> },
{ path: "/with space", element: <ShowPath /> },
]);
let ctx = render(<RouterProvider router={router} />);
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
});
describe("browser routers", () => {
let testWindow: Window;
beforeEach(() => {
// Need to use our own custom DOM in order to get a working history
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
url: "https://remix.run/",
});
testWindow = dom.window as unknown as Window;
testWindow.history.pushState({}, "", "/");
});
it("encodes characters in BrowserRouter", () => {
testWindow.history.replaceState(null, "", "/with space");
let ctx = render(
<BrowserRouter window={testWindow}>
<Routes>
<Route path="/with space" element={<ShowPath />} />
</Routes>
</BrowserRouter>
);
expect(testWindow.location.pathname).toBe("/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
it("encodes characters in BrowserRouter (navigate)", () => {
testWindow.history.replaceState(null, "", "/");
function Start() {
let navigate = useNavigate();
// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(() => navigate("/with space"), []);
return null;
}
let ctx = render(
<BrowserRouter window={testWindow}>
<Routes>
<Route path="/" element={<Start />} />
<Route path="/with space" element={<ShowPath />} />
</Routes>
</BrowserRouter>
);
expect(testWindow.location.pathname).toBe("/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
it("encodes characters in createBrowserRouter", () => {
testWindow.history.replaceState(null, "", "/with space");
let router = createBrowserRouter(
[{ path: "/with space", element: <ShowPath /> }],
{ window: testWindow }
);
let ctx = render(<RouterProvider router={router} />);
expect(testWindow.location.pathname).toBe("/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
it("encodes characters in createBrowserRouter (navigate)", () => {
testWindow.history.replaceState(null, "", "/with space");
function Start() {
let navigate = useNavigate();
// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(() => navigate("/with space"), []);
return null;
}
let router = createBrowserRouter(
[
{ path: "/", element: <Start /> },
{ path: "/with space", element: <ShowPath /> },
],
{ window: testWindow }
);
let ctx = render(<RouterProvider router={router} />);
expect(testWindow.location.pathname).toBe("/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
});
describe("hash routers", () => {
let testWindow: Window;
beforeEach(() => {
// Need to use our own custom DOM in order to get a working history
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
url: "https://remix.run/",
});
testWindow = dom.window as unknown as Window;
testWindow.history.pushState({}, "", "/");
});
it("encodes characters in HashRouter", () => {
testWindow.history.replaceState(null, "", "/#/with space");
let ctx = render(
<HashRouter window={testWindow}>
<Routes>
<Route path="/with space" element={<ShowPath />} />
</Routes>
</HashRouter>
);
expect(testWindow.location.pathname).toBe("/");
expect(testWindow.location.hash).toBe("#/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
it("encodes characters in HashRouter (navigate)", () => {
testWindow.history.replaceState(null, "", "/");
function Start() {
let navigate = useNavigate();
// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(() => navigate("/with space"), []);
return null;
}
let ctx = render(
<HashRouter window={testWindow}>
<Routes>
<Route path="/" element={<Start />} />
<Route path="/with space" element={<ShowPath />} />
</Routes>
</HashRouter>
);
expect(testWindow.location.pathname).toBe("/");
expect(testWindow.location.hash).toBe("#/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
it("encodes characters in createHashRouter", () => {
testWindow.history.replaceState(null, "", "/#/with space");
let router = createHashRouter(
[{ path: "/with space", element: <ShowPath /> }],
{ window: testWindow }
);
let ctx = render(<RouterProvider router={router} />);
expect(testWindow.location.pathname).toBe("/");
expect(testWindow.location.hash).toBe("#/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
it("encodes characters in createHashRouter (navigate)", () => {
testWindow.history.replaceState(null, "", "/");
function Start() {
let navigate = useNavigate();
// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(() => navigate("/with space"), []);
return null;
}
let router = createHashRouter(
[
{ path: "/", element: <Start /> },
{ path: "/with space", element: <ShowPath /> },
],
{ window: testWindow }
);
let ctx = render(<RouterProvider router={router} />);
expect(testWindow.location.pathname).toBe("/");
expect(testWindow.location.hash).toBe("#/with%20space");
expect(ctx.container.innerHTML).toMatchInlineSnapshot(
`"<pre>{\\"pathname\\":\\"/with%20space\\",\\"search\\":\\"\\",\\"hash\\":\\"\\"}</pre>"`
);
});
});
});
});