Skip to content

Commit

Permalink
Refactor: src/screens/UserPortal/Posts from Jest to Vitest #2578 (#3190)
Browse files Browse the repository at this point in the history
* changes done

* changes done
  • Loading branch information
khushipatil1523 authored Jan 9, 2025
1 parent d669de3 commit 6ef6c6f
Showing 1 changed file with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -262,39 +274,35 @@ 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();
const startPostBtn = await screen.findByTestId('postBtn');
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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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(
<MockedProvider addTypename={false} link={link}>
<MemoryRouter initialEntries={['/user/organization/']}>
Expand Down

0 comments on commit 6ef6c6f

Please sign in to comment.