@@ -5,6 +5,7 @@ var React = require('react');
5
5
var Router = require ( '../' ) ;
6
6
var Location = React . createFactory ( Router . Location ) ;
7
7
var NotFound = React . createFactory ( Router . NotFound ) ;
8
+ var URLPattern = require ( 'url-pattern' ) ;
8
9
9
10
describe ( 'matchRoutes' , function ( ) {
10
11
@@ -14,10 +15,19 @@ describe('matchRoutes', function() {
14
15
Location ( { path : '/mod/*' , handler : React . createElement ( 'div' , { name : 'mod' } ) } ) ,
15
16
Location ( { path : / \/ r e g e x \/ ( [ a - z A - Z ] * ) $ / , handler : React . createElement ( 'div' , { name : 'regex' } ) } ) ,
16
17
Location ( { path : / \/ ( .* ?) \/ ( \d ) \/ ( [ a - z A - Z ] * ) $ / , handler : React . createElement ( 'div' , { name : 'regexMatch' } ) ,
17
- matchKeys : [ 'name' , 'num' , 'text' ] } ) ,
18
+ matchKeys : [ 'name' , 'num' , 'text' ] } ) ,
18
19
NotFound ( { handler : React . createElement ( 'div' , { name : 'notfound' } ) } )
19
20
] ;
20
21
22
+ afterEach ( function ( ) {
23
+ // Remove any compiled regex patterns from all routes
24
+ routes . forEach ( function ( r ) {
25
+ r . props . pattern = null ;
26
+ } ) ;
27
+ // In case we overrode this, reset it.
28
+ Router . createURLPatternCompiler = function ( ) { return new URLPattern . Compiler ( ) ; } ;
29
+ } ) ;
30
+
21
31
it ( 'matches ""' , function ( ) {
22
32
var match = matchRoutes ( routes , '' ) ;
23
33
assert ( match . route ) ;
@@ -48,6 +58,62 @@ describe('matchRoutes', function() {
48
58
assert . strictEqual ( match . unmatchedPath , null ) ;
49
59
} ) ;
50
60
61
+ it ( 'fails to match /cat/:id with periods in param' , function ( ) {
62
+ var match = matchRoutes ( routes , '/cat/hello.with.periods' ) ;
63
+ assert ( match . route ) ;
64
+ assert . strictEqual ( match . route . props . handler . props . name , 'notfound' ) ;
65
+ assert . deepEqual ( match . match , null ) ;
66
+ assert . strictEqual ( match . path , '/cat/hello.with.periods' ) ;
67
+ assert . strictEqual ( match . matchedPath , '/cat/hello.with.periods' ) ;
68
+ assert . strictEqual ( match . unmatchedPath , null ) ;
69
+ } ) ;
70
+
71
+ it ( 'matches /cat/:id with a custom url-pattern compiler and periods in param' , function ( ) {
72
+ Router . createURLPatternCompiler = function ( ) {
73
+ var compiler = new URLPattern . Compiler ( ) ;
74
+ compiler . segmentValueCharset = 'a-zA-Z0-9_\\- %\\.' ;
75
+ return compiler ;
76
+ } ;
77
+ var match = matchRoutes ( routes , '/cat/hello.with.periods' ) ;
78
+ assert ( match . route ) ;
79
+ assert . strictEqual ( match . route . props . handler . props . name , 'cat' ) ;
80
+ assert . deepEqual ( match . match , { id : 'hello.with.periods' } ) ;
81
+ assert . strictEqual ( match . path , '/cat/hello.with.periods' ) ;
82
+ assert . strictEqual ( match . matchedPath , '/cat/hello.with.periods' ) ;
83
+ assert . strictEqual ( match . unmatchedPath , null ) ;
84
+ } ) ;
85
+
86
+ it ( 'matches a very custom url-pattern compiler config' , function ( ) {
87
+ var route = Location ( { path : '[http[s]!://][$sub_domain.]$domain.$toplevel-domain[/?]' ,
88
+ handler : React . createElement ( 'div' , { name : 'parseDomain' } ) } ) ;
89
+
90
+ // Lifted from url-pattern docs
91
+ Router . createURLPatternCompiler = function ( routeProps ) {
92
+ // Somebody might use this, make sure it works.
93
+ assert . strictEqual ( routeProps . path , route . props . path ) ;
94
+
95
+ // Create a very custom compiler.
96
+ var compiler = new URLPattern . Compiler ( ) ;
97
+ compiler . escapeChar = '!' ;
98
+ compiler . segmentNameStartChar = '$' ;
99
+ compiler . segmentNameCharset = 'a-zA-Z0-9_-' ;
100
+ compiler . segmentValueCharset = 'a-zA-Z0-9' ;
101
+ compiler . optionalSegmentStartChar = '[' ;
102
+ compiler . optionalSegmentEndChar = ']' ;
103
+ compiler . wildcardChar = '?' ;
104
+ return compiler ;
105
+ } ;
106
+
107
+ var match = matchRoutes ( [ route ] , 'https://www.github.com/strml/react-router-component' ) ;
108
+ assert ( match . route ) ;
109
+ assert . strictEqual ( match . route . props . handler . props . name , 'parseDomain' ) ;
110
+ assert . deepEqual ( match . match , { sub_domain : 'www' , domain : 'github' , 'toplevel-domain' : 'com' ,
111
+ _ : [ 'strml/react-router-component' ] } ) ;
112
+ assert . strictEqual ( match . path , 'https://www.github.com/strml/react-router-component' ) ;
113
+ assert . strictEqual ( match . matchedPath , 'https://www.github.com/' ) ;
114
+ assert . strictEqual ( match . unmatchedPath , 'strml/react-router-component' ) ;
115
+ } ) ;
116
+
51
117
it ( 'matches /mod/wow/here' , function ( ) {
52
118
var match = matchRoutes ( routes , '/mod/wow/here' ) ;
53
119
assert ( match . route ) ;
0 commit comments