ResetPasswordContainer.test.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import React from 'react';
  2. import { fireEvent, render, screen, waitFor, renderHook } from '../../../../../../tests/test-utils';
  3. import { getTRPCMock, getTRPCMockError } from '../../../../mocks/getTrpcMock';
  4. import { server } from '../../../../mocks/server';
  5. import { useToastStore } from '../../../../state/toastStore';
  6. import { ResetPasswordContainer } from './ResetPasswordContainer';
  7. const pushFn = jest.fn();
  8. jest.mock('next/router', () => {
  9. const actualRouter = jest.requireActual('next-router-mock');
  10. return {
  11. ...actualRouter,
  12. useRouter: () => ({
  13. ...actualRouter.useRouter(),
  14. push: pushFn,
  15. }),
  16. };
  17. });
  18. describe('ResetPasswordContainer', () => {
  19. it('should render the component', () => {
  20. render(<ResetPasswordContainer isRequested={false} />);
  21. expect(screen.getByText('Reset your password')).toBeInTheDocument();
  22. expect(screen.getByText('Run this command on your server and then refresh this page')).toBeInTheDocument();
  23. expect(screen.getByText('./scripts/reset-password.sh')).toBeInTheDocument();
  24. });
  25. it('should render the password reset success message', async () => {
  26. // Arrange
  27. const email = 'test@test.com';
  28. render(<ResetPasswordContainer isRequested />);
  29. const resetPasswordForm = screen.getByRole('button', { name: 'Reset password' });
  30. const newPassword = 'new_password';
  31. const response = { email };
  32. server.use(getTRPCMock({ path: ['auth', 'changeOperatorPassword'], type: 'mutation', response, delay: 100 }));
  33. const passwordInput = screen.getByLabelText('Password');
  34. const confirmPasswordInput = screen.getByLabelText('Confirm password');
  35. // Act
  36. fireEvent.change(passwordInput, { target: { value: newPassword } });
  37. fireEvent.change(confirmPasswordInput, { target: { value: newPassword } });
  38. fireEvent.click(resetPasswordForm);
  39. // Assert
  40. await waitFor(() => {
  41. expect(screen.getByText('Password reset')).toBeInTheDocument();
  42. expect(screen.getByText(`Your password has been reset. You can now login with your new password. And your email ${email}`)).toBeInTheDocument();
  43. expect(screen.getByText('Back to login')).toBeInTheDocument();
  44. });
  45. });
  46. it('should show error toast if reset password mutation fails', async () => {
  47. // Arrange
  48. const { result, unmount } = renderHook(() => useToastStore());
  49. render(<ResetPasswordContainer isRequested />);
  50. const resetPasswordForm = screen.getByRole('button', { name: 'Reset password' });
  51. fireEvent.click(resetPasswordForm);
  52. const newPassword = 'new_password';
  53. const error = { message: 'Something went wrong' };
  54. server.use(getTRPCMockError({ path: ['auth', 'changeOperatorPassword'], type: 'mutation', message: error.message }));
  55. const passwordInput = screen.getByLabelText('Password');
  56. const confirmPasswordInput = screen.getByLabelText('Confirm password');
  57. // Act
  58. fireEvent.change(passwordInput, { target: { value: newPassword } });
  59. fireEvent.change(confirmPasswordInput, { target: { value: newPassword } });
  60. fireEvent.click(resetPasswordForm);
  61. // Assert
  62. await waitFor(() => {
  63. expect(result.current.toasts[0].description).toBe(error.message);
  64. });
  65. unmount();
  66. });
  67. it('should call the cancel request mutation when cancel button is clicked', async () => {
  68. // Arrange
  69. const { result, unmount } = renderHook(() => useToastStore());
  70. render(<ResetPasswordContainer isRequested />);
  71. server.use(getTRPCMock({ path: ['auth', 'cancelPasswordChangeRequest'], type: 'mutation', response: true }));
  72. const cancelRequestForm = screen.getByRole('button', { name: 'Cancel password change request' });
  73. // Act
  74. fireEvent.click(cancelRequestForm);
  75. // Assert
  76. await waitFor(() => {
  77. expect(result.current.toasts[0].title).toBe('Password change request cancelled');
  78. });
  79. unmount();
  80. });
  81. it('should redirect to login page when Back to login button is clicked', async () => {
  82. // Arrange
  83. render(<ResetPasswordContainer isRequested />);
  84. server.use(getTRPCMock({ path: ['auth', 'changeOperatorPassword'], type: 'mutation', response: { email: 'goofy@test.com' } }));
  85. const resetPasswordForm = screen.getByRole('button', { name: 'Reset password' });
  86. const passwordInput = screen.getByLabelText('Password');
  87. const confirmPasswordInput = screen.getByLabelText('Confirm password');
  88. const newPassword = 'new_password';
  89. fireEvent.change(passwordInput, { target: { value: newPassword } });
  90. fireEvent.change(confirmPasswordInput, { target: { value: newPassword } });
  91. fireEvent.click(resetPasswordForm);
  92. await waitFor(() => {
  93. expect(screen.getByText('Back to login')).toBeInTheDocument();
  94. });
  95. // Act
  96. const backToLoginButton = screen.getByRole('button', { name: 'Back to login' });
  97. fireEvent.click(backToLoginButton);
  98. // Assert
  99. await waitFor(() => {
  100. expect(pushFn).toHaveBeenCalledWith('/login');
  101. });
  102. });
  103. });