index.spec.tsx 2.8 KB

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