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:vitest to MemberDetailScreen #2662

Open
wants to merge 1 commit into
base: develop-postgres
Choose a base branch
from
Open
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 @@ -21,6 +21,7 @@ import MemberDetail, { getLanguageName, prettyDate } from './MemberDetail';
import { MOCKS1, MOCKS2, MOCKS3 } from './MemberDetailMocks';
import type { ApolloLink } from '@apollo/client';
import { toast } from 'react-toastify';
import { vi } from 'vitest';

const link1 = new StaticMockLink(MOCKS1, true);
const link2 = new StaticMockLink(MOCKS2, true);
Expand All @@ -44,21 +45,38 @@ const translations = {
),
};

jest.mock('@mui/x-date-pickers/DateTimePicker', () => {
/**
* Mocking the DateTimePicker from \@mui/x-date-pickers to use DesktopDateTimePicker in tests.
* This ensures that the component behaves consistently in the test environment.
*/

vi.mock('@mui/x-date-pickers/DateTimePicker', async () => {
const actual = await vi.importActual(
'@mui/x-date-pickers/DesktopDateTimePicker',
);
return {
DateTimePicker: jest.requireActual(
'@mui/x-date-pickers/DesktopDateTimePicker',
).DesktopDateTimePicker,
DateTimePicker: actual.DesktopDateTimePicker,
};
});

jest.mock('react-toastify', () => ({
/**
* Mocking the react-toastify library to intercept toast calls during tests.
* Provides mock implementations for success and error toast functions.
*/

vi.mock('react-toastify', () => ({
toast: {
success: jest.fn(),
error: jest.fn(),
success: vi.fn(),
error: vi.fn(),
},
}));

vi.mock('@dicebear/core', () => ({
createAvatar: vi.fn(() => ({
toDataUri: vi.fn(() => 'mocked-data-uri'),
})),
}));

const props = {
id: 'rishav-jha-mech',
};
Expand Down Expand Up @@ -87,10 +105,10 @@ const renderMemberDetailScreen = (link: ApolloLink): RenderResult => {
};

describe('MemberDetail', () => {
global.alert = jest.fn();
global.alert = vi.fn();

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

Expand All @@ -115,7 +133,7 @@ describe('MemberDetail', () => {

test('prettyDate function should work properly', () => {
// If the date is provided
const datePretty = jest.fn(prettyDate);
const datePretty = vi.fn(prettyDate);
expect(datePretty('2023-02-18T09:22:27.969Z')).toBe(
prettyDate('2023-02-18T09:22:27.969Z'),
);
Expand All @@ -124,7 +142,7 @@ describe('MemberDetail', () => {
});

test('getLanguageName function should work properly', () => {
const getLangName = jest.fn(getLanguageName);
const getLangName = vi.fn(getLanguageName);
// If the language code is provided
expect(getLangName('en')).toBe('English');
// If the language code is not provided
Expand Down Expand Up @@ -229,7 +247,7 @@ describe('MemberDetail', () => {

expect(screen.queryByText('Loading data...')).not.toBeInTheDocument();

const dicebearUrl = `mocked-data-uri`;
const dicebearUrl = 'mocked-data-uri';

const userImage = await screen.findByTestId('userImageAbsent');
expect(userImage).toBeInTheDocument();
Expand Down
Loading