TeamGroupSync.test.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { render, screen, waitFor } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { TeamGroup } from '../../types';
  5. import { Props, TeamGroupSync } from './TeamGroupSync';
  6. import { getMockTeamGroups } from './__mocks__/teamMocks';
  7. const setup = (propOverrides?: object) => {
  8. const props: Props = {
  9. isReadOnly: false,
  10. groups: [] as TeamGroup[],
  11. loadTeamGroups: jest.fn(),
  12. addTeamGroup: jest.fn(),
  13. removeTeamGroup: jest.fn(),
  14. };
  15. Object.assign(props, propOverrides);
  16. return render(<TeamGroupSync {...props} />);
  17. };
  18. describe('TeamGroupSync', () => {
  19. it('should render component', () => {
  20. setup();
  21. expect(screen.getByRole('heading', { name: /External group sync/i })).toBeInTheDocument();
  22. });
  23. it('should render groups table', () => {
  24. setup({ groups: getMockTeamGroups(3) });
  25. expect(screen.getAllByRole('row')).toHaveLength(4); // 3 items plus table header
  26. });
  27. it('should call add group', async () => {
  28. const mockAddGroup = jest.fn();
  29. setup({ addTeamGroup: mockAddGroup });
  30. // Empty List CTA "Add group" button is second in the DOM order
  31. await userEvent.click(screen.getAllByRole('button', { name: /add group/i })[1]);
  32. expect(screen.getByRole('textbox', { name: /add external group/i })).toBeVisible();
  33. await userEvent.type(screen.getByRole('textbox', { name: /add external group/i }), 'test/group');
  34. await userEvent.click(screen.getAllByRole('button', { name: /add group/i })[0]);
  35. await waitFor(() => {
  36. expect(mockAddGroup).toHaveBeenCalledWith('test/group');
  37. });
  38. });
  39. it('should call remove group', async () => {
  40. const mockRemoveGroup = jest.fn();
  41. const mockGroup: TeamGroup = { teamId: 1, groupId: 'some/group' };
  42. setup({ removeTeamGroup: mockRemoveGroup, groups: [mockGroup] });
  43. await userEvent.click(screen.getByRole('button', { name: 'Remove group some/group' }));
  44. await waitFor(() => {
  45. expect(mockRemoveGroup).toHaveBeenCalledWith('some/group');
  46. });
  47. });
  48. });