teamSelector.spec.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {addTeamToProject} from 'sentry/actionCreators/projects';
  3. import {TeamSelector} from 'sentry/components/forms/teamSelector';
  4. import OrganizationStore from 'sentry/stores/organizationStore';
  5. import TeamStore from 'sentry/stores/teamStore';
  6. jest.mock('sentry/actionCreators/projects', () => ({
  7. addTeamToProject: jest.fn(),
  8. }));
  9. const teamData = [
  10. {
  11. id: '1',
  12. slug: 'team1',
  13. name: 'Team 1',
  14. },
  15. {
  16. id: '2',
  17. slug: 'team2',
  18. name: 'Team 2',
  19. },
  20. {
  21. id: '3',
  22. slug: 'team3',
  23. name: 'Team 3',
  24. },
  25. ];
  26. const teams = teamData.map(data => TestStubs.Team(data));
  27. const project = TestStubs.Project({teams: [teams[0]]});
  28. const organization = TestStubs.Organization({access: ['project:write']});
  29. act(() => OrganizationStore.onUpdate(organization, {replace: true}));
  30. function createWrapper(props = {}) {
  31. return render(
  32. <TeamSelector
  33. organization={organization}
  34. name="teamSelector"
  35. aria-label="Select a team"
  36. {...props}
  37. />
  38. );
  39. }
  40. describe('Team Selector', function () {
  41. beforeEach(function () {
  42. TeamStore.loadInitialData(teams);
  43. });
  44. it('renders options', function () {
  45. createWrapper();
  46. userEvent.type(screen.getByText('Select...'), '{keyDown}');
  47. expect(screen.getByText('#team1')).toBeInTheDocument();
  48. expect(screen.getByText('#team2')).toBeInTheDocument();
  49. expect(screen.getByText('#team3')).toBeInTheDocument();
  50. });
  51. it('selects an option', function () {
  52. const onChangeMock = jest.fn();
  53. createWrapper({onChange: onChangeMock});
  54. userEvent.type(screen.getByText('Select...'), '{keyDown}');
  55. const option = screen.getByText('#team1');
  56. userEvent.click(option);
  57. expect(onChangeMock).toHaveBeenCalledWith(
  58. expect.objectContaining({value: 'team1'}),
  59. expect.anything()
  60. );
  61. });
  62. it('respects the team filter', function () {
  63. const teamFilter = team => team.slug === 'team1';
  64. createWrapper({teamFilter});
  65. userEvent.type(screen.getByText('Select...'), '{keyDown}');
  66. expect(screen.getByText('#team1')).toBeInTheDocument();
  67. // These options should be filtered out
  68. expect(screen.queryByText('#team2')).not.toBeInTheDocument();
  69. expect(screen.queryByText('#team3')).not.toBeInTheDocument();
  70. });
  71. it('respects the project filter', function () {
  72. createWrapper({project});
  73. userEvent.type(screen.getByText('Select...'), '{keyDown}');
  74. expect(screen.getByText('#team1')).toBeInTheDocument();
  75. // team2 and team3 should have add to project buttons
  76. expect(screen.getAllByRole('button').length).toBe(2);
  77. });
  78. it('respects the team and project filter', function () {
  79. const teamFilter = team => team.slug === 'team1' || team.slug === 'team2';
  80. createWrapper({teamFilter, project});
  81. userEvent.type(screen.getByText('Select...'), '{keyDown}');
  82. expect(screen.getByText('#team1')).toBeInTheDocument();
  83. // team3 should be filtered out
  84. expect(screen.queryByText('#team3')).not.toBeInTheDocument();
  85. // team2 should have add to project buttons
  86. expect(screen.getAllByRole('button').length).toBe(1);
  87. });
  88. it('allows you to add teams outside of project', function () {
  89. createWrapper({project});
  90. userEvent.type(screen.getByText('Select...'), '{keyDown}');
  91. expect(screen.getByText('#team1')).toBeInTheDocument();
  92. // team2 and team3 should have add to project buttons
  93. const addToProjectButtons = screen.getAllByRole('button');
  94. userEvent.click(addToProjectButtons[0]);
  95. expect(addTeamToProject).toHaveBeenCalled();
  96. });
  97. it('allows searching by slug with useId', function () {
  98. const onChangeMock = jest.fn();
  99. createWrapper({useId: true, onChange: onChangeMock});
  100. userEvent.type(screen.getByText('Select...'), '{keyDown}');
  101. userEvent.type(screen.getByLabelText('Select a team'), 'team2');
  102. expect(screen.getByText('#team2')).toBeInTheDocument();
  103. userEvent.click(screen.getByText('#team2'));
  104. expect(onChangeMock).toHaveBeenCalledWith(
  105. expect.objectContaining({value: '2'}),
  106. expect.anything()
  107. );
  108. });
  109. });