index.spec.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(
  16. <OrganizationJoinRequest
  17. {...TestStubs.routeComponentProps()}
  18. params={{orgId: org.slug}}
  19. />
  20. );
  21. expect(screen.getByRole('heading', {name: 'Request to Join'})).toBeInTheDocument();
  22. expect(screen.getByRole('textbox', {name: 'Email Address'})).toBeInTheDocument();
  23. expect(screen.getByRole('button', {name: 'Request to Join'})).toBeInTheDocument();
  24. });
  25. it('submits', async function () {
  26. const postMock = MockApiClient.addMockResponse({
  27. url: endpoint,
  28. method: 'POST',
  29. });
  30. render(
  31. <OrganizationJoinRequest
  32. {...TestStubs.routeComponentProps()}
  33. params={{orgId: org.slug}}
  34. />
  35. );
  36. await userEvent.type(
  37. screen.getByRole('textbox', {name: 'Email Address'}),
  38. 'email@example.com{enter}'
  39. );
  40. expect(postMock).toHaveBeenCalled();
  41. expect(
  42. await screen.findByRole('heading', {name: 'Request Sent'})
  43. ).toBeInTheDocument();
  44. expect(
  45. screen.queryByRole('button', {name: 'Request to Join'})
  46. ).not.toBeInTheDocument();
  47. });
  48. it('errors', async function () {
  49. const postMock = MockApiClient.addMockResponse({
  50. url: endpoint,
  51. method: 'POST',
  52. statusCode: 400,
  53. });
  54. render(
  55. <OrganizationJoinRequest
  56. {...TestStubs.routeComponentProps()}
  57. params={{orgId: org.slug}}
  58. />
  59. );
  60. await userEvent.type(
  61. screen.getByRole('textbox', {name: 'Email Address'}),
  62. 'email@example.com{enter}'
  63. );
  64. await waitFor(() => {
  65. expect(postMock).toHaveBeenCalled();
  66. expect(addErrorMessage).toHaveBeenCalled();
  67. });
  68. expect(screen.getByRole('heading', {name: 'Request to Join'})).toBeInTheDocument();
  69. });
  70. it('cancels', async function () {
  71. const spy = jest.spyOn(window.location, 'assign').mockImplementation(() => {});
  72. render(
  73. <OrganizationJoinRequest
  74. {...TestStubs.routeComponentProps()}
  75. params={{orgId: org.slug}}
  76. />
  77. );
  78. await userEvent.click(screen.getByRole('button', {name: 'Cancel'}));
  79. expect(spy).toHaveBeenCalledWith(`/auth/login/${org.slug}/`);
  80. });
  81. });