From 323060e5ea14671a3cb60daa804c3c762dad7c12 Mon Sep 17 00:00:00 2001 From: Mister-Mario Date: Sun, 28 Apr 2024 02:09:44 +0200 Subject: [PATCH] RestorePassword tests added --- .../ForgetPassword/RestorePassword.test.js | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 webapp/src/components/ForgetPassword/RestorePassword.test.js diff --git a/webapp/src/components/ForgetPassword/RestorePassword.test.js b/webapp/src/components/ForgetPassword/RestorePassword.test.js new file mode 100644 index 0000000..292d03d --- /dev/null +++ b/webapp/src/components/ForgetPassword/RestorePassword.test.js @@ -0,0 +1,99 @@ +import { screen, render, fireEvent } from '@testing-library/react'; +import '@testing-library/jest-dom'; // Ensures custom assertions are available +import RestorePassword from './RestorePassword'; // Path to the component to test + +// Mock for useTranslation +jest.mock('react-i18next', () => ({ + useTranslation: jest.fn().mockReturnValue({ + t: (key) => key, // Simulate translation by returning the key + }), +})); + +describe('RestorePassword Component', () => { + it('should render the form with all expected fields', () => { + const mockHandleSubmit = jest.fn(); // Mock for form submission + const mockHandlePasswordChange = jest.fn(); + const mockShowErrors = jest.fn().mockReturnValue(null); // Simulate no errors + + const { getByText } = render( + key} // Mock for translation + showErrors={mockShowErrors} + /> + ); + + // Verify key elements are rendered + expect(getByText('forgotPassword.enter_password')).toBeInTheDocument(); + expect(getByText(/addUser.password_placeholder/i)).toBeInTheDocument(); + expect(getByText(/addUser.repeat_password_placeholder/i)).toBeInTheDocument(); + + // Check if the email and username are read-only and have correct values + const inputs = screen.getAllByRole('textbox'); + expect(inputs[0].value).toBe('test@example.com'); + expect(inputs[1].value).toBe('testuser'); + }); + + it('should call handlePasswordChange on password input change', () => { + const mockHandlePasswordChange = jest.fn(); + + const { container } = render( + key} + showErrors={jest.fn()} + /> + ); + + // Simulate changing the password input + + const passwordInput = container.querySelector(`input[name="password"]`); + fireEvent.change(passwordInput, { target: { value: 'newPassword' } }); + + // Ensure handlePasswordChange was called with the correct argument + expect(mockHandlePasswordChange).toHaveBeenCalledWith(expect.any(Object)); // Expecting event object + }); + + it('should call handleSubmit on form submission', () => { + const mockHandleSubmit = jest.fn(); + + const { getByText } = render( + key} + showErrors={jest.fn()} + /> + ); + + // Simulate form submission + const form = getByText('forgotPassword.enter_password_button').closest('form'); + fireEvent.submit(form); + + // Verify handleSubmit was called on form submission + expect(mockHandleSubmit).toHaveBeenCalled(); + }); +});