organizationJoinRequest.spec.jsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  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. const wrapper = mountWithTheme(
  18. <OrganizationJoinRequest params={{orgId: org.slug}} />
  19. );
  20. expect(wrapper.find('h3').text()).toBe('Request to Join');
  21. expect(wrapper.find('EmailField').exists()).toBe(true);
  22. expect(wrapper.find('button[aria-label="Request to Join"]').exists()).toBe(true);
  23. expect(trackAdhocEvent).toHaveBeenCalledWith({
  24. eventKey: 'join_request.viewed',
  25. org_slug: org.slug,
  26. });
  27. });
  28. it('submits', async function () {
  29. const postMock = MockApiClient.addMockResponse({
  30. url: endpoint,
  31. method: 'POST',
  32. });
  33. const wrapper = mountWithTheme(
  34. <OrganizationJoinRequest params={{orgId: org.slug}} />
  35. );
  36. wrapper
  37. .find('input[id="email"]')
  38. .simulate('change', {target: {value: 'email@example.com'}});
  39. wrapper.find('form').simulate('submit');
  40. expect(postMock).toHaveBeenCalled();
  41. await tick();
  42. wrapper.update();
  43. expect(wrapper.find('h3').text()).toBe('Request Sent');
  44. expect(wrapper.find('EmailField').exists()).toBe(false);
  45. expect(wrapper.find('button[aria-label="Request to Join"]').exists()).toBe(false);
  46. });
  47. it('errors', async function () {
  48. const postMock = MockApiClient.addMockResponse({
  49. url: endpoint,
  50. method: 'POST',
  51. statusCode: 400,
  52. });
  53. const wrapper = mountWithTheme(
  54. <OrganizationJoinRequest params={{orgId: org.slug}} />
  55. );
  56. wrapper
  57. .find('input[id="email"]')
  58. .simulate('change', {target: {value: 'email@example.com'}});
  59. wrapper.find('form').simulate('submit');
  60. expect(postMock).toHaveBeenCalled();
  61. await tick();
  62. wrapper.update();
  63. expect(addErrorMessage).toHaveBeenCalled();
  64. expect(wrapper.find('h3').text()).toBe('Request to Join');
  65. expect(wrapper.find('EmailField').exists()).toBe(true);
  66. expect(wrapper.find('button[aria-label="Request to Join"]').exists()).toBe(true);
  67. });
  68. it('cancels', function () {
  69. const spy = jest.spyOn(window.location, 'assign').mockImplementation(() => {});
  70. const wrapper = mountWithTheme(
  71. <OrganizationJoinRequest params={{orgId: org.slug}} />
  72. );
  73. wrapper.find('button[aria-label="Cancel"]').simulate('click');
  74. expect(spy).toHaveBeenCalledWith(`/auth/login/${org.slug}/`);
  75. });
  76. });