tableField.spec.jsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {mountGlobalModal} from 'sentry-test/modal';
  3. import Form from 'sentry/components/forms/form';
  4. import FormModel from 'sentry/components/forms/model';
  5. import TableField from 'sentry/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. );
  25. });
  26. it('renders without form context', function () {
  27. wrapper = mountWithTheme(
  28. <TableField
  29. name="fieldName"
  30. columnKeys={columnKeys}
  31. columnLabels={columnLabels}
  32. />
  33. );
  34. expect(wrapper).toSnapshot();
  35. });
  36. it('renders with form context', function () {
  37. expect(wrapper).toSnapshot();
  38. });
  39. it('renders button text', function () {
  40. expect(wrapper.find('button[aria-label="Add Thing"]').text()).toEqual('Add Thing');
  41. });
  42. it("doesn't render columns if there's no initialData", function () {
  43. expect(wrapper.find('HeaderLabel').exists()).toBe(false);
  44. });
  45. describe('saves changes', function () {
  46. it('handles adding a new row', function () {
  47. wrapper.find('button[aria-label="Add Thing"]').simulate('click');
  48. expect(wrapper.find('HeaderLabel').at(0).text()).toBe('Column 1');
  49. expect(wrapper.find('HeaderLabel').at(1).text()).toBe('Column 2');
  50. });
  51. it('handles removing a row', async function () {
  52. // add a couple new rows for funsies
  53. wrapper.find('button[aria-label="Add Thing"]').simulate('click');
  54. wrapper.find('button[aria-label="Add Thing"]').simulate('click');
  55. // delete the last row
  56. wrapper.find('button[aria-label="delete"]').last().simulate('click');
  57. // click through confirmation
  58. const modal = await mountGlobalModal();
  59. modal.find('Button[data-test-id="confirm-button"]').simulate('click');
  60. wrapper.update();
  61. expect(wrapper.find('RowContainer[data-test-id="field-row"]')).toHaveLength(1);
  62. });
  63. });
  64. });
  65. });