Skip to content

Commit b9a58ef

Browse files
committed
create some tests
1 parent 0bfeb7a commit b9a58ef

4 files changed

Lines changed: 106 additions & 3 deletions

File tree

example/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
<div id="app"></div>
4040

41-
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.0/react.min.js"></script>
41+
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.0/react.js"></script>
4242
<script src="../react-search-input.js"></script>
4343
<script>
4444
var App = React.createClass({

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@
66
"dependencies": {
77
"react": "^0.12.0"
88
},
9-
"scripts": {},
9+
"devDependencies": {
10+
"jsdom": "^3.0.2",
11+
"coveralls": "^2.11.2",
12+
"mocha": "^2.1.0",
13+
"istanbul": "^0.3.5"
14+
},
15+
"scripts": {
16+
"test": "mocha",
17+
"coverage": "./node_modules/istanbul/lib/cli.js cover --print detail ./node_modules/mocha/bin/_mocha -- --ui bdd -R spec"
18+
},
1019
"repository": {
1120
"type": "git",
1221
"url": "https://github.com/enki-com/react-search-input"

react-search-input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636

3737
updateSearch: function(e) {
38-
var searchTerm = e.currentTarget.value.toLowerCase();
38+
var searchTerm = e.target.value.toLowerCase();
3939
this.setState({
4040
searchTerm: searchTerm
4141
}, function() {

test/index.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
global.document = require("jsdom").jsdom("");
2+
global.window = document.parentWindow;
3+
global.navigator = window.navigator;
4+
5+
var assert = require("assert");
6+
7+
var React = require("react/addons")
8+
, TestUtils = React.addons.TestUtils;
9+
10+
var SearchInput = require("../react-search-input");
11+
12+
13+
var randomString = function () {
14+
return Math.random().toString(36).substring(7);
15+
};
16+
17+
var TestComponent = React.createClass({
18+
19+
getInitialState: function () {
20+
return { searchTerm: '' }
21+
}
22+
23+
, searchInput: function () {
24+
return this.refs.search;
25+
}
26+
27+
, getSearchTerm: function () {
28+
return this.state.searchTerm;
29+
}
30+
31+
, searchUpdated: function(term) {
32+
this.setState({searchTerm: term}); // needed to force re-render
33+
}
34+
35+
, render: function () {
36+
return React.createElement(SearchInput, React.__spread({}, {
37+
ref: "search"
38+
, onChange: this.searchUpdated
39+
}, this.props));
40+
}
41+
});
42+
43+
describe("SearchInput", function () {
44+
var createSearchInput = function (props) {
45+
var component = TestUtils.renderIntoDocument(React.createElement(TestComponent, props));
46+
return component;
47+
};
48+
49+
var changeSearchTerm = function (searchInput, term, blur) {
50+
var input = TestUtils.findRenderedDOMComponentWithTag(searchInput, "input");
51+
52+
53+
TestUtils.Simulate.click(input);
54+
TestUtils.Simulate.change(input, { target: { value: term } });
55+
56+
return input;
57+
};
58+
59+
describe("basic", function () {
60+
it("should change search term", function () {
61+
var container = createSearchInput();
62+
var searchinput = container.searchInput();
63+
var term = randomString();
64+
65+
changeSearchTerm(searchinput, term);
66+
67+
var searchTerm = searchinput.state.searchTerm;
68+
69+
assert.equal(searchTerm, term, "the search term should have been changed");
70+
});
71+
72+
it("should change call the props onChange", function () {
73+
var container = createSearchInput();
74+
var searchinput = container.searchInput();
75+
var term = randomString();
76+
77+
changeSearchTerm(searchinput, term);
78+
79+
var searchTerm = container.getSearchTerm();
80+
81+
assert.equal(searchTerm, term, "the search term should have been changed");
82+
});
83+
});
84+
85+
describe("props", function () {
86+
it("should test className", function () {
87+
var searchInput = createSearchInput({
88+
className: "test"
89+
}).searchInput();
90+
91+
assert.equal(searchInput.getDOMNode().className, "search-input test", "there should be a new class");
92+
});
93+
});
94+
});

0 commit comments

Comments
 (0)