Skip to content

Commit

Permalink
changes done
Browse files Browse the repository at this point in the history
  • Loading branch information
khushipatil1523 committed Jan 7, 2025
1 parent 495e976 commit 7f41ad3
Showing 1 changed file with 33 additions and 22 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, test, 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,28 +274,24 @@ 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 () => {
Expand Down Expand Up @@ -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 Down Expand Up @@ -371,11 +378,15 @@ describe('Testing Home Screen: User Portal', () => {
});

describe('HomeScreen with invalid orgId', () => {
beforeEach(() => {
mockUseParams.mockReturnValue({ orgId: undefined });
});

afterEach(() => {
vi.clearAllMocks();
});

test('Redirect to /user when organizationId is falsy', async () => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({ orgId: undefined }),
}));
render(
<MockedProvider addTypename={false} link={link}>
<MemoryRouter initialEntries={['/user/organization/']}>
Expand Down

0 comments on commit 7f41ad3

Please sign in to comment.