Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added jest for ForgetPasswordFunctions #152

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion webapp/src/components/ForgetPassword/ForgetPassword.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import userEvent from '@testing-library/user-event';
import Cookies from 'js-cookie';

import ForgetPasswordFunctions from './ForgetPasswordFunctions';
i18en.use(initReactI18next).init({
resources: {},
lng: 'en',
Expand All @@ -19,6 +19,7 @@ i18en.use(initReactI18next).init({
});
global.i18en = i18en;
const mockAxios = new MockAdapter(axios);
jest.mock('axios');
describe('ForgetPassword Component', () => {
test('renders Ask Email component', async () => {
act( () => {
Expand Down Expand Up @@ -71,5 +72,53 @@ describe('ForgetPassword Component', () => {
});
});

describe('ForgetPasswordFunctions', () => {
let forgetPasswordFunctions;

beforeEach(() => {
forgetPasswordFunctions = new ForgetPasswordFunctions();
});

describe('sendEmail', () => {
it('should send email and return true on success', async () => {
axios.post.mockResolvedValue({ data: true });
const result = await forgetPasswordFunctions.sendEmail('[email protected]', 'testuser');
expect(result).toBe(true);
});

it('should throw error on failure', async () => {
axios.post.mockRejectedValue(new Error('Failed to send email'));
await expect(forgetPasswordFunctions.sendEmail('[email protected]', 'testuser')).rejects.toThrow('Failed to send email');
});
});

describe('tokenFromCode', () => {
it('should get token from code and set it', async () => {
const token = 'testToken';
axios.get.mockResolvedValue({ data: { token } });
await forgetPasswordFunctions.tokenFromCode('testCode');
expect(forgetPasswordFunctions.token).toBe(token);
});

it('should throw error on failure', async () => {
axios.get.mockRejectedValue(new Error('Failed to get token'));
await expect(forgetPasswordFunctions.tokenFromCode('testCode')).rejects.toThrow('Failed to get token');
});
});

describe('changePassword', () => {
it('should change password', async () => {
const response = { data: 'Password changed successfully' };
axios.post.mockResolvedValue(response);
const result = await forgetPasswordFunctions.changePassword('[email protected]', 'testuser', 'newpassword', 'newpassword');
expect(result).toEqual(response);
});

it('should throw error on failure', async () => {
axios.post.mockRejectedValue(new Error('Failed to change password'));
await expect(forgetPasswordFunctions.changePassword('[email protected]', 'testuser', 'newpassword', 'newpassword')).rejects.toThrow('Failed to change password');
});
});
});

});