formField.spec.jsx 2.7 KB

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