withTeamsForUser.spec.jsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {Organization} from 'fixtures/js-stubs/organization';
  2. import {Project} from 'fixtures/js-stubs/project';
  3. import {render, screen} from 'sentry-test/reactTestingLibrary';
  4. import ProjectsStore from 'sentry/stores/projectsStore';
  5. import TeamStore from 'sentry/stores/teamStore';
  6. import withTeamsForUser from 'sentry/utils/withTeamsForUser';
  7. describe('withUserTeams HoC', function () {
  8. const api = new MockApiClient();
  9. const organization = Organization();
  10. delete organization.projects;
  11. function Output({error, teams}) {
  12. if (error) {
  13. return <p>Error: {error.responseText}</p>;
  14. }
  15. return (
  16. <p>
  17. {teams.map(team => (
  18. <span key={team.slug}>{team.slug}</span>
  19. ))}
  20. </p>
  21. );
  22. }
  23. beforeEach(function () {
  24. MockApiClient.clearMockResponses();
  25. jest.spyOn(ProjectsStore, 'loadInitialData');
  26. jest.spyOn(TeamStore, 'loadInitialData');
  27. });
  28. it('forwards errors', async function () {
  29. MockApiClient.addMockResponse({
  30. url: `/organizations/${organization.slug}/user-teams/`,
  31. statusCode: 400,
  32. });
  33. const Container = withTeamsForUser(Output);
  34. render(<Container organization={organization} api={api} />);
  35. expect(await screen.findByText(/Error:/)).toBeInTheDocument();
  36. });
  37. it('fetches teams and loads stores', async function () {
  38. const mockProjectA = Project({slug: 'a', id: '1'});
  39. const mockProjectB = Project({slug: 'b', id: '2'});
  40. const mockTeams = [
  41. {
  42. slug: 'sentry',
  43. projects: [mockProjectB],
  44. },
  45. {
  46. slug: 'captainplanet',
  47. projects: [mockProjectA, mockProjectB],
  48. },
  49. ];
  50. MockApiClient.addMockResponse({
  51. url: `/organizations/${organization.slug}/user-teams/`,
  52. body: mockTeams,
  53. });
  54. const Container = withTeamsForUser(Output);
  55. render(<Container organization={organization} api={api} />);
  56. expect(await screen.findByText('sentry')).toBeInTheDocument();
  57. expect(screen.getByText('captainplanet')).toBeInTheDocument();
  58. });
  59. });