Skip to content

Commit

Permalink
added a new test and structure to files
Browse files Browse the repository at this point in the history
  • Loading branch information
Clue355 committed Jul 7, 2024
1 parent f23c6d9 commit e6c0493
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
44 changes: 19 additions & 25 deletions context/AuthContextProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,32 @@ import { logoutAccount } from './__mocks__/authcontextprovider';

const mockLoginAccount = jest.fn();

jest.mock('./AuthHelper', () => ({
logoutAccount: logoutAccount,
jest.mock('./__mocks__/authcontextprovider', () => ({
logoutAccount: jest.fn(),
}));

jest.mock('react-hot-toast', () => ({
toast: {
custom: jest.fn(),
},
}));

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} />,
test('mock a successful logout and show default notification', async () => {
(logoutAccount as jest.Mock).mockImplementation(async () => {
toast.custom(
<Alert variant={AlertVariants.Default} message="Logged Out" />,
);
});

await logoutAccount();
expect(logoutAccount).toHaveBeenCalled();
expect(toast.custom).toHaveBeenCalledWith(
<Alert variant={AlertVariants.Default} message="Logged Out" />,
);
expect(getByText(message)).toBeInTheDocument();
});
});
2 changes: 2 additions & 0 deletions context/__mocks__/authcontextprovider.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import toast from 'react-hot-toast';

/**
* Log out and clear session state
* @returns {Promise<void>}
Expand Down

0 comments on commit e6c0493

Please sign in to comment.