Skip to content

Commit

Permalink
Merge branch 'develop-postgres' into Events-jest-to-vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
abbi4code authored Dec 20, 2024
2 parents c246c41 + 4b45d85 commit 9931a8e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 34 deletions.
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default {
'!**/index.{js,ts}',
'!**/*.d.ts',
'!src/test/**',
'!vitest.config.ts',],
'!vitest.config.ts',
],
// setupFiles: ['react-app-polyfill/jsdom'],
setupFiles: ['whatwg-fetch'],
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
Expand Down Expand Up @@ -42,6 +43,7 @@ export default {
'\\.svg\\?react$': '<rootDir>/scripts/__mocks__/fileMock.js',
'\\.svg$': '<rootDir>/scripts/__mocks__/fileMock.js',
'^@pdfme/generator$': '<rootDir>/scripts/__mocks__/@pdfme/generator.ts',
'\\.(css|less|scss|sass)$': 'identity-obj-proxy',
},
moduleFileExtensions: [
'web.js',
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions src/components/OrganizationScreen/OrganizationScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,13 @@ describe('Testing OrganizationScreen', () => {

const closeButton = screen.getByTestId('closeMenu');
fireEvent.click(closeButton);

// Check for contract class after closing
expect(screen.getByTestId('mainpageright')).toHaveClass('_expand_ccl5z_8');
expect(screen.getByTestId('mainpageright')).toHaveClass(styles.expand);

const openButton = screen.getByTestId('openMenu');
fireEvent.click(openButton);

// Check for expand class after opening
expect(screen.getByTestId('mainpageright')).toHaveClass(
'_contract_ccl5z_61',
);
expect(screen.getByTestId('mainpageright')).toHaveClass(styles.contract);
});

test('handles window resize', () => {
Expand Down
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,28 @@ const translations = {
),
};

jest.mock('@mui/x-date-pickers/DateTimePicker', () => {
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', () => ({
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 +95,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 +123,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 +132,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 +237,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
3 changes: 3 additions & 0 deletions src/screens/OrganizationPeople/AddMember.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ function AddMember(): JSX.Element {
function openAddUserModal(): void {
setAddUserModalIsOpen(true);
}
useEffect(() => {
setUserName('');
}, [addUserModalisOpen]);

const toggleDialogModal = (): void =>
setAddUserModalIsOpen(!addUserModalisOpen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { act } from 'react';
import { MockedProvider } from '@apollo/react-testing';
import { fireEvent, render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import type { Params } from 'react-router-dom';
import { BrowserRouter } from 'react-router-dom';
import userEvent from '@testing-library/user-event';
import { I18nextProvider } from 'react-i18next';
Expand All @@ -13,14 +14,37 @@ import {
USERS_CONNECTION_LIST,
USER_LIST_FOR_TABLE,
} from 'GraphQl/Queries/Queries';
import 'jest-location-mock';
// import 'jest-location-mock';
import i18nForTest from 'utils/i18nForTest';
import { StaticMockLink } from 'utils/StaticMockLink';
import {
ADD_MEMBER_MUTATION,
SIGNUP_MUTATION,
} from 'GraphQl/Mutations/mutations';
import type { TestMock } from './MockDataTypes';
import { vi } from 'vitest';

/**
* Mock window.location for testing redirection behavior.
*/

Object.defineProperty(window, 'location', {
value: {
href: 'http://localhost/',
assign: vi.fn((url) => {
const urlObj = new URL(url, 'http://localhost');
window.location.href = urlObj.href;
window.location.pathname = urlObj.pathname;
window.location.search = urlObj.search;
window.location.hash = urlObj.hash;
}),
reload: vi.fn(),
pathname: '/',
search: '',
hash: '',
origin: 'http://localhost',
},
});

const createMemberMock = (
orgId = '',
Expand Down Expand Up @@ -596,14 +620,18 @@ async function wait(ms = 2): Promise<void> {
});
}
const linkURL = 'orgid';
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({ orgId: linkURL }),
}));

vi.mock('react-router-dom', async () => {
const actualDom = await vi.importActual('react-router-dom');
return {
...actualDom,
useParams: (): Readonly<Params<string>> => ({ orgId: linkURL }),
};
});

// TODO - REMOVE THE NEXT LINE IT IS TO SUPPRESS THE ERROR
// FOR THE FIRST TEST WHICH CAME OUT OF NOWHERE
console.error = jest.fn();
console.error = vi.fn();

describe('Organization People Page', () => {
const searchData = {
Expand Down Expand Up @@ -683,7 +711,7 @@ describe('Organization People Page', () => {
},
]);

expect(window.location).toBeAt('/orgpeople/orgid');
expect(window.location.href).toBe('http://localhost/orgpeople/orgid');
});

test('It is necessary to query the correct mock data.', async () => {
Expand All @@ -705,7 +733,7 @@ describe('Organization People Page', () => {

await wait();

expect(window.location).toBeAt('/orgpeople/orgid');
expect(window.location.href).toBe('http://localhost/orgpeople/orgid');
});

test('Testing MEMBERS list', async () => {
Expand Down Expand Up @@ -753,7 +781,7 @@ describe('Organization People Page', () => {
);

await wait();
expect(window.location).toBeAt('/orgpeople/orgid');
expect(window.location.href).toBe('http://localhost/orgpeople/orgid');
});

test('Testing MEMBERS list with filters', async () => {
Expand Down Expand Up @@ -792,7 +820,7 @@ describe('Organization People Page', () => {
await wait();
expect(findtext).toBeInTheDocument();
await wait();
expect(window.location).toBeAt('/orgpeople/orgid');
expect(window.location.href).toBe('http://localhost/orgpeople/orgid');
});

test('Testing ADMIN LIST', async () => {
Expand Down Expand Up @@ -855,7 +883,7 @@ describe('Organization People Page', () => {

// Wait for any asynchronous operations to complete
await wait();
expect(window.location).toBeAt('/orgpeople/orgid');
expect(window.location.href).toBe('http://localhost/orgpeople/orgid');
});

test('Testing ADMIN list with filters', async () => {
Expand Down Expand Up @@ -905,7 +933,7 @@ describe('Organization People Page', () => {

// Ensure that the name is still present after filtering
await wait();
expect(window.location).toBeAt('/orgpeople/orgid');
expect(window.location.href).toBe('http://localhost/orgpeople/orgid');
});

test('Testing add existing user modal', async () => {
Expand Down Expand Up @@ -1256,7 +1284,9 @@ describe('Organization People Page', () => {
const btn = screen.getByTestId('searchbtn');
userEvent.click(btn);
await wait();
expect(window.location).toBeAt('/orgpeople/6401ff65ce8e8406b8f07af1');
expect(window.location.href).toBe(
'http://localhost/orgpeople/6401ff65ce8e8406b8f07af1',
);
});

test('Testing USERS list with filters', async () => {
Expand Down Expand Up @@ -1289,7 +1319,9 @@ describe('Organization People Page', () => {
const btn = screen.getByTestId('searchbtn');
userEvent.click(btn);
await wait();
expect(window.location).toBeAt('/orgpeople/6401ff65ce8e8406b8f07af2');
expect(window.location.href).toBe(
'http://localhost/orgpeople/6401ff65ce8e8406b8f07af2',
);
});

test('Add Member component renders', async () => {
Expand Down Expand Up @@ -1378,7 +1410,7 @@ describe('Organization People Page', () => {
);

await wait();
expect(window.location).toBeAt('/orgpeople/orgid');
expect(window.location.href).toBe('http://localhost/orgpeople/orgid');
expect(screen.queryByText(/Nothing Found !!/i)).toBeInTheDocument();
});
});
Expand Down

0 comments on commit 9931a8e

Please sign in to comment.