jsonForm.spec.tsx 6.5 KB

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