-
-
Notifications
You must be signed in to change notification settings - Fork 864
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
1 parent
1f2379b
commit 7a27af0
Showing
1 changed file
with
34 additions
and
21 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 |
---|---|---|
|
@@ -17,32 +17,37 @@ import { | |
import { REMOVE_MEMBER_MUTATION } from 'GraphQl/Mutations/mutations'; | ||
import { getItem } from 'utils/useLocalstorage'; | ||
import { toast } from 'react-toastify'; | ||
import { vi } from 'vitest'; | ||
|
||
jest.mock('react-toastify', () => ({ | ||
toast: { success: jest.fn() }, // Mock toast function | ||
vi.mock('react-toastify', () => ({ | ||
toast: { success: vi.fn() }, // Mock toast function | ||
})); | ||
|
||
Object.defineProperty(window, 'localStorage', { | ||
value: { | ||
getItem: jest.fn(), | ||
setItem: jest.fn(), | ||
removeItem: jest.fn(), | ||
clear: jest.fn(), | ||
getItem: vi.fn(), | ||
setItem: vi.fn(), | ||
removeItem: vi.fn(), | ||
clear: vi.fn(), | ||
}, | ||
writable: true, | ||
}); | ||
|
||
// Mock useParams to return a test organization ID | ||
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
useParams: jest.fn(), | ||
useNavigate: jest.fn(), | ||
})); | ||
|
||
vi.mock('react-router-dom', async () => { | ||
const actualDom = await vi.importActual('react-router-dom'); | ||
return { | ||
...actualDom, | ||
useParams: vi.fn(), | ||
useNavigate: vi.fn(), | ||
}; | ||
}); | ||
|
||
// Mock the custom hook | ||
jest.mock('utils/useLocalstorage', () => { | ||
vi.mock('utils/useLocalstorage', () => { | ||
return { | ||
getItem: jest.fn((prefix: string, key: string) => { | ||
getItem: vi.fn((prefix: string, key: string) => { | ||
if (prefix === 'Talawa-admin' && key === 'email') | ||
return '[email protected]'; | ||
if (prefix === 'Talawa-admin' && key === 'userId') return '12345'; | ||
|
@@ -235,8 +240,10 @@ const errorMocks = [ | |
|
||
beforeEach(() => { | ||
localStorage.clear(); | ||
jest.clearAllMocks(); // Clear mocks before each test | ||
(useParams as jest.Mock).mockReturnValue({ orgId: 'test-org-id' }); | ||
vi.clearAllMocks(); | ||
(useParams as unknown as ReturnType<typeof vi.fn>).mockReturnValue({ | ||
orgId: 'test-org-id', | ||
}); | ||
}); | ||
|
||
describe('LeaveOrganization Component', () => { | ||
|
@@ -309,7 +316,9 @@ describe('LeaveOrganization Component', () => { | |
}); | ||
|
||
test('logs an error when unable to access localStorage', () => { | ||
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); | ||
const consoleErrorSpy = vi | ||
.spyOn(console, 'error') | ||
.mockImplementation(() => {}); | ||
const userEmail = (() => { | ||
try { | ||
return getItem('Talawa-admin-error', 'user-email-error') ?? ''; | ||
|
@@ -336,9 +345,11 @@ describe('LeaveOrganization Component', () => { | |
}); | ||
|
||
test('navigates and shows toast when email matches', async () => { | ||
const mockNavigate = jest.fn(); | ||
(useNavigate as jest.Mock).mockReturnValue(mockNavigate); | ||
const toastSuccessMock = jest.fn(); | ||
const mockNavigate = vi.fn(); | ||
(useNavigate as unknown as ReturnType<typeof vi.fn>).mockReturnValue( | ||
mockNavigate, | ||
); | ||
const toastSuccessMock = vi.fn(); | ||
toast.success = toastSuccessMock; | ||
render( | ||
<MockedProvider mocks={mocks} addTypename={false}> | ||
|
@@ -485,8 +496,10 @@ describe('LeaveOrganization Component', () => { | |
}); | ||
|
||
test('closes modal and resets state when Esc key is pressed', async () => { | ||
const mockNavigate = jest.fn(); | ||
(useNavigate as jest.Mock).mockReturnValue(mockNavigate); | ||
const mockNavigate = vi.fn(); | ||
(useNavigate as unknown as ReturnType<typeof vi.fn>).mockReturnValue( | ||
mockNavigate, | ||
); | ||
render( | ||
<MockedProvider mocks={mocks} addTypename={false}> | ||
<BrowserRouter> | ||
|