Skip to content

Commit

Permalink
Merge pull request #152 from Arquisoft/132-add-a-forget-password-func…
Browse files Browse the repository at this point in the history
…tionality

Added jest for ForgetPasswordFunctions
  • Loading branch information
Mister-Mario authored Apr 27, 2024
2 parents 83493a8 + ae0fe1d commit 4dd8ba2
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 4dd8ba2

Please sign in to comment.