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