Skip to content

Commit

Permalink
changes to notification test
Browse files Browse the repository at this point in the history
  • Loading branch information
Clue355 committed Jul 7, 2024
1 parent cc95013 commit f23c6d9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
44 changes: 44 additions & 0 deletions context/AuthContextProvider.test.tsx
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();
});
});
6 changes: 6 additions & 0 deletions context/AuthContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ export const AuthContextProvider = ({
);
router.push('/login');
} catch (error) {
toast.custom(
<Alert
variant={AlertVariants.Error}
message="Something went wrong. Try logging out again."
/>,
);
console.error('Logout error:', error);
}
};
Expand Down
25 changes: 25 additions & 0 deletions context/__mocks__/authcontextprovider.js
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 };

0 comments on commit f23c6d9

Please sign in to comment.