Skip to content

Commit 8d92a45

Browse files
committed
test: add some route SSR tests
1 parent aec2722 commit 8d92a45

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/route/__story__/StoryRouteTruncateRoute.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Router, Route} from '..';
44

55
const StoryRouteTruncateRoute = () =>
66
<div>
7-
<Router route='/api/users/43f23f-23f34f43r.json'>
7+
<Router route='/api/users/123.json'>
88
<Route match='/api'>
99
<Route match='/users'>
1010
<Route match={/.*/}>{(result) =>
@@ -17,7 +17,7 @@ const StoryRouteTruncateRoute = () =>
1717
<hr />
1818

1919
<pre style={{fontFamily: 'monospace'}}>{`
20-
<Router route='/api/users/43f23f-23f34f43r.json'>
20+
<Router route='/api/users/132.json'>
2121
<Route match='/api'>
2222
<Route match='/users'>
2323
<Route match={/.*/}>{(result) =>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {createElement as h} from 'react';
2+
import {renderToStaticMarkup} from 'react-dom/server';
3+
import {expect} from 'chai';
4+
import {Router, Route} from '..';
5+
6+
describe('route SSR', () => {
7+
it('renders without crashing', () => {
8+
const html = renderToStaticMarkup(
9+
<Router route='/foo/bar'>
10+
<Route>
11+
<span>baz</span>
12+
</Route>
13+
</Router>
14+
);
15+
16+
expect(html).to.equal('<span>baz</span>');
17+
});
18+
19+
it('renders matching routes', () => {
20+
const html = renderToStaticMarkup(
21+
<Router route='/foo/bar'>
22+
<Route>
23+
<span>1</span>
24+
</Route>
25+
<Route match='/baz'>
26+
<span>2</span>
27+
</Route>
28+
<Route match='/foo'>
29+
<span>3</span>
30+
</Route>
31+
<Route match='/foo/bar'>
32+
<span>4</span>
33+
</Route>
34+
</Router>
35+
);
36+
37+
expect(html).to.equal('<div><span>1</span></div>');
38+
});
39+
40+
it('matches partial step', () => {
41+
const html = renderToStaticMarkup(
42+
<Router route='/foo/bar'>
43+
<Route match='/foo/b'>
44+
<span>1</span>
45+
</Route>
46+
</Router>
47+
);
48+
49+
expect(html).to.equal('<span>1</span>');
50+
});
51+
});

src/route/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class Router extends Component<IRouteProviderProps, any> {
1818
};
1919

2020
renderProvider (route) {
21+
const {children} = this.props;
2122
this.matches = 0;
2223

2324
const element = h(Provider, {
@@ -28,7 +29,7 @@ export class Router extends Component<IRouteProviderProps, any> {
2829
getMathces: () => this.matches,
2930
parent: this.props.parent
3031
}
31-
}, this.props.children);
32+
}, Array.isArray(children) ? h('div', null, children) : children);
3233

3334
return element;
3435
}
@@ -41,7 +42,7 @@ export class Router extends Component<IRouteProviderProps, any> {
4142
return this.renderProvider(route);
4243
}
4344

44-
return h(LocationSensor, {}, (value) => this.renderProvider(value.pathname));
45+
return h(LocationSensor, null, ({pathname}) => this.renderProvider(pathname));
4546
}
4647
}
4748

0 commit comments

Comments
 (0)