diff --git a/src/screens/UserPortal/Posts/Posts.test.tsx b/src/screens/UserPortal/Posts/Posts.spec.tsx similarity index 89% rename from src/screens/UserPortal/Posts/Posts.test.tsx rename to src/screens/UserPortal/Posts/Posts.spec.tsx index 433e36f94a..83b626ba20 100644 --- a/src/screens/UserPortal/Posts/Posts.test.tsx +++ b/src/screens/UserPortal/Posts/Posts.spec.tsx @@ -16,17 +16,29 @@ import i18nForTest from 'utils/i18nForTest'; import Home from './Posts'; import useLocalStorage from 'utils/useLocalstorage'; import { DELETE_POST_MUTATION } from 'GraphQl/Mutations/mutations'; +import { expect, describe, it, vi } from 'vitest'; const { setItem } = useLocalStorage(); -jest.mock('react-toastify', () => ({ +vi.mock('react-toastify', () => ({ toast: { - error: jest.fn(), - info: jest.fn(), - success: jest.fn(), + error: vi.fn(), + info: vi.fn(), + success: vi.fn(), }, })); +const mockUseParams = vi.fn().mockReturnValue({ orgId: 'orgId' }); + +vi.mock('react-router-dom', async () => { + const actual = await vi.importActual('react-router-dom'); + return { + ...actual, + useParams: () => mockUseParams(), + useNavigate: () => vi.fn(), + }; +}); + const MOCKS = [ { request: { @@ -262,31 +274,27 @@ const renderHomeScreen = (): RenderResult => Object.defineProperty(window, 'matchMedia', { writable: true, - value: jest.fn().mockImplementation((query) => ({ + value: vi.fn().mockImplementation((query) => ({ matches: false, media: query, onchange: null, - addListener: jest.fn(), // Deprecated - removeListener: jest.fn(), // Deprecated - addEventListener: jest.fn(), - removeEventListener: jest.fn(), - dispatchEvent: jest.fn(), + addListener: vi.fn(), // Deprecated + removeListener: vi.fn(), // Deprecated + addEventListener: vi.fn(), + removeEventListener: vi.fn(), + dispatchEvent: vi.fn(), })), }); describe('Testing Home Screen: User Portal', () => { - beforeAll(() => { - jest.mock('react-router-dom', () => ({ - ...jest.requireActual('react-router-dom'), - useParams: () => ({ orgId: 'orgId' }), - })); + beforeEach(() => { + mockUseParams.mockReturnValue({ orgId: 'orgId' }); }); - afterAll(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); }); - test('Check if HomeScreen renders properly', async () => { + it('Check if HomeScreen renders properly', async () => { renderHomeScreen(); await wait(); @@ -294,7 +302,7 @@ describe('Testing Home Screen: User Portal', () => { expect(startPostBtn).toBeInTheDocument(); }); - test('StartPostModal should render on click of StartPost btn', async () => { + it('StartPostModal should render on click of StartPost btn', async () => { renderHomeScreen(); await wait(); @@ -306,7 +314,7 @@ describe('Testing Home Screen: User Portal', () => { expect(startPostModal).toBeInTheDocument(); }); - test('StartPostModal should close on clicking the close button', async () => { + it('StartPostModal should close on clicking the close button', async () => { renderHomeScreen(); await wait(); @@ -325,7 +333,6 @@ describe('Testing Home Screen: User Portal', () => { userEvent.type(screen.getByTestId('postInput'), 'some content'); - // Check that the content and image have been added expect(screen.getByTestId('postInput')).toHaveValue('some content'); await screen.findByAltText('Post Image Preview'); expect(screen.getByAltText('Post Image Preview')).toBeInTheDocument(); @@ -342,7 +349,7 @@ describe('Testing Home Screen: User Portal', () => { expect(screen.getByTestId('postImageInput')).toHaveValue(''); }); - test('Check whether Posts render in PostCard', async () => { + it('Check whether Posts render in PostCard', async () => { setItem('userId', '640d98d9eb6a743d75341067'); renderHomeScreen(); await wait(); @@ -359,7 +366,7 @@ describe('Testing Home Screen: User Portal', () => { expect(screen.queryByText('This is the post two')).toBeInTheDocument(); }); - test('Checking if refetch works after deleting this post', async () => { + it('Checking if refetch works after deleting this post', async () => { setItem('userId', '640d98d9eb6a743d75341067'); renderHomeScreen(); expect(screen.queryAllByTestId('dropdown')).not.toBeNull(); @@ -371,11 +378,15 @@ describe('Testing Home Screen: User Portal', () => { }); describe('HomeScreen with invalid orgId', () => { - test('Redirect to /user when organizationId is falsy', async () => { - jest.mock('react-router-dom', () => ({ - ...jest.requireActual('react-router-dom'), - useParams: () => ({ orgId: undefined }), - })); + beforeEach(() => { + mockUseParams.mockReturnValue({ orgId: undefined }); + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + + it('Redirect to /user when organizationId is falsy', async () => { render(