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 all commits
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, 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
Loading