SignupInvited.test.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { render, screen, waitFor, within } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { getRouteComponentProps } from 'app/core/navigation/__mocks__/routeProps';
  5. import { backendSrv } from '../../core/services/backend_srv';
  6. import { SignupInvitedPage, Props } from './SignupInvited';
  7. jest.mock('app/core/core', () => ({
  8. contextSrv: {
  9. user: { orgName: 'Invited to Org Name' },
  10. },
  11. }));
  12. jest.mock('@grafana/runtime', () => ({
  13. ...(jest.requireActual('@grafana/runtime') as unknown as object),
  14. getBackendSrv: () => backendSrv,
  15. }));
  16. const defaultGet = {
  17. email: 'some.user@localhost',
  18. name: 'Some User',
  19. invitedBy: 'Invited By User',
  20. username: 'someuser',
  21. };
  22. async function setupTestContext({ get = defaultGet }: { get?: typeof defaultGet | null } = {}) {
  23. jest.clearAllMocks();
  24. const getSpy = jest.spyOn(backendSrv, 'get');
  25. getSpy.mockResolvedValue(get);
  26. const postSpy = jest.spyOn(backendSrv, 'post');
  27. postSpy.mockResolvedValue([]);
  28. const props: Props = {
  29. ...getRouteComponentProps({
  30. match: {
  31. params: { code: 'some code' },
  32. } as any,
  33. }),
  34. };
  35. render(<SignupInvitedPage {...props} />);
  36. await waitFor(() => expect(getSpy).toHaveBeenCalled());
  37. expect(getSpy).toHaveBeenCalledTimes(1);
  38. return { getSpy, postSpy };
  39. }
  40. describe('SignupInvitedPage', () => {
  41. describe('when initialized but invite data has not been retrieved yet', () => {
  42. it('then it should not render', async () => {
  43. await setupTestContext({ get: null });
  44. expect(screen.queryByText(/email/i)).not.toBeInTheDocument();
  45. });
  46. });
  47. describe('when initialized and invite data has been retrieved', () => {
  48. it('then the greeting should be correct', async () => {
  49. await setupTestContext();
  50. expect(
  51. screen.getByRole('heading', {
  52. name: /hello some user\./i,
  53. })
  54. ).toBeInTheDocument();
  55. });
  56. it('then the invited by should be correct', async () => {
  57. await setupTestContext();
  58. const view = screen.getByText(
  59. /has invited you to join grafana and the organization please complete the following and choose a password to accept your invitation and continue:/i
  60. );
  61. expect(within(view).getByText(/invited by user/i)).toBeInTheDocument();
  62. });
  63. it('then the organization invited to should be correct', async () => {
  64. await setupTestContext();
  65. const view = screen.getByText(
  66. /has invited you to join grafana and the organization please complete the following and choose a password to accept your invitation and continue:/i
  67. );
  68. expect(within(view).getByText(/invited to org name/i)).toBeInTheDocument();
  69. });
  70. it('then the form should include form data', async () => {
  71. await setupTestContext();
  72. expect(screen.getByPlaceholderText(/email@example\.com/i)).toHaveValue('some.user@localhost');
  73. expect(screen.getByPlaceholderText(/name \(optional\)/i)).toHaveValue('Some User');
  74. expect(screen.getByPlaceholderText(/username/i)).toHaveValue('some.user@localhost');
  75. expect(screen.getByPlaceholderText(/password/i)).toHaveValue('');
  76. });
  77. });
  78. describe('when user submits the form and the required fields are not filled in', () => {
  79. it('then required fields should show error messages and nothing should be posted', async () => {
  80. const { postSpy } = await setupTestContext({ get: { email: '', invitedBy: '', name: '', username: '' } });
  81. await userEvent.click(screen.getByRole('button', { name: /sign up/i }));
  82. await waitFor(() => expect(screen.getByText(/email is required/i)).toBeInTheDocument());
  83. expect(screen.getByText(/username is required/i)).toBeInTheDocument();
  84. expect(screen.getByText(/password is required/i)).toBeInTheDocument();
  85. expect(postSpy).toHaveBeenCalledTimes(0);
  86. });
  87. });
  88. describe('when user submits the form and the required fields are filled in', () => {
  89. it('then correct form data should be posted', async () => {
  90. const { postSpy } = await setupTestContext();
  91. await userEvent.type(screen.getByPlaceholderText(/password/i), 'pass@word1');
  92. await userEvent.click(screen.getByRole('button', { name: /sign up/i }));
  93. await waitFor(() => expect(postSpy).toHaveBeenCalledTimes(1));
  94. expect(postSpy).toHaveBeenCalledWith('/api/user/invite/complete', {
  95. email: 'some.user@localhost',
  96. name: 'Some User',
  97. username: 'some.user@localhost',
  98. password: 'pass@word1',
  99. inviteCode: 'some code',
  100. });
  101. });
  102. });
  103. });