File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed
packages/react-router/__tests__ Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ import * as React from "react" ;
2
+ import { create as createTestRenderer } from "react-test-renderer" ;
3
+ import {
4
+ MemoryRouter as Router ,
5
+ Routes ,
6
+ Route ,
7
+ useResolvedPath
8
+ } from "react-router" ;
9
+ import type { Path } from "history" ;
10
+
11
+ describe ( "useResolvedPath" , ( ) => {
12
+ it ( "path string resolves to Path object" , ( ) => {
13
+ let path ! : Path ;
14
+ function Home ( ) {
15
+ path = useResolvedPath ( "/home?user=mj#welcome" ) ;
16
+ return < h1 > Home</ h1 > ;
17
+ }
18
+
19
+ createTestRenderer (
20
+ < Router initialEntries = { [ "/home" ] } >
21
+ < Routes >
22
+ < Route path = "/home" element = { < Home /> } />
23
+ </ Routes >
24
+ </ Router >
25
+ ) ;
26
+
27
+ expect ( typeof path ) . toBe ( "object" ) ;
28
+ expect ( path ) . toMatchObject ( {
29
+ pathname : "/home" ,
30
+ search : "?user=mj" ,
31
+ hash : "#welcome"
32
+ } ) ;
33
+ } ) ;
34
+
35
+ describe ( "given a hash with a ? character" , ( ) => {
36
+ it ( "hash is not parsed as a search string" , ( ) => {
37
+ let path ! : Path ;
38
+ function Home ( ) {
39
+ path = useResolvedPath ( "/home#welcome?user=mj" ) ;
40
+ return < h1 > Home</ h1 > ;
41
+ }
42
+
43
+ createTestRenderer (
44
+ < Router initialEntries = { [ "/home" ] } >
45
+ < Routes >
46
+ < Route path = "/home" element = { < Home /> } />
47
+ </ Routes >
48
+ </ Router >
49
+ ) ;
50
+
51
+ expect ( typeof path ) . toBe ( "object" ) ;
52
+ expect ( path ) . toMatchObject ( {
53
+ pathname : "/home" ,
54
+ search : "" ,
55
+ hash : "#welcome?user=mj"
56
+ } ) ;
57
+ } ) ;
58
+ } ) ;
59
+ } ) ;
You can’t perform that action at this time.
0 commit comments