1
1
import expect from 'expect' ;
2
- import { getPathname , getQueryString } from '../URLUtils' ;
2
+ import { getPathname , getQueryString , formatPattern } from '../URLUtils' ;
3
3
4
4
describe ( 'getPathname' , function ( ) {
5
5
it ( 'returns the pathname portion of a path' , function ( ) {
@@ -16,3 +16,101 @@ describe('getQueryString', function () {
16
16
describe ( 'matchPattern' , function ( ) {
17
17
it ( 'ignores trailing slashes' ) ;
18
18
} ) ;
19
+
20
+ describe ( 'formatPattern' , function ( ) {
21
+ describe ( 'when a pattern does not have dynamic segments' , function ( ) {
22
+ var pattern = 'a/b/c' ;
23
+
24
+ it ( 'returns the pattern' , function ( ) {
25
+ expect ( formatPattern ( pattern , { } ) ) . toEqual ( pattern ) ;
26
+ } ) ;
27
+ } ) ;
28
+
29
+ describe ( 'when a pattern has dynamic segments' , function ( ) {
30
+ var pattern = 'comments/:id/edit' ;
31
+
32
+ describe ( 'and a param is missing' , function ( ) {
33
+ it ( 'throws an Error' , function ( ) {
34
+ expect ( function ( ) {
35
+ formatPattern ( pattern , { } ) ;
36
+ } ) . toThrow ( Error ) ;
37
+ } ) ;
38
+ } ) ;
39
+
40
+ describe ( 'and a param is optional' , function ( ) {
41
+ var pattern = 'comments/(:id)/edit' ;
42
+
43
+ it ( 'returns the correct path when param is supplied' , function ( ) {
44
+ expect ( formatPattern ( pattern , { id :'123' } ) ) . toEqual ( 'comments/123/edit' ) ;
45
+ } ) ;
46
+
47
+ it ( 'returns the correct path when param is not supplied' , function ( ) {
48
+ expect ( formatPattern ( pattern , { } ) ) . toEqual ( 'comments/edit' ) ;
49
+ } ) ;
50
+ } ) ;
51
+
52
+ describe ( 'and a param and forward slash are optional' , function ( ) {
53
+ var pattern = 'comments(/:id)/edit' ;
54
+
55
+ it ( 'returns the correct path when param is supplied' , function ( ) {
56
+ expect ( formatPattern ( pattern , { id :'123' } ) ) . toEqual ( 'comments/123/edit' ) ;
57
+ } ) ;
58
+
59
+ it ( 'returns the correct path when param is not supplied' , function ( ) {
60
+ expect ( formatPattern ( pattern , { } ) ) . toEqual ( 'comments/edit' ) ;
61
+ } ) ;
62
+ } ) ;
63
+
64
+ describe ( 'and all params are present' , function ( ) {
65
+ it ( 'returns the correct path' , function ( ) {
66
+ expect ( formatPattern ( pattern , { id : 'abc' } ) ) . toEqual ( 'comments/abc/edit' ) ;
67
+ } ) ;
68
+
69
+ it ( 'returns the correct path when the value is 0' , function ( ) {
70
+ expect ( formatPattern ( pattern , { id : 0 } ) ) . toEqual ( 'comments/0/edit' ) ;
71
+ } ) ;
72
+ } ) ;
73
+
74
+ describe ( 'and some params have special URL encoding' , function ( ) {
75
+ it ( 'returns the correct path' , function ( ) {
76
+ expect ( formatPattern ( pattern , { id : 'one, two' } ) ) . toEqual ( 'comments/one, two/edit' ) ;
77
+ } ) ;
78
+ } ) ;
79
+
80
+ describe ( 'and a param has a forward slash' , function ( ) {
81
+ it ( 'preserves the forward slash' , function ( ) {
82
+ expect ( formatPattern ( pattern , { id : 'the/id' } ) ) . toEqual ( 'comments/the/id/edit' ) ;
83
+ } ) ;
84
+ } ) ;
85
+
86
+ describe ( 'and some params contain dots' , function ( ) {
87
+ it ( 'returns the correct path' , function ( ) {
88
+ expect ( formatPattern ( pattern , { id : 'alt.black.helicopter' } ) ) . toEqual ( 'comments/alt.black.helicopter/edit' ) ;
89
+ } ) ;
90
+ } ) ;
91
+ } ) ;
92
+
93
+ describe ( 'when a pattern has one splat' , function ( ) {
94
+ it ( 'returns the correct path' , function ( ) {
95
+ expect ( formatPattern ( '/a/*/d' , { splat : 'b/c' } ) ) . toEqual ( '/a/b/c/d' ) ;
96
+ } ) ;
97
+ } ) ;
98
+
99
+ describe ( 'when a pattern has multiple splats' , function ( ) {
100
+ it ( 'returns the correct path' , function ( ) {
101
+ expect ( formatPattern ( '/a/*/c/*' , { splat : [ 'b' , 'd' ] } ) ) . toEqual ( '/a/b/c/d' ) ;
102
+ } ) ;
103
+
104
+ it ( 'complains if not given enough splat values' , function ( ) {
105
+ expect ( function ( ) {
106
+ formatPattern ( '/a/*/c/*' , { splat : [ 'b' ] } ) ;
107
+ } ) . toThrow ( Error ) ;
108
+ } ) ;
109
+ } ) ;
110
+
111
+ describe ( 'when a pattern has dots' , function ( ) {
112
+ it ( 'returns the correct path' , function ( ) {
113
+ expect ( formatPattern ( '/foo.bar.baz' ) ) . toEqual ( '/foo.bar.baz' ) ;
114
+ } ) ;
115
+ } ) ;
116
+ } ) ;
0 commit comments