uptimeHeadersField.spec.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import Form from 'sentry/components/forms/form';
  3. import FormModel from 'sentry/components/forms/model';
  4. import {UptimeHeadersField} from './uptimeHeadersField';
  5. describe('UptimeHeaderField', function () {
  6. function input(name: string) {
  7. return screen.getByRole('textbox', {name});
  8. }
  9. it('can be manipulated', async function () {
  10. const model = new FormModel();
  11. render(
  12. <Form model={model}>
  13. <UptimeHeadersField name="headers" />
  14. </Form>
  15. );
  16. // Starts with one empty header row
  17. expect(input('Name of header 1')).toBeInTheDocument();
  18. expect(input('Value of header 1')).toBeInTheDocument();
  19. // Change the name of the first item
  20. await userEvent.type(input('Name of header 1'), 'My-Header');
  21. expect(model.getValue('headers')).toEqual([['My-Header', '']]);
  22. // Change the value of the first item
  23. await userEvent.type(input('Value of My-Header'), 'example');
  24. expect(model.getValue('headers')).toEqual([['My-Header', 'example']]);
  25. // Add a new header
  26. await userEvent.click(screen.getByRole('button', {name: 'Add Header'}));
  27. // New item has been added
  28. expect(input('Name of header 2')).toBeInTheDocument();
  29. expect(input('Value of header 2')).toBeInTheDocument();
  30. // Old items still exist
  31. expect(input('Name of header 1')).toBeInTheDocument();
  32. expect(input('Name of header 1')).toHaveValue('My-Header');
  33. expect(model.getValue('headers')).toEqual([['My-Header', 'example']]);
  34. // Setting the value of header item 2 does persist the value without a name
  35. await userEvent.type(input('Value of header 2'), 'whatever');
  36. expect(input('Value of header 2')).toHaveValue('whatever');
  37. expect(model.getValue('headers')).toEqual([['My-Header', 'example']]);
  38. // Giving header item 2 a name will persist it's value
  39. await userEvent.type(input('Name of header 2'), 'X-Second-Header');
  40. expect(model.getValue('headers')).toEqual([
  41. ['My-Header', 'example'],
  42. ['X-Second-Header', 'whatever'],
  43. ]);
  44. // Remove the first header
  45. await userEvent.click(screen.getByRole('button', {name: 'Remove My-Header'}));
  46. expect(model.getValue('headers')).toEqual([['X-Second-Header', 'whatever']]);
  47. expect(
  48. screen.queryByRole('textbox', {name: 'Vaalue of My-Header'})
  49. ).not.toBeInTheDocument();
  50. });
  51. it('disambiguates headers with the same name', async function () {
  52. const model = new FormModel();
  53. render(
  54. <Form model={model}>
  55. <UptimeHeadersField name="headers" />
  56. </Form>
  57. );
  58. await userEvent.click(screen.getByRole('button', {name: 'Add Header'}));
  59. await userEvent.type(input('Name of header 1'), 'test');
  60. await userEvent.type(input('Name of header 2'), 'test');
  61. expect(input('Value of test')).toBeInTheDocument();
  62. expect(input('Value of test (2)')).toBeInTheDocument();
  63. });
  64. it('does not persist empty header names', async function () {
  65. const model = new FormModel();
  66. render(
  67. <Form model={model}>
  68. <UptimeHeadersField name="headers" />
  69. </Form>
  70. );
  71. await userEvent.click(screen.getByRole('button', {name: 'Add Header'}));
  72. await userEvent.type(input('Name of header 1'), 'test');
  73. await userEvent.type(input('Value of test'), 'test value');
  74. // The second value is dropped
  75. expect(model.getValue('headers')).toEqual([['test', 'test value']]);
  76. });
  77. it('populates with initial values', function () {
  78. const model = new FormModel({
  79. initialData: {
  80. headers: [
  81. ['one', 'test one'],
  82. ['two', 'test two'],
  83. ],
  84. },
  85. });
  86. render(
  87. <Form model={model}>
  88. <UptimeHeadersField name="headers" />
  89. </Form>
  90. );
  91. expect(input('Name of header 1')).toHaveValue('one');
  92. expect(input('Name of header 2')).toHaveValue('two');
  93. expect(input('Value of one')).toHaveValue('test one');
  94. expect(input('Value of two')).toHaveValue('test two');
  95. });
  96. it('remove special characters from header names', async function () {
  97. const model = new FormModel();
  98. render(
  99. <Form model={model}>
  100. <UptimeHeadersField name="headers" />
  101. </Form>
  102. );
  103. // Spaces and special characters are stripped
  104. await userEvent.type(input('Name of header 1'), 'My Awesome Header!');
  105. expect(model.getValue('headers')).toEqual([['MyAwesomeHeader', '']]);
  106. });
  107. });