index.spec.jsx 2.4 KB

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