registerForm.spec.jsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 api = new MockApiClient();
  7. function doLogin(apiRequest) {
  8. userEvent.type(screen.getByLabelText('Name'), 'joe');
  9. userEvent.type(screen.getByLabelText('Email'), 'test@test.com');
  10. userEvent.type(screen.getByLabelText('Password'), '12345pass');
  11. userEvent.click(screen.getByRole('button', {name: 'Continue'}));
  12. expect(apiRequest).toHaveBeenCalledWith(
  13. '/auth/register/',
  14. expect.objectContaining({
  15. data: {
  16. name: 'joe',
  17. username: 'test@test.com',
  18. password: '12345pass',
  19. subscribe: true,
  20. },
  21. })
  22. );
  23. }
  24. it('handles errors', async function () {
  25. const mockRequest = MockApiClient.addMockResponse({
  26. url: '/auth/register/',
  27. method: 'POST',
  28. statusCode: 400,
  29. body: {
  30. detail: 'Registration failed',
  31. errors: {},
  32. },
  33. });
  34. const authConfig = {};
  35. render(<RegisterForm api={api} authConfig={authConfig} />);
  36. doLogin(mockRequest);
  37. expect(await screen.findByText('Registration failed')).toBeInTheDocument();
  38. });
  39. it('handles success', async function () {
  40. const userObject = {
  41. id: 1,
  42. name: 'Joe',
  43. };
  44. const mockRequest = MockApiClient.addMockResponse({
  45. url: '/auth/register/',
  46. method: 'POST',
  47. statusCode: 200,
  48. body: {
  49. user: userObject,
  50. nextUri: '/next/',
  51. },
  52. });
  53. const authConfig = {};
  54. render(<RegisterForm api={api} authConfig={authConfig} />);
  55. doLogin(mockRequest);
  56. await waitFor(() => expect(ConfigStore.get('user')).toEqual(userObject));
  57. expect(browserHistory.push).toHaveBeenCalledWith({pathname: '/next/'});
  58. });
  59. });