tableField.spec.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. render,
  3. renderGlobalModal,
  4. screen,
  5. userEvent,
  6. } from 'sentry-test/reactTestingLibrary';
  7. import Form from 'sentry/components/forms/form';
  8. import FormModel from 'sentry/components/forms/model';
  9. import TableField from './tableField';
  10. describe('TableField', function () {
  11. const mockSubmit = jest.fn();
  12. const model = new FormModel();
  13. const defaultProps = {
  14. columnKeys: ['column1', 'column2'],
  15. columnLabels: {column1: 'Column 1', column2: 'Column 2'},
  16. addButtonText: 'Add Thing',
  17. name: 'table',
  18. };
  19. describe('renders', function () {
  20. it('renders without form context', function () {
  21. const {container} = render(<TableField {...defaultProps} />);
  22. expect(container).toSnapshot();
  23. });
  24. it('renders with form context', function () {
  25. const {container} = render(
  26. <Form onSubmit={mockSubmit} model={model}>
  27. <TableField {...defaultProps} />
  28. </Form>
  29. );
  30. expect(container).toSnapshot();
  31. });
  32. it('renders button text', function () {
  33. render(
  34. <Form onSubmit={mockSubmit} model={model}>
  35. <TableField {...defaultProps} />
  36. </Form>
  37. );
  38. expect(screen.getByLabelText('Add Thing')).toHaveTextContent('Add Thing');
  39. });
  40. describe('saves changes', function () {
  41. it('handles adding a new row', function () {
  42. render(
  43. <Form onSubmit={mockSubmit} model={model}>
  44. <TableField {...defaultProps} />
  45. </Form>
  46. );
  47. userEvent.click(screen.getByLabelText('Add Thing'));
  48. const columns = screen.getAllByText(/Column/);
  49. expect(columns).toHaveLength(2);
  50. expect(columns[0]).toHaveTextContent('Column 1');
  51. expect(columns[1]).toHaveTextContent('Column 2');
  52. });
  53. it('handles removing a row', function () {
  54. render(
  55. <Form onSubmit={mockSubmit} model={model}>
  56. <TableField {...defaultProps} />
  57. </Form>
  58. );
  59. renderGlobalModal();
  60. // add a couple new rows
  61. userEvent.click(screen.getByLabelText('Add Thing'));
  62. userEvent.click(screen.getByLabelText('Add Thing'));
  63. // delete the last row
  64. userEvent.click(screen.getAllByLabelText(/delete/).at(-1)!);
  65. // click through confirmation
  66. userEvent.click(screen.getByTestId('confirm-button'));
  67. expect(screen.getByTestId('field-row')).toBeInTheDocument();
  68. });
  69. });
  70. });
  71. });