loginForm.spec.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 LoginForm from 'sentry/views/auth/loginForm';
  5. async function doLogin() {
  6. await userEvent.type(screen.getByRole('textbox', {name: 'Account'}), 'test@test.com');
  7. await userEvent.type(screen.getByRole('textbox', {name: 'Password'}), '12345pass');
  8. await userEvent.click(screen.getByRole('button', {name: 'Continue'}));
  9. }
  10. describe('LoginForm', function () {
  11. const emptyAuthConfig = {
  12. canRegister: false,
  13. githubLoginLink: '',
  14. googleLoginLink: '',
  15. hasNewsletter: false,
  16. serverHostname: '',
  17. vstsLoginLink: '',
  18. };
  19. it('handles errors', async function () {
  20. MockApiClient.addMockResponse({
  21. url: '/auth/login/',
  22. method: 'POST',
  23. statusCode: 400,
  24. body: {
  25. detail: 'Login attempt failed',
  26. errors: {__all__: 'Bad username password'},
  27. },
  28. });
  29. render(<LoginForm authConfig={emptyAuthConfig} />);
  30. await doLogin();
  31. expect(await screen.findByText('Bad username password')).toBeInTheDocument();
  32. });
  33. it('handles success', async function () {
  34. const userObject = {
  35. id: 1,
  36. name: 'Joe',
  37. };
  38. const mockRequest = MockApiClient.addMockResponse({
  39. url: '/auth/login/',
  40. method: 'POST',
  41. statusCode: 200,
  42. body: {
  43. user: userObject,
  44. nextUri: '/next/',
  45. },
  46. });
  47. render(<LoginForm authConfig={emptyAuthConfig} />);
  48. await doLogin();
  49. expect(mockRequest).toHaveBeenCalledWith(
  50. '/auth/login/',
  51. expect.objectContaining({
  52. data: {username: 'test@test.com', password: '12345pass'},
  53. })
  54. );
  55. await waitFor(() => expect(ConfigStore.get('user')).toEqual(userObject));
  56. expect(browserHistory.push).toHaveBeenCalledWith({pathname: '/next/'});
  57. });
  58. it('renders login provider buttons', function () {
  59. const authConfig = {
  60. ...emptyAuthConfig,
  61. vstsLoginLink: '/vstsLogin',
  62. githubLoginLink: '/githubLogin',
  63. };
  64. render(<LoginForm authConfig={authConfig} />);
  65. expect(screen.getByText('Sign in with GitHub')).toBeInTheDocument();
  66. expect(screen.getByText('Sign in with Azure DevOps')).toBeInTheDocument();
  67. });
  68. });