Skip to content

Instantly share code, notes, and snippets.

@benhowdle89
Created July 31, 2015 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhowdle89/3e1cfd863e5e920b79ae to your computer and use it in GitHub Desktop.
Save benhowdle89/3e1cfd863e5e920b79ae to your computer and use it in GitHub Desktop.
import React from 'react';
import AuthModel from './../../models/auth';
const Login = React.createClass({
getInitialState() {
return {
username: this.fetchUsername(),
errored: false
};
},
componentWillMount() {
this.authModel = AuthModel; // AuthModel is a singleton
},
handleSubmit(event) {
event.preventDefault();
this.login();
},
login() {
let typedUsername = React.findDOMNode(this.refs.username).value;
if(!typedUsername){
return this.setState({
errored: true
});
}
// we don't actually send the request from here, but set the username on the AuthModel and call the
// `login` method below
this.authModel.set('username', typedUsername);
this.authModel.login();
}
});
import React from 'react/addons';
import sinon from 'sinon';
import Login from './../../../assets/js/react_components/auth/login.jsx';
const TestUtils = React.addons.TestUtils;
describe('login', () => {
let LoginElement = TestUtils.renderIntoDocument(<Login />),
form = React.findDOMNode(LoginElement.refs.loginForm),
authModelStub = sinon.stub(LoginElement.authModel, 'login');
afterEach(() => {
LoginElement.state.errored = false;
});
it('doesn\'t call the login method if there\'s no username', () => {
TestUtils.Simulate.submit(form);
expect(LoginElement.state.errored).toEqual(true);
});
it('calls login when there\'s a username present', () => {
React.findDOMNode(LoginElement.refs.username).value = 'foo';
TestUtils.Simulate.submit(form);
expect(authModelStub.calledOnce).toEqual(true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment