jsonForm.spec.tsx 6.9 KB

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