From 2d58498e9c6b1a16ad27f0df6bd5adfd17b46f61 Mon Sep 17 00:00:00 2001 From: Peter Harrison <16875803+palisadoes@users.noreply.github.com> Date: Sun, 19 Jan 2025 19:08:35 -0800 Subject: [PATCH] Revert "Improved Code Coverage of UserListCard.tsx (#3230)" (#3358) This reverts commit 477ea1182c9c345320e9ed89618790a5a376402a. --- .../UserListCard/UserListCard.spec.tsx | 148 +++--------------- src/components/UserListCard/UserListCard.tsx | 2 + 2 files changed, 20 insertions(+), 130 deletions(-) diff --git a/src/components/UserListCard/UserListCard.spec.tsx b/src/components/UserListCard/UserListCard.spec.tsx index e278a4bcc3..6a3b2d42d8 100644 --- a/src/components/UserListCard/UserListCard.spec.tsx +++ b/src/components/UserListCard/UserListCard.spec.tsx @@ -1,46 +1,21 @@ import React from 'react'; -import { render, screen, waitFor } from '@testing-library/react'; +import { render, screen, act } from '@testing-library/react'; import { MockedProvider } from '@apollo/react-testing'; import userEvent from '@testing-library/user-event'; import { I18nextProvider } from 'react-i18next'; -import { toast } from 'react-toastify'; import UserListCard from './UserListCard'; import { ADD_ADMIN_MUTATION } from 'GraphQl/Mutations/mutations'; import i18nForTest from 'utils/i18nForTest'; import { BrowserRouter } from 'react-router-dom'; -import { vi, describe, beforeEach, afterEach } from 'vitest'; - -// Test constants -const TEST_USER_ID = '456'; -const TEST_ORG_ID = '554'; -const TEST_SUCCESS_MESSAGE = 'User is added as admin.'; -const TEST_ERROR_MESSAGE = 'An error occurred'; -const DEFAULT_TIMEOUT = 2000; - -// Mock modules -vi.mock('react-toastify', () => ({ - toast: { - success: vi.fn(), - error: vi.fn(), - }, -})); - -vi.mock('react-router-dom', async () => { - const actual = await vi.importActual('react-router-dom'); - return { - ...actual, - useParams: () => ({ - orgId: TEST_ORG_ID, - }), - }; -}); +import { StaticMockLink } from 'utils/StaticMockLink'; +import { vi, describe, it, beforeEach } from 'vitest'; const MOCKS = [ { request: { query: ADD_ADMIN_MUTATION, - variables: { userid: TEST_USER_ID, orgid: TEST_ORG_ID }, + variables: { userid: '784', orgid: '554' }, }, result: { data: { @@ -53,41 +28,24 @@ const MOCKS = [ }, }, ]; +const link = new StaticMockLink(MOCKS, true); -const ERROR_MOCKS = [ - { - request: { - query: ADD_ADMIN_MUTATION, - variables: { userid: TEST_USER_ID, orgid: TEST_ORG_ID }, - }, - error: new Error(TEST_ERROR_MESSAGE), - }, -]; +async function wait(ms = 100): Promise { + await act(() => new Promise((resolve) => setTimeout(resolve, ms))); +} describe('Testing User List Card', () => { - const mockReload = vi.fn(); - beforeEach(() => { vi.spyOn(global, 'alert').mockImplementation(() => {}); - Object.defineProperty(window, 'location', { - value: { - reload: mockReload, - }, - writable: true, - }); - }); - - afterEach(() => { - vi.clearAllMocks(); }); - test('Should show success toast and reload page after successful mutation', async () => { + it('Should render props and text elements test for the page component', async () => { const props = { - id: TEST_USER_ID, + id: '456', }; render( - + @@ -96,31 +54,17 @@ describe('Testing User List Card', () => { , ); - const button = screen.getByText(/Add Admin/i); - await userEvent.click(button); - - await waitFor( - () => { - expect(toast.success).toHaveBeenCalledWith(TEST_SUCCESS_MESSAGE); - }, - { timeout: DEFAULT_TIMEOUT }, - ); - - await waitFor( - () => { - expect(mockReload).toHaveBeenCalled(); - }, - { timeout: DEFAULT_TIMEOUT }, - ); + await wait(); + userEvent.click(screen.getByText(/Add Admin/i)); }); - test('Should show error toast when mutation fails', async () => { + it('Should render text elements when props value is not passed', async () => { const props = { - id: TEST_USER_ID, + id: '456', }; render( - + @@ -129,63 +73,7 @@ describe('Testing User List Card', () => { , ); - const button = screen.getByText(/Add Admin/i); - await userEvent.click(button); - - await waitFor( - () => { - expect(toast.error).toHaveBeenCalled(); - }, - { timeout: DEFAULT_TIMEOUT }, - ); - }); - - test('Should render button with correct styling', () => { - const props = { - id: TEST_USER_ID, - key: 1, - }; - - render( - - - - - - - , - ); - - const button = screen.getByRole('button', { name: /Add Admin/i }); - expect(button).toBeInTheDocument(); - expect(button.className).toContain('memberfontcreatedbtn'); - }); - - test('Should handle translations and URL parameters correctly', async () => { - const props = { - id: TEST_USER_ID, - }; - - render( - - - - - - - , - ); - - const button = screen.getByText(/Add Admin/i); - expect(button).toBeInTheDocument(); - - await userEvent.click(button); - - await waitFor( - () => { - expect(toast.success).toHaveBeenCalled(); - }, - { timeout: DEFAULT_TIMEOUT }, - ); + await wait(); + userEvent.click(screen.getByText(/Add Admin/i)); }); }); diff --git a/src/components/UserListCard/UserListCard.tsx b/src/components/UserListCard/UserListCard.tsx index fee79644c2..157c18f404 100644 --- a/src/components/UserListCard/UserListCard.tsx +++ b/src/components/UserListCard/UserListCard.tsx @@ -45,6 +45,7 @@ function userListCard(props: InterfaceUserListCardProps): JSX.Element { }, }); + /* istanbul ignore next */ if (data) { toast.success(t('addedAsAdmin') as string); setTimeout(() => { @@ -52,6 +53,7 @@ function userListCard(props: InterfaceUserListCardProps): JSX.Element { }, 2000); } } catch (error: unknown) { + /* istanbul ignore next */ errorHandler(t, error); } };