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 : Added Vitest to Requests Screen #2654

Closed
Changes from 2 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
@@ -1,8 +1,6 @@
import React, { act } from 'react';
import { MockedProvider } from '@apollo/react-testing';
import { render, screen } from '@testing-library/react';
import 'jest-localstorage-mock';
import 'jest-location-mock';
import { I18nextProvider } from 'react-i18next';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
Expand All @@ -22,6 +20,26 @@ import {
MOCKS4,
} from './RequestsMocks';
import useLocalStorage from 'utils/useLocalstorage';
import { vi } from 'vitest';

/**
* Set up `localStorage` stubs for testing.
*/

vi.stubGlobal('localStorage', {
getItem: vi.fn(),
setItem: vi.fn(),
clear: vi.fn(),
removeItem: vi.fn(),
});

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

Object.defineProperty(window, 'location', {
value: { href: 'http://localhost/', assign: vi.fn(), reload: vi.fn() },
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance window.location mock implementation.

The current implementation could be improved to better match the Window.Location interface:

 /**
  * Mock `window.location` for testing redirection behavior.
+ * 
+ * @example
+ * window.location.href = '/new-path'
+ * expect(window.location.pathname).toBe('/new-path')
  */
 Object.defineProperty(window, 'location', {
   value: {
     href: 'http://localhost/',
     assign: vi.fn(),
     reload: vi.fn(),
+    pathname: '/',
+    search: '',
+    hash: '',
+    origin: 'http://localhost',
   },
 });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* Mock `window.location` for testing redirection behavior.
*/
Object.defineProperty(window, 'location', {
value: { href: 'http://localhost/', assign: vi.fn(), reload: vi.fn() },
});
/**
* Mock `window.location` for testing redirection behavior.
*
* @example
* window.location.href = '/new-path'
* expect(window.location.pathname).toBe('/new-path')
*/
Object.defineProperty(window, 'location', {
value: {
href: 'http://localhost/',
assign: vi.fn(),
reload: vi.fn(),
pathname: '/',
search: '',
hash: '',
origin: 'http://localhost',
},
});


const { setItem, removeItem } = useLocalStorage();

Expand All @@ -33,6 +51,14 @@ const link5 = new StaticMockLink(MOCKS_WITH_ERROR, true);
const link6 = new StaticMockLink(MOCKS3, true);
const link7 = new StaticMockLink(MOCKS4, true);

/**
* Utility function to wait for a specified amount of time.
* Wraps `setTimeout` in an `act` block for testing purposes.
*
* @param ms - The duration to wait in milliseconds. Default is 100ms.
* @returns A promise that resolves after the specified time.
*/

async function wait(ms = 100): Promise<void> {
await act(() => {
return new Promise((resolve) => {
Expand All @@ -53,7 +79,6 @@ afterEach(() => {

describe('Testing Requests screen', () => {
test('Component should be rendered properly', async () => {
const loadMoreRequests = jest.fn();
render(
<MockedProvider addTypename={false} link={link7}>
<BrowserRouter>
Expand Down
Loading