index.spec.tsx 2.8 KB

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