form.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import ReactDOM from 'react-dom'; // jshint ignore:line
  4. import Form from 'misago/components/form';
  5. import { email, minLength } from 'misago/utils/validators'; // jshint ignore:line
  6. var form = null;
  7. class TestForm extends Form { // jshint ignore:line
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. 'isLoading': false,
  12. 'requiredField': '',
  13. 'validatedField': '',
  14. 'optionalField': '',
  15. validators: {
  16. required: {
  17. 'requiredField': [],
  18. 'validatedField': [email("That ain't valid e-mail!")]
  19. },
  20. optional: {
  21. 'optionalField': [minLength(4)]
  22. }
  23. }
  24. };
  25. }
  26. render() {
  27. /* jshint ignore:start */
  28. return <p>No need</p>;
  29. /* jshint ignore:end */
  30. }
  31. }
  32. describe("Form", function() {
  33. beforeEach(function() {
  34. /* jshint ignore:start */
  35. form = ReactDOM.render(
  36. <TestForm />,
  37. document.getElementById('test-mount')
  38. );
  39. /* jshint ignore:end */
  40. });
  41. afterEach(function() {
  42. window.emptyTestContainers();
  43. });
  44. it("validates individual field", function() {
  45. assert.deepEqual(
  46. form.validateField('requiredField', ''), ["This field is required."],
  47. "empty value returned error");
  48. assert.equal(form.validateField('requiredField', 'required'), null,
  49. "non-empty value returned no errors");
  50. assert.deepEqual(
  51. form.validateField('validatedField', 'lorem'),
  52. ["That ain't valid e-mail!"],
  53. "invalid value returned error from validator");
  54. assert.deepEqual(
  55. form.validateField('validatedField', 'lorem@ipsum.com'), null,
  56. "valid value returned no errors from validation");
  57. });
  58. it("yields errors on empty fields", function(done) {
  59. form.forceUpdate(function() {
  60. assert.deepEqual(form.validate(), {
  61. 'requiredField': ["This field is required."],
  62. 'validatedField': ["This field is required."]
  63. }, "both required inputs failed to pass initial validation");
  64. done();
  65. });
  66. });
  67. it("yields errors on invalid fields", function(done) {
  68. form.setState({
  69. 'requiredField': "Its okay!",
  70. 'validatedField': "Lorem ipsumd dolor met."
  71. });
  72. form.forceUpdate(function() {
  73. assert.deepEqual(form.validate(), {
  74. 'requiredField': null,
  75. 'validatedField': ["That ain't valid e-mail!"]
  76. }, "invalid field failed to pass validation");
  77. done();
  78. });
  79. });
  80. it("yields errors on invalid optional fields", function(done) {
  81. form.setState({
  82. 'requiredField': "Its okay!",
  83. 'validatedField': "Lorem ipsumd dolor met.",
  84. 'optionalField': "sho"
  85. });
  86. form.forceUpdate(function() {
  87. assert.deepEqual(form.validate(), {
  88. 'requiredField': null,
  89. 'validatedField': ["That ain't valid e-mail!"],
  90. 'optionalField': [
  91. "Ensure this value has at least 4 characters (it has 3)."
  92. ]
  93. }, "invalid optional field failed to pass validation");
  94. done();
  95. });
  96. });
  97. it("passes valid form", function(done) {
  98. form.setState({
  99. 'requiredField': "Its okay!",
  100. 'validatedField': "lorem@ipsum.com",
  101. 'optionalField': "Lorem ipsum dolor long!"
  102. });
  103. form.forceUpdate(function() {
  104. assert.deepEqual(form.validate(), {
  105. 'requiredField': null,
  106. 'validatedField': null,
  107. 'optionalField': null
  108. }, "valid fields passed validation");
  109. done();
  110. });
  111. });
  112. it("binds fields", function(done) {
  113. form.bindInput('requiredField')({target: {value: "It's okay!"}});
  114. form.bindInput('validatedField')({target: {value: "Not a e-mail!"}});
  115. form.forceUpdate(function() {
  116. assert.deepEqual(form.state.errors, {
  117. 'requiredField': null,
  118. 'validatedField': ["That ain't valid e-mail!"]
  119. }, "invalid field failed to pass validation");
  120. done();
  121. });
  122. });
  123. });