ssoForm.spec.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {browserHistory} from 'react-router';
  2. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import SsoForm from 'sentry/views/auth/ssoForm';
  4. describe('SsoForm', function () {
  5. const emptyAuthConfig = {
  6. canRegister: false,
  7. githubLoginLink: '',
  8. googleLoginLink: '',
  9. hasNewsletter: false,
  10. serverHostname: '',
  11. vstsLoginLink: '',
  12. };
  13. async function doSso(apiRequest: jest.Mock) {
  14. await userEvent.type(
  15. screen.getByRole('textbox', {name: 'Organization ID'}),
  16. 'org123'
  17. );
  18. await userEvent.click(screen.getByRole('button', {name: 'Continue'}));
  19. expect(apiRequest).toHaveBeenCalledWith(
  20. '/auth/sso-locate/',
  21. expect.objectContaining({data: {organization: 'org123'}})
  22. );
  23. }
  24. it('renders', function () {
  25. const authConfig = {
  26. ...emptyAuthConfig,
  27. serverHostname: 'testserver',
  28. };
  29. render(<SsoForm authConfig={authConfig} />);
  30. expect(screen.getByLabelText('Organization ID')).toBeInTheDocument();
  31. });
  32. it('handles errors', async function () {
  33. const mockRequest = MockApiClient.addMockResponse({
  34. url: '/auth/sso-locate/',
  35. method: 'POST',
  36. statusCode: 400,
  37. body: {
  38. detail: 'Invalid org name',
  39. },
  40. });
  41. render(<SsoForm authConfig={emptyAuthConfig} />);
  42. await doSso(mockRequest);
  43. expect(await screen.findByText('Invalid org name')).toBeInTheDocument();
  44. });
  45. it('handles success', async function () {
  46. const mockRequest = MockApiClient.addMockResponse({
  47. url: '/auth/sso-locate/',
  48. method: 'POST',
  49. statusCode: 200,
  50. body: {
  51. nextUri: '/next/',
  52. },
  53. });
  54. render(<SsoForm authConfig={emptyAuthConfig} />);
  55. await doSso(mockRequest);
  56. await waitFor(() =>
  57. expect(browserHistory.push).toHaveBeenCalledWith({pathname: '/next/'})
  58. );
  59. });
  60. });