form.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import Form from 'misago/components/form';
  4. import { email, minLength } from 'misago/utils/validators'; // jshint ignore:line
  5. import * as testUtils from 'misago/utils/test-utils';
  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 = testUtils.render(<TestForm />);
  36. /* jshint ignore:end */
  37. });
  38. afterEach(function() {
  39. testUtils.unmountComponents();
  40. });
  41. it("validates individual field", function() {
  42. assert.deepEqual(
  43. form.validateField('requiredField', ''), ["This field is required."],
  44. "empty value returned error");
  45. assert.equal(form.validateField('requiredField', 'required'), null,
  46. "non-empty value returned no errors");
  47. assert.deepEqual(
  48. form.validateField('validatedField', 'lorem'),
  49. ["That ain't valid e-mail!"],
  50. "invalid value returned error from validator");
  51. assert.deepEqual(
  52. form.validateField('validatedField', 'lorem@ipsum.com'), null,
  53. "valid value returned no errors from validation");
  54. });
  55. it("yields errors on empty fields", function(done) {
  56. form.forceUpdate(function() {
  57. assert.deepEqual(form.validate(), {
  58. 'requiredField': ["This field is required."],
  59. 'validatedField': ["This field is required."]
  60. }, "both required inputs failed to pass initial validation");
  61. done();
  62. });
  63. });
  64. it("yields errors on invalid fields", function(done) {
  65. form.setState({
  66. 'requiredField': "Its okay!",
  67. 'validatedField': "Lorem ipsumd dolor met."
  68. });
  69. form.forceUpdate(function() {
  70. assert.deepEqual(form.validate(), {
  71. 'requiredField': null,
  72. 'validatedField': ["That ain't valid e-mail!"]
  73. }, "invalid field failed to pass validation");
  74. done();
  75. });
  76. });
  77. it("yields errors on invalid optional fields", function(done) {
  78. form.setState({
  79. 'requiredField': "Its okay!",
  80. 'validatedField': "Lorem ipsumd dolor met.",
  81. 'optionalField': "sho"
  82. });
  83. form.forceUpdate(function() {
  84. assert.deepEqual(form.validate(), {
  85. 'requiredField': null,
  86. 'validatedField': ["That ain't valid e-mail!"],
  87. 'optionalField': [
  88. "Ensure this value has at least 4 characters (it has 3)."
  89. ]
  90. }, "invalid optional field failed to pass validation");
  91. done();
  92. });
  93. });
  94. it("passes valid form", function(done) {
  95. form.setState({
  96. 'requiredField': "Its okay!",
  97. 'validatedField': "lorem@ipsum.com",
  98. 'optionalField': "Lorem ipsum dolor long!"
  99. });
  100. form.forceUpdate(function() {
  101. assert.deepEqual(form.validate(), {
  102. 'requiredField': null,
  103. 'validatedField': null,
  104. 'optionalField': null
  105. }, "valid fields passed validation");
  106. done();
  107. });
  108. });
  109. it("binds fields", function(done) {
  110. form.bindInput('requiredField')({target: {value: "It's okay!"}});
  111. form.bindInput('validatedField')({target: {value: "Not a e-mail!"}});
  112. form.forceUpdate(function() {
  113. assert.deepEqual(form.state.errors, {
  114. 'requiredField': null,
  115. 'validatedField': ["That ain't valid e-mail!"]
  116. }, "invalid field failed to pass validation");
  117. done();
  118. });
  119. });
  120. });