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 OrgPeople Screen #2663

Merged
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 @@ -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
Loading