|
| 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