-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import Alert from '@/components/AlertNotification/AlertNotification'; | ||
import { AlertVariants } from '@/components/AlertNotification/Alerts.enum'; | ||
import { render } from '@testing-library/react'; | ||
import { toast } from 'react-hot-toast'; | ||
import { logoutAccount } from './__mocks__/authcontextprovider'; | ||
|
||
const mockLoginAccount = jest.fn(); | ||
|
||
jest.mock('./AuthHelper', () => ({ | ||
logoutAccount: logoutAccount, | ||
})); | ||
|
||
describe('AuthContextProvider', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('mock a successful login and show default notification', async () => { | ||
mockLoginAccount.mockResolvedValueOnce('default'); | ||
|
||
const result = await mockLoginAccount(); | ||
expect(result).toBe('success'); | ||
|
||
const message = "You've successfully logged in!"; | ||
const { getByText } = render( | ||
<Alert variant={AlertVariants.Success} message={message} />, | ||
); | ||
expect(getByText(message)).toBeInTheDocument(); | ||
}); | ||
|
||
test('mock a failed login attempt and show error notification', async () => { | ||
mockLoginAccount.mockRejectedValueOnce('error'); | ||
|
||
const result = await mockLoginAccount(); | ||
expect(result).toBe('error'); | ||
|
||
const message = 'Something went wrong!'; | ||
const { getByText } = render( | ||
<Alert variant={AlertVariants.Error} message={message} />, | ||
); | ||
expect(getByText(message)).toBeInTheDocument(); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Log out and clear session state | ||
* @returns {Promise<void>} | ||
*/ | ||
const logoutAccount = async (): Promise<void> => { | ||
try { | ||
await account.deleteSession('current'); | ||
setIsSignedIn(false); | ||
resetUser(); // Reset user data in the store | ||
toast.custom( | ||
<Alert variant={AlertVariants.Default} message="Logged Out" />, | ||
); | ||
router.push('/login'); | ||
} catch (error) { | ||
toast.custom( | ||
<Alert | ||
variant={AlertVariants.Error} | ||
message="Something went wrong. Try logging out again." | ||
/>, | ||
); | ||
console.error('Logout error:', error); | ||
} | ||
}; | ||
|
||
export { logoutAccount }; |