generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from Arquisoft/132-add-a-forget-password-func…
…tionality Added jest for ForgetPasswordFunctions
- Loading branch information
Showing
1 changed file
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
|
@@ -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( () => { | ||
|
@@ -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'); | ||
}); | ||
}); | ||
}); | ||
|
||
}); |