tableField.spec.jsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {mountGlobalModal} from 'sentry-test/modal';
  3. import Form from 'app/views/settings/components/forms/form';
  4. import FormModel from 'app/views/settings/components/forms/model';
  5. import TableField from 'app/views/settings/components/forms/tableField';
  6. const mockSubmit = jest.fn();
  7. describe('TableField', function () {
  8. let wrapper;
  9. let model;
  10. const columnKeys = ['column1', 'column2'];
  11. const columnLabels = {column1: 'Column 1', column2: 'Column 2'};
  12. describe('renders', function () {
  13. beforeEach(() => {
  14. model = new FormModel();
  15. wrapper = mountWithTheme(
  16. <Form onSubmit={mockSubmit} model={model}>
  17. <TableField
  18. name="fieldName"
  19. columnKeys={columnKeys}
  20. columnLabels={columnLabels}
  21. addButtonText="Add Thing"
  22. />
  23. </Form>,
  24. TestStubs.routerContext()
  25. );
  26. });
  27. it('renders without form context', function () {
  28. wrapper = mountWithTheme(
  29. <TableField
  30. name="fieldName"
  31. columnKeys={columnKeys}
  32. columnLabels={columnLabels}
  33. />,
  34. TestStubs.routerContext()
  35. );
  36. expect(wrapper).toSnapshot();
  37. });
  38. it('renders with form context', function () {
  39. expect(wrapper).toSnapshot();
  40. });
  41. it('renders button text', function () {
  42. expect(wrapper.find('button[aria-label="Add Thing"]').text()).toEqual('Add Thing');
  43. });
  44. it("doesn't render columns if there's no initialData", function () {
  45. expect(wrapper.find('HeaderLabel').exists()).toBe(false);
  46. });
  47. describe('saves changes', function () {
  48. it('handles adding a new row', function () {
  49. wrapper.find('button[aria-label="Add Thing"]').simulate('click');
  50. expect(wrapper.find('HeaderLabel').at(0).text()).toBe('Column 1');
  51. expect(wrapper.find('HeaderLabel').at(1).text()).toBe('Column 2');
  52. });
  53. it('handles removing a row', async function () {
  54. // add a couple new rows for funsies
  55. wrapper.find('button[aria-label="Add Thing"]').simulate('click');
  56. wrapper.find('button[aria-label="Add Thing"]').simulate('click');
  57. // delete the last row
  58. wrapper.find('button[aria-label="delete"]').last().simulate('click');
  59. // click through confirmation
  60. const modal = await mountGlobalModal();
  61. modal.find('Button[data-test-id="confirm-button"]').simulate('click');
  62. wrapper.update();
  63. expect(wrapper.find('RowContainer[data-test-id="field-row"]')).toHaveLength(1);
  64. });
  65. });
  66. });
  67. });