onChange.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { Form, Select, Button } from '../../../index';
  2. import { noop } from 'lodash';
  3. import { BASE_CLASS_PREFIX } from '../../../../semi-foundation/base/constants';
  4. function getForm(props) {
  5. return mount(<Form {...props}></Form>, {
  6. attachTo: document.getElementById('container'),
  7. });
  8. }
  9. const Option = Select.Option;
  10. const FormSelect = (
  11. <Form.Select label="business" field="business" style={{ width: 200 }}>
  12. <Option value="abc">Abc</Option>
  13. <Option value="hotsoon">Hotsoon</Option>
  14. <Option value="topbuzz">TopBuzz</Option>
  15. </Form.Select>
  16. );
  17. const FormInput = <Form.Input field="name" />;
  18. const FieldCls = `.${BASE_CLASS_PREFIX}-form-field`;
  19. const fields = (
  20. <>
  21. {FormInput}
  22. {FormSelect}
  23. </>
  24. );
  25. describe('Form-onChange', () => {
  26. // beforeEach(() => {
  27. // document.body.innerHTML = '';
  28. // // Avoid `attachTo: document.body` Warning
  29. // const div = document.createElement('div');
  30. // div.setAttribute('id', 'container');
  31. // document.body.appendChild(div);
  32. // });
  33. // afterEach(() => {
  34. // const div = document.getElementById('container');
  35. // if (div) {
  36. // document.body.removeChild(div);
  37. // }
  38. // });
  39. // TODO
  40. // 1、field mounted
  41. // it('onChange - trigger by field mounted', () => {
  42. // const spyOnChange = sinon.spy(() => {});
  43. // const props = {
  44. // onChange: spyOnChange,
  45. // };
  46. // const form = getForm(props);
  47. // form.setProps({ children: fields });
  48. // TODO mounted目前不触发form上面的onChange
  49. // expect(spyOnChange.called).toEqual(true);
  50. // });
  51. // 2、field umounted
  52. it('onChange - trigger by field unmounted', () => {
  53. const spyOnChange = sinon.spy(() => {});
  54. const props = {
  55. onChange: spyOnChange,
  56. children: fields,
  57. };
  58. const form = getForm(props);
  59. // unmount 2 field component
  60. form.setProps({ children: undefined });
  61. expect(spyOnChange.callCount).toEqual(2);
  62. });
  63. // 3、field value changed
  64. it('onChange - trigger by field value updated internal', () => {
  65. const spyOnChange = sinon.spy(formState => {});
  66. const props = {
  67. onChange: spyOnChange,
  68. children: <Form.Input field="name" />,
  69. };
  70. const form = getForm(props);
  71. let event = { target: { value: 'semi' } };
  72. form.find(`.${BASE_CLASS_PREFIX}-input`).simulate('change', event);
  73. let expectFormState = {
  74. values: { name: 'semi' },
  75. errors: {},
  76. touched: { name: true },
  77. };
  78. expect(spyOnChange.callCount).toEqual(1);
  79. expect(spyOnChange.calledWithMatch(expectFormState)).toBe(true);
  80. });
  81. // 4、field error status changed
  82. it('onChange - trigger by field error status updated internal', done => {
  83. const spyOnChange = sinon.spy(formState => {});
  84. const props = {
  85. onChange: spyOnChange,
  86. children: (
  87. <Form.Input
  88. trigger="change"
  89. field="name"
  90. rules={[{ validator: (rule, value) => value === 'muji', message: 'not muji' }]}
  91. />
  92. ),
  93. };
  94. const form = getForm(props);
  95. let event = { target: { value: 'semi' } };
  96. form.find(`.${BASE_CLASS_PREFIX}-input`).simulate('change', event);
  97. let expectFormState = {
  98. values: { name: 'semi' },
  99. errors: { name: 'not muji' },
  100. touched: { name: true },
  101. };
  102. // casuse validate is async operation, need to wait
  103. setTimeout(() => {
  104. // first called by value change
  105. expect(spyOnChange.callCount).toEqual(2);
  106. expect(spyOnChange.lastCall.calledWithMatch(expectFormState)).toEqual(true);
  107. done();
  108. }, 300);
  109. });
  110. // 5、field touched status changed
  111. it('onChange - trigger by touched status updated, internal', () => {
  112. const spyOnChange = sinon.spy(formState => {});
  113. const props = {
  114. onChange: spyOnChange,
  115. children: <Form.Input field="name" />,
  116. };
  117. const form = getForm(props);
  118. let event = {};
  119. form.find(`.${BASE_CLASS_PREFIX}-input`).simulate('blur', event);
  120. let expectFormState = {
  121. values: {},
  122. errors: {},
  123. touched: { name: true },
  124. };
  125. expect(spyOnChange.calledOnce).toBe(true);
  126. expect(spyOnChange.calledWithMatch(expectFormState)).toEqual(true);
  127. });
  128. });