ssoForm.spec.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import {browserHistory} from 'sentry/utils/browserHistory';
  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. });