Skip to content
This repository was archived by the owner on Jun 6, 2022. It is now read-only.

Commit 145f65c

Browse files
committed
Add tests, remove examples
1 parent 68df0fc commit 145f65c

File tree

8 files changed

+100
-66
lines changed

8 files changed

+100
-66
lines changed

.jshintrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"exports": false,
1414
"console": false,
1515
"document": false,
16-
"setTimeout": false,
17-
"__callback": false
16+
"setTimeout": false
1817
}
1918
}

.zuul.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ui: mocha-bdd
2+
browsers:
3+
- name: chrome
4+
version: 27..latest
5+
- name: ie
6+
version: 10..latest
7+
- name: safari
8+
version: latest
9+
- name: firefox
10+
version: 24..latest
11+
- name: iphone
12+
version: 6..latest
13+
- name: ipad
14+
version: 6..latest

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ install link:
66
@npm $@
77

88
lint:
9-
@$(BIN)/jshint *.js
9+
@$(BIN)/jshint index.js
1010

1111
test:
12-
@$(BIN)/mocha -b -R spec specs/*.js
12+
@echo 'nothing' > /dev/null
13+
14+
test-cloud:
15+
@$(BIN)/zuul -- tests.js
1316

1417
release-patch: test lint
1518
@$(call release,patch)

examples/example.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

examples/example.jsx

Lines changed: 0 additions & 32 deletions
This file was deleted.

index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ var pattern = require('url-pattern');
55

66
function createRouter(component) {
77
return React.createClass({
8+
9+
navigate: function(path, cb) {
10+
window.history.pushState({}, '', path);
11+
this.setState({path: window.location.pathname}, cb);
12+
},
13+
814
getInitialState: function() {
915
return {
1016
path: this.props.path || window.location.pathname
@@ -20,16 +26,21 @@ function createRouter(component) {
2026
},
2127

2228
onPopState: function(e) {
23-
e.preventDefault();
24-
this.setState({location: window.location.pathname});
29+
var path = window.location.pathname;
30+
31+
if (this.state.path !== path) {
32+
this.setState({path: path});
33+
}
2534
},
2635

2736
render: function() {
2837
var match, page, notFound;
2938
var len, i;
3039

31-
for (i = 0, len = this.props.children.length; i < len; i++) {
32-
var current = this.props.children[i];
40+
this.children = this.children || this.props.children;
41+
42+
for (i = 0, len = this.children.length; i < len; i++) {
43+
var current = this.children[i];
3344

3445
if (process.env.NODE_ENV !== "production") {
3546
invariant(
@@ -55,7 +66,7 @@ function createRouter(component) {
5566
notFound ? notFound.children :
5667
[];
5768

58-
return component(this.props, children(this.state, match));
69+
return this.transferPropsTo(component(null, children(match)));
5970
}
6071
});
6172
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"react": "~0.8.0",
1111
"semver": "~2.2.1",
1212
"mocha": "~1.17.1",
13-
"jshint": "~2.4.3"
13+
"jshint": "~2.4.3",
14+
"zuul": "~1.5.2"
1415
},
1516
"scripts": {
1617
"test": "make test"

tests.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var assert = require('assert');
2+
var React = require('react');
3+
var Router = require('./index');
4+
5+
var App = React.createClass({
6+
7+
render: function() {
8+
return Router.Locations({ref: 'router'},
9+
Router.Location({path: '/__zuul'}, function(props) { return 'mainpage' }),
10+
Router.Location({path: '/__zuul/:slug'}, function(props) { return props.slug })
11+
);
12+
}
13+
});
14+
15+
function getText(node) {
16+
return node.textContent || node.innerText;
17+
}
18+
19+
describe('react-router-component', function() {
20+
21+
var host, app, router;
22+
23+
beforeEach(function() {
24+
host = document.createElement('div');
25+
document.body.appendChild(host);
26+
app = React.renderComponent(App(), host);
27+
router = app.refs.router;
28+
});
29+
30+
afterEach(function() {
31+
React.unmountComponentAtNode(host);
32+
document.body.removeChild(host);
33+
host = null;
34+
app = null;
35+
router = null
36+
});
37+
38+
it('renders', function() {
39+
assert.equal(getText(host), 'mainpage');
40+
});
41+
42+
it('navigates to a different route', function(done) {
43+
assert.equal(getText(host), 'mainpage');
44+
router.navigate('/__zuul/hello', function() {
45+
assert.equal(getText(host), 'hello');
46+
history.back();
47+
setTimeout(done, 200);
48+
});
49+
});
50+
51+
it('handles "popstate" event', function(done) {
52+
assert.equal(getText(host), 'mainpage');
53+
router.navigate('/__zuul/hello', function() {
54+
assert.equal(getText(host), 'hello');
55+
history.back();
56+
setTimeout(function() {
57+
assert.equal(getText(host), 'mainpage');
58+
done();
59+
}, 200);
60+
});
61+
});
62+
});

0 commit comments

Comments
 (0)