1
1
require ( './helper' ) ;
2
- var Route = require ( '../modules/Route' ) ;
3
- var RouteComponent = require ( '../modules/components/Route' ) ;
2
+ var Route = require ( '../modules/components/Route' ) ;
4
3
5
4
var App = React . createClass ( {
6
5
displayName : 'App' ,
@@ -9,53 +8,129 @@ var App = React.createClass({
9
8
}
10
9
} ) ;
11
10
12
- describe ( 'a Route with a named handler' , function ( ) {
13
- var route ;
14
- beforeEach ( function ( ) {
15
- route = new Route ( { handler : App } ) ;
16
- } ) ;
11
+ describe ( 'a Route that matches the URL' , function ( ) {
12
+ it ( 'returns an array' , function ( ) {
13
+ var route = Route ( { path : '/a/b/c' , handler : App } ) ;
14
+
15
+ var matches = route . match ( '/a/b/c' ) ;
16
+ assert ( matches ) ;
17
+ expect ( matches . length ) . toEqual ( 1 ) ;
17
18
18
- it ( 'has the correct displayName' , function ( ) {
19
- expect ( route . displayName ) . toEqual ( 'AppRoute' ) ;
19
+ var rootMatch = getRootMatch ( matches ) ;
20
+ expect ( rootMatch . params ) . toEqual ( { } ) ;
20
21
} ) ;
21
22
22
- it ( 'has the correct toString representation' , function ( ) {
23
- expect ( route + '' ) . toEqual ( '<AppRoute>' ) ;
23
+ describe ( 'that contains dynamic segments' , function ( ) {
24
+ it ( 'returns an array with the correct params' , function ( ) {
25
+ var route = Route ( { path : '/posts/:id/edit' , handler : App } ) ;
26
+
27
+ var matches = route . match ( '/posts/abc/edit' ) ;
28
+ assert ( matches ) ;
29
+ expect ( matches . length ) . toEqual ( 1 ) ;
30
+
31
+ var rootMatch = getRootMatch ( matches ) ;
32
+ expect ( rootMatch . params ) . toEqual ( { id : 'abc' } ) ;
33
+ } ) ;
24
34
} ) ;
25
35
} ) ;
26
36
27
- describe ( 'a Route without a handler component' , function ( ) {
28
- it ( 'throws an Error' , function ( ) {
29
- expect ( function ( ) {
30
- new Route ( { handler : null } ) ;
31
- } ) . toThrow ( Error ) ;
37
+ describe ( 'a Route that does not match the URL' , function ( ) {
38
+ it ( 'returns null' , function ( ) {
39
+ var route = Route ( { path : '/a/b/c' , handler : App } ) ;
40
+ expect ( route . match ( '/not-found' ) ) . toBe ( null ) ;
32
41
} ) ;
33
42
} ) ;
34
43
35
- describe ( 'a Route with no path' , function ( ) {
36
- describe ( 'or a name' , function ( ) {
37
- it ( 'uses / as its path' , function ( ) {
38
- var route = new Route ( { handler : App } ) ;
39
- expect ( route . path ) . toEqual ( '/' ) ;
40
- } ) ;
44
+ describe ( 'a nested Route that matches the URL' , function ( ) {
45
+ it ( 'returns the appropriate params for each match' , function ( ) {
46
+ var route = Route ( { name : 'posts' , path : '/posts/:id' , handler : App } ,
47
+ Route ( { name : 'comment' , path : '/posts/:id/comments/:commentId' , handler : App } )
48
+ ) ;
49
+
50
+ var matches = route . match ( '/posts/abc/comments/123' ) ;
51
+ assert ( matches ) ;
52
+ expect ( matches . length ) . toEqual ( 2 ) ;
53
+
54
+ var rootMatch = getRootMatch ( matches ) ;
55
+ expect ( rootMatch . route . props . name ) . toEqual ( 'comment' ) ;
56
+ expect ( rootMatch . params ) . toEqual ( { id : 'abc' , commentId : '123' } ) ;
57
+
58
+ var firstMatch = matches [ 0 ] ;
59
+ expect ( firstMatch . route . props . name ) . toEqual ( 'posts' ) ;
60
+ expect ( firstMatch . params ) . toEqual ( { id : 'abc' } ) ;
41
61
} ) ;
62
+ } ) ;
42
63
43
- describe ( 'and a name' , function ( ) {
44
- it ( 'uses its name as its path' , function ( ) {
45
- var route = new Route ( { name : 'users' , handler : App } ) ;
46
- expect ( route . path ) . toEqual ( '/users' ) ;
47
- } ) ;
64
+ describe ( 'multiple nested Router that match the URL' , function ( ) {
65
+ it ( 'returns the first one in the subtree, depth-first' , function ( ) {
66
+ var route = Route ( { path : '/' , handler : App } ,
67
+ Route ( { path : '/a' , handler : App } ,
68
+ Route ( { path : '/a/b' , name : 'expected' , handler : App } )
69
+ ) ,
70
+ Route ( { path : '/a/b' , handler : App } )
71
+ ) ;
72
+
73
+ var matches = route . match ( '/a/b' ) ;
74
+ assert ( matches ) ;
75
+ expect ( matches . length ) . toEqual ( 3 ) ;
76
+
77
+ var rootMatch = getRootMatch ( matches ) ;
78
+ expect ( rootMatch . route . props . name ) . toEqual ( 'expected' ) ;
48
79
} ) ;
49
80
} ) ;
50
81
51
- describe ( 'a Route that is missing a parameter that its parent Route needs' , function ( ) {
52
- it ( 'throws an Error' , function ( ) {
53
- expect ( function ( ) {
54
- Route . fromComponent (
55
- RouteComponent ( { path : '/users/:userId' , handler : App } ,
56
- RouteComponent ( { path : '/users/:id/comments' , handler : App } )
57
- )
58
- ) ;
59
- } ) . toThrow ( / m i s s i n g t h e " u s e r I d " p a r a m e t e r / ) ;
82
+ describe ( 'a Route with custom props' , function ( ) {
83
+ it ( 'receives props' , function ( done ) {
84
+ var route = Route ( { handler : App , customProp : 'prop' } ) ;
85
+ var component = TestUtils . renderIntoDocument ( route ) ;
86
+
87
+ route . dispatch ( '/' ) . then ( function ( ) {
88
+ assert ( component . props . customProp ) ;
89
+ expect ( component . props . customProp ) . toEqual ( 'prop' ) ;
90
+ done ( ) ;
91
+ } ) ;
60
92
} ) ;
61
93
} ) ;
94
+
95
+ // describe('a Router that renders on the server', function() {
96
+ // it('works with async willTransitionTo()', function(done) {
97
+ // var dataStore = 'goodbye';
98
+ // var Layout = React.createClass({
99
+ // render: function() {
100
+ // return React.DOM.article(null, this.props.activeRoute);
101
+ // }
102
+ // });
103
+ // var AsyncApp = React.createClass({
104
+ // displayName: 'AsyncApp',
105
+ // statics: {
106
+ // willTransitionTo: function() {
107
+ // return new Promise(function(resolve) {
108
+ // setTimeout(function() {
109
+ // dataStore = 'hello';
110
+ // resolve();
111
+ // }, 0);
112
+ // });
113
+ // }
114
+ // },
115
+ // render: function() {
116
+ // return React.DOM.div(null, dataStore);
117
+ // }
118
+ // });
119
+
120
+ // var router = Router(
121
+ // RouteComponent({ path: '/', handler: Layout},
122
+ // RouteComponent({ path: '/a', handler: AsyncApp }))
123
+ // );
124
+
125
+ // router.renderComponentToString('/a').then(function(result) {
126
+ // expect(result.indexOf('div') > -1).toBe(true);
127
+ // expect(result.indexOf('hello') > -1).toBe(true);
128
+
129
+ // done();
130
+ // });
131
+ // });
132
+ // });
133
+
134
+ function getRootMatch ( matches ) {
135
+ return matches [ matches . length - 1 ] ;
136
+ }
0 commit comments