jsonForm.spec.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import JsonForm from 'sentry/components/forms/jsonForm';
  3. import accountDetailsFields from 'sentry/data/forms/accountDetails';
  4. import {fields} from 'sentry/data/forms/projectGeneralSettings';
  5. const user = TestStubs.User({});
  6. describe('JsonForm', function () {
  7. describe('form prop', function () {
  8. it('default', function () {
  9. const wrapper = mountWithTheme(
  10. <JsonForm forms={accountDetailsFields} additionalFieldProps={{user}} />
  11. );
  12. expect(wrapper).toSnapshot();
  13. });
  14. it('missing additionalFieldProps required in "valid" prop', function () {
  15. // eslint-disable-next-line no-console
  16. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  17. try {
  18. mountWithTheme(<JsonForm forms={accountDetailsFields} />);
  19. } catch (error) {
  20. expect(error.message).toBe(
  21. "Cannot read properties of undefined (reading 'email')"
  22. );
  23. }
  24. });
  25. it('should ALWAYS hide panel, if all fields have visible set to false AND there is no renderHeader & renderFooter - visible prop is of type boolean', function () {
  26. const modifiedAccountDetails = accountDetailsFields.map(accountDetailsField => ({
  27. ...accountDetailsField,
  28. fields: accountDetailsField.fields.map(field => ({...field, visible: false})),
  29. }));
  30. const wrapper = mountWithTheme(
  31. <JsonForm forms={modifiedAccountDetails} additionalFieldProps={{user}} />
  32. );
  33. expect(wrapper.find('FormPanel')).toHaveLength(0);
  34. });
  35. it('should ALWAYS hide panel, if all fields have visible set to false AND there is no renderHeader & renderFooter - visible prop is of type func', function () {
  36. const modifiedAccountDetails = accountDetailsFields.map(accountDetailsField => ({
  37. ...accountDetailsField,
  38. fields: accountDetailsField.fields.map(field => ({
  39. ...field,
  40. visible: () => false,
  41. })),
  42. }));
  43. const wrapper = mountWithTheme(
  44. <JsonForm forms={modifiedAccountDetails} additionalFieldProps={{user}} />
  45. );
  46. expect(wrapper.find('FormPanel')).toHaveLength(0);
  47. });
  48. it('should NOT hide panel, if at least one field has visible set to true - no visible prop (1 field) + visible prop is of type func (2 field)', function () {
  49. // accountDetailsFields has two fields. The second field will always have visible set to false, because the username and the email are the same 'foo@example.com'
  50. const wrapper = mountWithTheme(
  51. <JsonForm forms={accountDetailsFields} additionalFieldProps={{user}} />
  52. );
  53. expect(wrapper.find('FormPanel')).toHaveLength(1);
  54. expect(wrapper.find('input')).toHaveLength(1);
  55. });
  56. it('should NOT hide panel, if all fields have visible set to false AND a prop renderHeader is passed', function () {
  57. const modifiedAccountDetails = accountDetailsFields.map(accountDetailsField => ({
  58. ...accountDetailsField,
  59. fields: accountDetailsField.fields.map(field => ({...field, visible: false})),
  60. }));
  61. const wrapper = mountWithTheme(
  62. <JsonForm
  63. forms={modifiedAccountDetails}
  64. additionalFieldProps={{user}}
  65. renderHeader={() => <div>this is a Header </div>}
  66. />
  67. );
  68. expect(wrapper.find('FormPanel')).toHaveLength(1);
  69. expect(wrapper.find('input')).toHaveLength(0);
  70. });
  71. it('should NOT hide panel, if all fields have visible set to false AND a prop renderFooter is passed', function () {
  72. const modifiedAccountDetails = accountDetailsFields.map(accountDetailsField => ({
  73. ...accountDetailsField,
  74. fields: accountDetailsField.fields.map(field => ({...field, visible: false})),
  75. }));
  76. const wrapper = mountWithTheme(
  77. <JsonForm
  78. forms={modifiedAccountDetails}
  79. additionalFieldProps={{user}}
  80. renderFooter={() => <div>this is a Footer </div>}
  81. />
  82. );
  83. expect(wrapper.find('FormPanel')).toHaveLength(1);
  84. expect(wrapper.find('input')).toHaveLength(0);
  85. });
  86. });
  87. describe('fields prop', function () {
  88. const jsonFormFields = [fields.name, fields.platform];
  89. it('default', function () {
  90. const wrapper = mountWithTheme(<JsonForm fields={jsonFormFields} />);
  91. expect(wrapper).toSnapshot();
  92. });
  93. it('missing additionalFieldProps required in "valid" prop', function () {
  94. // eslint-disable-next-line no-console
  95. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  96. try {
  97. mountWithTheme(
  98. <JsonForm
  99. fields={[{...jsonFormFields[0], visible: ({test}) => !!test.email}]}
  100. />
  101. );
  102. } catch (error) {
  103. expect(error.message).toBe(
  104. "Cannot read properties of undefined (reading 'email')"
  105. );
  106. }
  107. });
  108. it('should NOT hide panel, if at least one field has visible set to true - no visible prop', function () {
  109. // slug and platform have no visible prop, that means they will be always visible
  110. const wrapper = mountWithTheme(<JsonForm fields={jsonFormFields} />);
  111. expect(wrapper.find('FormPanel')).toHaveLength(1);
  112. expect(wrapper.find('input[type="text"]')).toHaveLength(2);
  113. });
  114. it('should NOT hide panel, if at least one field has visible set to true - visible prop is of type boolean', function () {
  115. // slug and platform have no visible prop, that means they will be always visible
  116. const wrapper = mountWithTheme(
  117. <JsonForm fields={jsonFormFields.map(field => ({...field, visible: true}))} />
  118. );
  119. expect(wrapper.find('FormPanel')).toHaveLength(1);
  120. expect(wrapper.find('input[type="text"]')).toHaveLength(2);
  121. });
  122. it('should NOT hide panel, if at least one field has visible set to true - visible prop is of type func', function () {
  123. // slug and platform have no visible prop, that means they will be always visible
  124. const wrapper = mountWithTheme(
  125. <JsonForm
  126. fields={jsonFormFields.map(field => ({...field, visible: () => true}))}
  127. />
  128. );
  129. expect(wrapper.find('FormPanel')).toHaveLength(1);
  130. expect(wrapper.find('input[type="text"]')).toHaveLength(2);
  131. });
  132. it('should ALWAYS hide panel, if all fields have visible set to false - visible prop is of type boolean', function () {
  133. // slug and platform have no visible prop, that means they will be always visible
  134. const wrapper = mountWithTheme(
  135. <JsonForm fields={jsonFormFields.map(field => ({...field, visible: false}))} />
  136. );
  137. expect(wrapper.find('FormPanel')).toHaveLength(0);
  138. });
  139. it('should ALWAYS hide panel, if all fields have visible set to false - visible prop is of type function', function () {
  140. // slug and platform have no visible prop, that means they will be always visible
  141. const wrapper = mountWithTheme(
  142. <JsonForm
  143. fields={jsonFormFields.map(field => ({...field, visible: () => false}))}
  144. />
  145. );
  146. expect(wrapper.find('FormPanel')).toHaveLength(0);
  147. });
  148. });
  149. });