From e6c04934ec909d6f77764b521af94a612618e835 Mon Sep 17 00:00:00 2001 From: Clue355 Date: Sun, 7 Jul 2024 17:31:36 -0500 Subject: [PATCH] added a new test and structure to files --- context/AuthContextProvider.test.tsx | 44 ++++++++++-------------- context/__mocks__/authcontextprovider.js | 2 ++ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/context/AuthContextProvider.test.tsx b/context/AuthContextProvider.test.tsx index 3684e783..639d5a85 100644 --- a/context/AuthContextProvider.test.tsx +++ b/context/AuthContextProvider.test.tsx @@ -7,8 +7,14 @@ 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', () => { @@ -16,29 +22,17 @@ describe('AuthContextProvider', () => { 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( - , - ); - 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( - , + test('mock a successful logout and show default notification', async () => { + (logoutAccount as jest.Mock).mockImplementation(async () => { + toast.custom( + , + ); + }); + + await logoutAccount(); + expect(logoutAccount).toHaveBeenCalled(); + expect(toast.custom).toHaveBeenCalledWith( + , ); - expect(getByText(message)).toBeInTheDocument(); }); }); diff --git a/context/__mocks__/authcontextprovider.js b/context/__mocks__/authcontextprovider.js index 849856ac..c7b7e1ce 100644 --- a/context/__mocks__/authcontextprovider.js +++ b/context/__mocks__/authcontextprovider.js @@ -1,3 +1,5 @@ +import toast from 'react-hot-toast'; + /** * Log out and clear session state * @returns {Promise}