teamProjects.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {Organization} from 'sentry-fixture/organization';
  2. import {Team} from 'sentry-fixture/team';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  5. import OrganizationTeamProjects from 'sentry/views/settings/organizationTeams/teamProjects';
  6. describe('OrganizationTeamProjects', function () {
  7. let getMock!: jest.Mock;
  8. let putMock!: jest.Mock;
  9. let postMock!: jest.Mock;
  10. let deleteMock!: jest.Mock;
  11. const team = Team({slug: 'team-slug'});
  12. const project = TestStubs.Project({
  13. teams: [team],
  14. access: ['project:read', 'project:write', 'project:admin'],
  15. });
  16. const project2 = TestStubs.Project({
  17. id: '3',
  18. slug: 'project-slug-2',
  19. name: 'Project Name 2',
  20. access: ['project:read', 'project:write', 'project:admin'],
  21. });
  22. const {routerContext, routerProps, organization} = initializeOrg({
  23. organization: Organization({slug: 'org-slug'}),
  24. projects: [project, project2],
  25. router: {params: {teamId: team.slug}},
  26. });
  27. beforeEach(function () {
  28. getMock = MockApiClient.addMockResponse({
  29. url: '/organizations/org-slug/projects/',
  30. body: [project, project2],
  31. });
  32. putMock = MockApiClient.addMockResponse({
  33. method: 'PUT',
  34. url: '/projects/org-slug/project-slug/',
  35. body: project,
  36. });
  37. postMock = MockApiClient.addMockResponse({
  38. method: 'POST',
  39. url: `/projects/org-slug/${project2.slug}/teams/${team.slug}/`,
  40. body: {...project2, teams: [team]},
  41. status: 201,
  42. });
  43. deleteMock = MockApiClient.addMockResponse({
  44. method: 'DELETE',
  45. url: `/projects/org-slug/${project2.slug}/teams/${team.slug}/`,
  46. body: {...project2, teams: []},
  47. status: 204,
  48. });
  49. });
  50. afterEach(function () {
  51. MockApiClient.clearMockResponses();
  52. });
  53. it('should fetch linked and unlinked projects', async function () {
  54. render(<OrganizationTeamProjects {...routerProps} team={team} />, {
  55. context: routerContext,
  56. organization,
  57. });
  58. expect(await screen.findByText('project-slug')).toBeInTheDocument();
  59. expect(getMock).toHaveBeenCalledTimes(2);
  60. expect(getMock.mock.calls[0][1].query.query).toBe('team:team-slug');
  61. expect(getMock.mock.calls[1][1].query.query).toBe('!team:team-slug');
  62. });
  63. it('should allow bookmarking', async function () {
  64. render(<OrganizationTeamProjects {...routerProps} team={team} />, {
  65. context: routerContext,
  66. organization,
  67. });
  68. const stars = await screen.findAllByRole('button', {name: 'Bookmark Project'});
  69. expect(stars).toHaveLength(2);
  70. await userEvent.click(stars[0]);
  71. expect(
  72. screen.getByRole('button', {name: 'Bookmark Project', pressed: true})
  73. ).toBeInTheDocument();
  74. expect(putMock).toHaveBeenCalledTimes(1);
  75. expect(putMock).toHaveBeenCalledWith(
  76. expect.any(String),
  77. expect.objectContaining({
  78. data: {isBookmarked: true},
  79. })
  80. );
  81. });
  82. it('should allow adding and removing projects', async function () {
  83. render(<OrganizationTeamProjects {...routerProps} team={team} />, {
  84. context: routerContext,
  85. organization,
  86. });
  87. expect(getMock).toHaveBeenCalledTimes(2);
  88. await userEvent.click(await screen.findByText('Add Project'));
  89. await userEvent.click(screen.getByRole('option', {name: 'project-slug-2'}));
  90. expect(postMock).toHaveBeenCalledTimes(1);
  91. // find second project's remove button
  92. const removeButtons = await screen.findAllByRole('button', {name: 'Remove'});
  93. await userEvent.click(removeButtons[1]);
  94. expect(deleteMock).toHaveBeenCalledTimes(1);
  95. });
  96. it('handles filtering unlinked projects', async function () {
  97. render(<OrganizationTeamProjects {...routerProps} team={team} />, {
  98. context: routerContext,
  99. organization,
  100. });
  101. expect(getMock).toHaveBeenCalledTimes(2);
  102. await userEvent.click(await screen.findByText('Add Project'));
  103. await userEvent.type(screen.getByRole('textbox'), 'a');
  104. expect(getMock).toHaveBeenCalledTimes(3);
  105. expect(getMock).toHaveBeenCalledWith(
  106. '/organizations/org-slug/projects/',
  107. expect.objectContaining({
  108. query: expect.objectContaining({
  109. query: '!team:team-slug a',
  110. }),
  111. })
  112. );
  113. });
  114. });