registerForm.spec.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {browserHistory} from 'react-router';
  2. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import ConfigStore from 'sentry/stores/configStore';
  4. import RegisterForm from 'sentry/views/auth/registerForm';
  5. describe('Register', function () {
  6. const emptyAuthConfig = {
  7. canRegister: false,
  8. githubLoginLink: '',
  9. googleLoginLink: '',
  10. hasNewsletter: false,
  11. serverHostname: '',
  12. vstsLoginLink: '',
  13. };
  14. async function doLogin(apiRequest: jest.Mock) {
  15. await userEvent.type(screen.getByRole('textbox', {name: 'Name'}), 'joe');
  16. await userEvent.type(screen.getByRole('textbox', {name: 'Email'}), 'test@test.com');
  17. await userEvent.type(screen.getByRole('textbox', {name: 'Password'}), '12345pass');
  18. await userEvent.click(screen.getByRole('button', {name: 'Continue'}));
  19. expect(apiRequest).toHaveBeenCalledWith(
  20. '/auth/register/',
  21. expect.objectContaining({
  22. data: {
  23. name: 'joe',
  24. username: 'test@test.com',
  25. password: '12345pass',
  26. subscribe: true,
  27. },
  28. })
  29. );
  30. }
  31. it('handles errors', async function () {
  32. const mockRequest = MockApiClient.addMockResponse({
  33. url: '/auth/register/',
  34. method: 'POST',
  35. statusCode: 400,
  36. body: {
  37. detail: 'Registration failed',
  38. errors: {},
  39. },
  40. });
  41. render(<RegisterForm authConfig={emptyAuthConfig} />);
  42. await doLogin(mockRequest);
  43. expect(await screen.findByText('Registration failed')).toBeInTheDocument();
  44. });
  45. it('handles success', async function () {
  46. const userObject = {
  47. id: 1,
  48. name: 'Joe',
  49. };
  50. const mockRequest = MockApiClient.addMockResponse({
  51. url: '/auth/register/',
  52. method: 'POST',
  53. statusCode: 200,
  54. body: {
  55. user: userObject,
  56. nextUri: '/next/',
  57. },
  58. });
  59. render(<RegisterForm authConfig={emptyAuthConfig} />);
  60. await doLogin(mockRequest);
  61. await waitFor(() => expect(ConfigStore.get('user')).toEqual(userObject));
  62. expect(browserHistory.push).toHaveBeenCalledWith({pathname: '/next/'});
  63. });
  64. });