Created
June 5, 2014 06:09
-
-
Save ryanflorence/bba285c1d39b23f42306 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Toggler = require('./toggler'); | |
var React = require('react/addons'); | |
var assert = chai.assert; | |
var Simulate = React.addons.TestUtils.Simulate | |
describe('Toggler', function(){ | |
var toggler, el; | |
beforeEach(function() { | |
toggler = render(Toggler({buttonText: 'toggle'}, 'ohai!')); | |
el = toggler.getDOMNode(); | |
}); | |
afterEach(function() { | |
teardownComponent(toggler); | |
}); | |
it('renders correctly', function(){ | |
assert.equal(toggler.refs.button.getDOMNode().textContent, 'toggle'); | |
assert.equal(toggler.refs.content.getDOMNode().textContent, 'ohai!'); | |
}); | |
it('toggles the content when the button is clicked', function() { | |
assert.ok(toggler.getDOMNode().textContent.match('ohai!')); | |
Simulate.click(toggler.refs.button.getDOMNode()); | |
assert.ok(!toggler.getDOMNode().textContent.match('ohai!')); | |
}); | |
}); | |
function render(component) { | |
var container = document.createElement('div'); | |
document.body.appendChild(container); | |
React.renderComponent(component, container); | |
return component; | |
} | |
function teardownComponent(component) { | |
React.unmountComponentAtNode(component.getDOMNode().parentNode); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var React = require('react'); | |
var div = React.DOM.div; | |
var button = React.DOM.button; | |
var Toggler = React.createClass({ | |
getInitialState: function() { | |
return { isOpen: true }; | |
}, | |
toggle: function() { | |
this.setState({isOpen: !this.state.isOpen}); | |
}, | |
render: function() { | |
var content = this.state.isOpen ? this.props.children : null; | |
return div({}, | |
button({ref: 'button', onClick: this.toggle}, this.props.buttonText), | |
div({ref: 'content'}, content) | |
); | |
} | |
}); | |
module.exports = Toggler; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rpflorence The database analogy is great! That's an interesting point of view, Ryan. Thank you :)