organizationJoinRequest.spec.jsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import {trackAdhocEvent} from 'sentry/utils/analytics';
  4. import OrganizationJoinRequest from 'sentry/views/organizationJoinRequest';
  5. jest.mock('sentry/utils/analytics', () => ({
  6. trackAdhocEvent: jest.fn(),
  7. }));
  8. jest.mock('sentry/actionCreators/indicator');
  9. describe('OrganizationJoinRequest', function () {
  10. const org = TestStubs.Organization({slug: 'test-org'});
  11. const endpoint = `/organizations/${org.slug}/join-request/`;
  12. beforeEach(function () {
  13. trackAdhocEvent.mockClear();
  14. MockApiClient.clearMockResponses();
  15. });
  16. it('renders', function () {
  17. render(<OrganizationJoinRequest params={{orgId: org.slug}} />);
  18. expect(screen.getByRole('heading', {name: 'Request to Join'})).toBeInTheDocument();
  19. expect(screen.getByRole('textbox', {name: 'Email Address'})).toBeInTheDocument();
  20. expect(screen.getByRole('button', {name: 'Request to Join'})).toBeInTheDocument();
  21. expect(trackAdhocEvent).toHaveBeenCalledWith({
  22. eventKey: 'join_request.viewed',
  23. org_slug: org.slug,
  24. });
  25. });
  26. it('submits', async function () {
  27. const postMock = MockApiClient.addMockResponse({
  28. url: endpoint,
  29. method: 'POST',
  30. });
  31. render(<OrganizationJoinRequest params={{orgId: org.slug}} />);
  32. userEvent.type(
  33. screen.getByRole('textbox', {name: 'Email Address'}),
  34. 'email@example.com{enter}'
  35. );
  36. expect(postMock).toHaveBeenCalled();
  37. expect(
  38. await screen.findByRole('heading', {name: 'Request Sent'})
  39. ).toBeInTheDocument();
  40. expect(
  41. screen.queryByRole('button', {name: 'Request to Join'})
  42. ).not.toBeInTheDocument();
  43. });
  44. it('errors', async function () {
  45. const postMock = MockApiClient.addMockResponse({
  46. url: endpoint,
  47. method: 'POST',
  48. statusCode: 400,
  49. });
  50. render(<OrganizationJoinRequest params={{orgId: org.slug}} />);
  51. userEvent.type(
  52. screen.getByRole('textbox', {name: 'Email Address'}),
  53. 'email@example.com{enter}'
  54. );
  55. await waitFor(() => {
  56. expect(postMock).toHaveBeenCalled();
  57. expect(addErrorMessage).toHaveBeenCalled();
  58. });
  59. expect(screen.getByRole('heading', {name: 'Request to Join'})).toBeInTheDocument();
  60. });
  61. it('cancels', function () {
  62. const spy = jest.spyOn(window.location, 'assign').mockImplementation(() => {});
  63. render(<OrganizationJoinRequest params={{orgId: org.slug}} />);
  64. userEvent.click(screen.getByRole('button', {name: 'Cancel'}));
  65. expect(spy).toHaveBeenCalledWith(`/auth/login/${org.slug}/`);
  66. });
  67. });