Guest User

Untitled

a guest
Nov 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. constructor(props) {
  2. super(props);
  3. this.state = {
  4. username: '',
  5. password: ''
  6. };
  7. this.handleUserNameChange = this.handleUserNameChange.bind(this);
  8. this.handlePasswordChange = this.handlePasswordChange.bind(this);
  9. this.onSubmit = this.onSubmit.bind(this);
  10. }
  11.  
  12. handleUserNameChange(event) {
  13. this.setState({
  14. username: event.target.value
  15. });
  16. }
  17.  
  18. handlePasswordChange(event) {
  19. this.setState({
  20. password: event.target.value
  21. });
  22. }
  23.  
  24. const LoginForm = ({username, password, handleLogin, handleUserNameChange, handlePasswordChange, user_error, loading, loading_color}) => {
  25. return (
  26. <form name="form" onSubmit={handleLogin}>
  27. <h1> Login Form </h1>
  28. <Field
  29. name="user_name"
  30. type="text"
  31. label="User Name"
  32. component={renderUserComponent}
  33. onChange={handleUserNameChange}
  34. value={username}
  35. /><br />
  36. <Field
  37. name="password"
  38. type="password"
  39. label="Password"
  40. component={renderUserComponent}
  41. onChange={handlePasswordChange}
  42. value={password}
  43. /><br />
  44. {renderSubmit({user_error, loading, handleLogin, loading_color})}
  45. <br />
  46. </form>
  47. );
  48. }
  49.  
  50. describe("LoginForm", () => {
  51. let state={
  52. username: '',
  53. password: ''
  54. }
  55. const defaultProps = {
  56. username: '',
  57. password: '',
  58. handleLogin: () => {},
  59. handleUserNameChange: (event) => {
  60. state.setState({
  61. username: event.target.value
  62. })
  63. },
  64. handlePasswordChange: (event) => {
  65. state.setState({
  66. password: event.target.value
  67. })
  68. }
  69. };
  70. const handleLogin = sinon.spy();
  71. const handleUserNameChange = sinon.spy();
  72. const handlePasswordChange = sinon.spy();
  73. let loginForm = mount(
  74. <MuiThemeProvider muiTheme={getMuiTheme()}>
  75. <Provider store={store}>
  76. <LoginForm {...defaultProps} handleLogin={handleLogin} handleUserNameChange={handleUserNameChange} handlePasswordChange={handlePasswordChange}/>
  77. </Provider>
  78. </MuiThemeProvider>
  79. );
  80.  
  81. describe("when the redux form was submitted", () => {
  82. it("the handleLogin function would be called once", () => {
  83. const button = loginForm.find("FlatButton");
  84. button.simulate('submit');
  85. expect(handleLogin.called).toBe(true);
  86. });
  87. });
  88.  
  89. describe("when the username was changed", () => {
  90. it("the handleUserNameChange function would be called once, and the state would be changed", () => {
  91. const username_field = loginForm.find("Field").first();
  92. username_field.simulate('keyDown', { keyCode: 40});
  93. username_field.simulate('change', { keyCode: 40});
  94. expect(handleUserNameChange.called).toBe(true);
  95. });
  96. });
  97. });
Add Comment
Please, Sign In to add comment