formField.spec.jsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {render} from 'sentry-test/reactTestingLibrary';
  2. import Form from 'sentry/components/forms/form';
  3. import FormModel from 'sentry/components/forms/model';
  4. import TextField from 'sentry/components/forms/textField';
  5. describe('FormField + model', function () {
  6. let model;
  7. beforeEach(function () {
  8. model = new FormModel();
  9. });
  10. it('renders with Form', function () {
  11. const wrapper = render(
  12. <Form model={model}>
  13. <TextField name="fieldName" />
  14. </Form>
  15. );
  16. expect(wrapper.container).toSnapshot();
  17. });
  18. it('sets initial data in model', function () {
  19. render(
  20. <Form model={model} initialData={{fieldName: 'test'}}>
  21. <TextField name="fieldName" />
  22. </Form>
  23. );
  24. expect(model.initialData.fieldName).toBe('test');
  25. });
  26. it('has `defaultValue` from field', function () {
  27. render(
  28. <Form model={model}>
  29. <TextField name="fieldName" defaultValue="foo" />
  30. </Form>
  31. );
  32. expect(model.initialData.fieldName).toBe('foo');
  33. expect(model.fields.get('fieldName')).toBe('foo');
  34. });
  35. it('does not use `defaultValue` when there is initial data', function () {
  36. render(
  37. <Form model={model} initialData={{fieldName: 'test'}}>
  38. <TextField name="fieldName" defaultValue="foo" />
  39. </Form>
  40. );
  41. expect(model.initialData.fieldName).toBe('test');
  42. expect(model.fields.get('fieldName')).toBe('test');
  43. });
  44. it('transforms `defaultValue` from field with `setValue`', function () {
  45. render(
  46. <Form model={model}>
  47. <TextField name="fieldName" defaultValue="foo" setValue={v => `${v}${v}`} />
  48. </Form>
  49. );
  50. expect(model.initialData.fieldName).toBe('foofoo');
  51. expect(model.fields.get('fieldName')).toBe('foofoo');
  52. });
  53. it('sets field descriptor in model', function () {
  54. render(
  55. <Form model={model} initialData={{fieldName: 'test'}}>
  56. <TextField name="fieldName" required />
  57. </Form>
  58. );
  59. expect(model.getDescriptor('fieldName', 'required')).toBe(true);
  60. });
  61. it('removes field descriptor in model on unmount', function () {
  62. const wrapper = render(
  63. <Form model={model} initialData={{fieldName: 'test'}}>
  64. <TextField name="fieldName" required />
  65. </Form>
  66. );
  67. expect(model.fieldDescriptor.has('fieldName')).toBe(true);
  68. wrapper.unmount();
  69. expect(model.fieldDescriptor.has('fieldName')).toBe(false);
  70. });
  71. });