Skip to content

Commit

Permalink
Added jest for ForgetPasswordFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
uo289267 committed Apr 27, 2024
1 parent 5eaf7e6 commit ae0fe1d
Showing 1 changed file with 50 additions and 1 deletion.
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');
});
});
});

});

0 comments on commit ae0fe1d

Please sign in to comment.