From 9739d26529997015b4b1d149af7ae21073cbb85f Mon Sep 17 00:00:00 2001 From: abhishek Date: Wed, 25 Dec 2024 16:29:36 +0530 Subject: [PATCH] updateSession tests migrated from jest to vitest --- ...ession.test.tsx => UpdateSession.spec.tsx} | 67 ++++++++++--------- 1 file changed, 34 insertions(+), 33 deletions(-) rename src/components/UpdateSession/{UpdateSession.test.tsx => UpdateSession.spec.tsx} (81%) diff --git a/src/components/UpdateSession/UpdateSession.test.tsx b/src/components/UpdateSession/UpdateSession.spec.tsx similarity index 81% rename from src/components/UpdateSession/UpdateSession.test.tsx rename to src/components/UpdateSession/UpdateSession.spec.tsx index ce0a868820..3453c990c2 100644 --- a/src/components/UpdateSession/UpdateSession.test.tsx +++ b/src/components/UpdateSession/UpdateSession.spec.tsx @@ -1,16 +1,7 @@ import React from 'react'; import { MockedProvider } from '@apollo/client/testing'; -import { - render, - screen, - act, - within, - fireEvent, - waitFor, -} from '@testing-library/react'; +import { render, screen, act, within } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import 'jest-localstorage-mock'; -import 'jest-location-mock'; import { I18nextProvider } from 'react-i18next'; import { BrowserRouter } from 'react-router-dom'; import { toast } from 'react-toastify'; @@ -19,6 +10,18 @@ import i18n from 'utils/i18nForTest'; import { GET_COMMUNITY_SESSION_TIMEOUT_DATA } from 'GraphQl/Queries/Queries'; import { UPDATE_SESSION_TIMEOUT } from 'GraphQl/Mutations/mutations'; import { errorHandler } from 'utils/errorHandler'; +import { vi } from 'vitest'; +/** + * This file contains unit tests for the `UpdateSession` component. + * + * The tests cover: + * - Proper rendering of the component with different scenarios, including mock data, null values, and error states. + * - Handling user interactions with the slider, such as setting minimum, maximum, and intermediate values. + * - Ensuring callbacks (e.g., `onValueChange`) are triggered correctly based on user input. + * - Simulating GraphQL query and mutation operations using mocked data to verify correct behavior in successful and error cases. + * - Testing edge cases, including null community data, invalid input values, and API errors, ensuring the component handles them gracefully. + * - Verifying proper integration of internationalization, routing, and toast notifications for success or error messages. + */ const MOCKS = [ { @@ -66,31 +69,29 @@ async function wait(ms = 100): Promise { }); } -jest.mock('react-toastify', () => ({ +vi.mock('react-toastify', () => ({ toast: { - success: jest.fn(), - warn: jest.fn(), - error: jest.fn(), + success: vi.fn(), + warn: vi.fn(), + error: vi.fn(), }, })); -jest.mock('utils/errorHandler', () => ({ - errorHandler: jest.fn(), +vi.mock('utils/errorHandler', () => ({ + errorHandler: vi.fn(), })); describe('Testing UpdateTimeout Component', () => { - let consoleWarnSpy: jest.SpyInstance; - beforeEach(() => { - consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(); + vi.spyOn(console, 'warn').mockImplementation(() => {}); }); afterEach(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); }); - test('Should handle minimum slider value correctly', async () => { - const mockOnValueChange = jest.fn(); + it('Should handle minimum slider value correctly', async () => { + const mockOnValueChange = vi.fn(); render( @@ -109,8 +110,8 @@ describe('Testing UpdateTimeout Component', () => { expect(mockOnValueChange).toHaveBeenCalledWith(15); // Adjust based on slider min value }); - test('Should handle maximum slider value correctly', async () => { - const mockOnValueChange = jest.fn(); + it('Should handle maximum slider value correctly', async () => { + const mockOnValueChange = vi.fn(); render( @@ -129,8 +130,8 @@ describe('Testing UpdateTimeout Component', () => { expect(mockOnValueChange).toHaveBeenCalledWith(60); // Adjust based on slider max value }); - test('Should not update value if an invalid value is passed', async () => { - const mockOnValueChange = jest.fn(); + it('Should not update value if an invalid value is passed', async () => { + const mockOnValueChange = vi.fn(); render( @@ -150,8 +151,8 @@ describe('Testing UpdateTimeout Component', () => { expect(mockOnValueChange).not.toHaveBeenCalled(); }); - test('Should update slider value on user interaction', async () => { - const mockOnValueChange = jest.fn(); + it('Should update slider value on user interaction', async () => { + const mockOnValueChange = vi.fn(); render( @@ -169,7 +170,7 @@ describe('Testing UpdateTimeout Component', () => { expect(mockOnValueChange).toHaveBeenCalledWith(expect.any(Number)); // Adjust as needed }); - test('Components should render properly', async () => { + it('Components should render properly', async () => { render( @@ -203,7 +204,7 @@ describe('Testing UpdateTimeout Component', () => { expect(screen.getByRole('button', { name: /Update/i })).toBeInTheDocument(); }); - test('Should update session timeout', async () => { + it('Should update session timeout', async () => { render( @@ -228,7 +229,7 @@ describe('Testing UpdateTimeout Component', () => { ); }); - test('Should handle query errors', async () => { + it('Should handle query errors', async () => { const errorMocks = [ { request: { @@ -253,7 +254,7 @@ describe('Testing UpdateTimeout Component', () => { expect(errorHandler).toHaveBeenCalled(); }); - test('Should handle update errors', async () => { + it('Should handle update errors', async () => { const errorMocks = [ { request: { @@ -306,7 +307,7 @@ describe('Testing UpdateTimeout Component', () => { expect(errorHandler).toHaveBeenCalled(); }); - test('Should handle null community object gracefully', async () => { + it('Should handle null community object gracefully', async () => { render(