Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: src/screens/UserPortal/Posts from Jest to Vitest #2578 #3190

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading