Skip to content

Commit

Permalink
Merge branch 'issue-2756' of https://github.com/Ramneet04/talawa-admin
Browse files Browse the repository at this point in the history
…into issue-2756
  • Loading branch information
Ramneet04 committed Dec 25, 2024
2 parents 0bce5ad + ecc49d7 commit 5a66210
Show file tree
Hide file tree
Showing 41 changed files with 3,270 additions and 2,437 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { render } from '@testing-library/react';
import { describe, it, expect } from 'vitest';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';

Expand All @@ -11,7 +12,7 @@ describe('Testing MainContent component', () => {
children: 'This is a dummy text',
};

test('should render props and children for the Main Content', () => {
it('should render props and children for the Main Content', () => {
const { getByTestId, getByText } = render(
<BrowserRouter>
<Provider store={store}>
Expand All @@ -20,7 +21,7 @@ describe('Testing MainContent component', () => {
</BrowserRouter>,
);

expect(getByTestId('mainContentCheck')).toBeInTheDocument();
expect(getByText(props.children)).toBeInTheDocument();
expect(getByTestId('mainContentCheck')).not.toBeNull();
expect(getByText(props.children)).not.toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { act } from 'react';
import { describe, test, expect, vi } from 'vitest';

import {
ApolloClient,
ApolloLink,
Expand All @@ -7,25 +9,28 @@ import {
InMemoryCache,
} from '@apollo/client';
import { MockedProvider } from '@apollo/client/testing';

import { fireEvent, render, screen } from '@testing-library/react';
import 'jest-location-mock';

import type { DocumentNode, NormalizedCacheObject } from '@apollo/client';
import userEvent from '@testing-library/user-event';
import { BACKEND_URL } from 'Constant/constant';
import { ADD_ADVERTISEMENT_MUTATION } from 'GraphQl/Mutations/mutations';

import { ADD_ADVERTISEMENT_MUTATION } from '../../GraphQl/Mutations/mutations';
import {
ORGANIZATIONS_LIST,
ORGANIZATION_ADVERTISEMENT_LIST,
PLUGIN_GET,
} from 'GraphQl/Queries/Queries';
} from '../../GraphQl/Queries/Queries';

import { I18nextProvider } from 'react-i18next';

import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import { ToastContainer } from 'react-toastify';
import { store } from 'state/store';
import i18nForTest from 'utils/i18nForTest';
import useLocalStorage from 'utils/useLocalstorage';
import { store } from '../../state/store';
import i18nForTest from '../../utils/i18nForTest';
import useLocalStorage from '../../utils/useLocalstorage';
import Advertisement from './Advertisements';

const { getItem } = useLocalStorage();
Expand All @@ -50,18 +55,22 @@ const client: ApolloClient<NormalizedCacheObject> = new ApolloClient({
link: ApolloLink.from([httpLink]),
});

jest.mock('components/AddOn/support/services/Plugin.helper', () => ({
vi.mock('components/AddOn/support/services/Plugin.helper', () => ({
__esModule: true,
default: jest.fn().mockImplementation(() => ({
fetchInstalled: jest.fn().mockResolvedValue([]),
fetchStore: jest.fn().mockResolvedValue([]),
default: vi.fn().mockImplementation(() => ({
fetchInstalled: vi.fn().mockResolvedValue([]),
fetchStore: vi.fn().mockResolvedValue([]),
})),
}));
let mockID: string | undefined = '1';
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({ orgId: mockID }),
}));

vi.mock('react-router-dom', async () => {
const actual = await vi.importActual('react-router-dom');
return {
...actual,
useParams: () => ({ orgId: mockID }),
};
});

const today = new Date();
const tomorrow = today;
Expand Down Expand Up @@ -466,18 +475,16 @@ describe('Testing Advertisement Component', () => {
/\b(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{4})\b/,
);
let dateObject = new Date();

if (dateMatch) {
const monthName = dateMatch[1];
const day = parseInt(dateMatch[2], 10);
const year = parseInt(dateMatch[3], 10);

const monthIndex =
'JanFebMarAprMayJunJulAugSepOctNovDec'.indexOf(monthName) / 3;

dateObject = new Date(year, monthIndex, day);
}

console.log(dateObject);
expect(dateObject.getTime()).toBeLessThan(new Date().getTime());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import {
fireEvent,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import 'jest-localstorage-mock';
import { MockedProvider } from '@apollo/client/testing';
import 'jest-location-mock';
import { I18nextProvider } from 'react-i18next';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
Expand All @@ -24,20 +22,20 @@ import { StaticMockLink } from 'utils/StaticMockLink';
import { props, props2 } from './AgendaItemsContainerProps';
import { MOCKS, MOCKS_ERROR } from './AgendaItemsContainerMocks';
import AgendaItemsContainer from './AgendaItemsContainer';

import { describe, test, expect, vi } from 'vitest';
const link = new StaticMockLink(MOCKS, true);
const link2 = new StaticMockLink(MOCKS_ERROR, true);

jest.mock('react-toastify', () => ({
vi.mock('react-toastify', () => ({
toast: {
success: jest.fn(),
error: jest.fn(),
success: vi.fn(),
error: vi.fn(),
},
}));

//temporarily fixes react-beautiful-dnd droppable method's depreciation error
//needs to be fixed in React 19
jest.spyOn(console, 'error').mockImplementation((message) => {
vi.spyOn(console, 'error').mockImplementation((message) => {
if (message.includes('Support for defaultProps will be removed')) {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions src/components/AgendaItems/AgendaItemsContainerProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type AgendaItemConnectionType = 'Event';
import { vi } from 'vitest';

export const props = {
agendaItemConnection: 'Event' as AgendaItemConnectionType,
Expand Down Expand Up @@ -68,7 +69,7 @@ export const props = {
},
},
],
agendaItemRefetch: jest.fn(),
agendaItemRefetch: vi.fn(),
agendaItemCategories: [
{
_id: 'agendaCategory1',
Expand Down Expand Up @@ -96,6 +97,6 @@ export const props = {
export const props2 = {
agendaItemConnection: 'Event' as AgendaItemConnectionType,
agendaItemData: [],
agendaItemRefetch: jest.fn(),
agendaItemRefetch: vi.fn(),
agendaItemCategories: [],
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';

import AgendaItemsPreviewModal from './AgendaItemsPreviewModal';
import { describe, test, expect, vi } from 'vitest';

const mockFormState = {
title: 'Test Title',
Expand Down Expand Up @@ -44,10 +45,10 @@ describe('AgendaItemsPreviewModal', () => {
<LocalizationProvider dateAdapter={AdapterDayjs}>
<AgendaItemsPreviewModal
agendaItemPreviewModalIsOpen
hidePreviewModal={jest.fn()}
hidePreviewModal={vi.fn()}
formState={mockFormState}
showUpdateModal={jest.fn()}
toggleDeleteModal={jest.fn()}
showUpdateModal={vi.fn()}
toggleDeleteModal={vi.fn()}
t={mockT}
/>
</LocalizationProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import { store } from 'state/store';
import i18nForTest from 'utils/i18nForTest';

import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';

import AgendaItemsUpdateModal from './AgendaItemsUpdateModal';
import { toast } from 'react-toastify';
import convertToBase64 from 'utils/convertToBase64';
import type { MockedFunction } from 'vitest';
import { describe, test, expect, vi } from 'vitest';

const mockFormState = {
title: 'Test Title',
Expand Down Expand Up @@ -67,19 +68,19 @@ const mockAgendaItemCategories = [
},
];

const mockHideUpdateModal = jest.fn();
const mockSetFormState = jest.fn();
const mockUpdateAgendaItemHandler = jest.fn();
const mockHideUpdateModal = vi.fn();
const mockSetFormState = vi.fn();
const mockUpdateAgendaItemHandler = vi.fn();
const mockT = (key: string): string => key;

jest.mock('react-toastify', () => ({
vi.mock('react-toastify', () => ({
toast: {
success: jest.fn(),
error: jest.fn(),
success: vi.fn(),
error: vi.fn(),
},
}));
jest.mock('utils/convertToBase64');
const mockedConvertToBase64 = convertToBase64 as jest.MockedFunction<
vi.mock('utils/convertToBase64');
const mockedConvertToBase64 = convertToBase64 as MockedFunction<
typeof convertToBase64
>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { MockedProvider } from '@apollo/react-testing';
import { checkInMutationSuccess, checkInMutationUnsuccess } from './mocks';
import { vi } from 'vitest';

/**
* Test suite for the `TableRow` component, focusing on the CheckIn table functionality.
*/

describe('Testing Table Row for CheckIn Table', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('If the user is not checked in, button to check in should be displayed, and the user should be able to check in successfully', async () => {
Expand All @@ -26,7 +31,7 @@ describe('Testing Table Row for CheckIn Table', () => {
checkIn: null,
eventId: `event123`,
},
refetch: jest.fn(),
refetch: vi.fn(),
};

const { findByText } = render(
Expand Down Expand Up @@ -63,7 +68,7 @@ describe('Testing Table Row for CheckIn Table', () => {
},
eventId: 'event123',
},
refetch: jest.fn(),
refetch: vi.fn(),
};

const { findByText } = render(
Expand All @@ -81,8 +86,8 @@ describe('Testing Table Row for CheckIn Table', () => {
</BrowserRouter>,
);

global.URL.createObjectURL = jest.fn(() => 'mockURL');
global.window.open = jest.fn();
global.URL.createObjectURL = vi.fn(() => 'mockURL');
global.window.open = vi.fn();

expect(await findByText('Checked In')).toBeInTheDocument();
expect(await findByText('Download Tag')).toBeInTheDocument();
Expand All @@ -93,7 +98,7 @@ describe('Testing Table Row for CheckIn Table', () => {
expect(await findByText('PDF generated successfully!')).toBeInTheDocument();

// Cleanup mocks
jest.clearAllMocks();
vi.clearAllMocks();
});

test('Upon failing of check in mutation, the appropriate error message should be shown', async () => {
Expand All @@ -105,7 +110,7 @@ describe('Testing Table Row for CheckIn Table', () => {
checkIn: null,
eventId: `event123`,
},
refetch: jest.fn(),
refetch: vi.fn(),
};

const { findByText } = render(
Expand Down Expand Up @@ -143,7 +148,7 @@ describe('Testing Table Row for CheckIn Table', () => {
},
eventId: `event123`,
},
refetch: jest.fn(),
refetch: vi.fn(),
};

const { findByText } = render(
Expand All @@ -162,8 +167,8 @@ describe('Testing Table Row for CheckIn Table', () => {
);

// Mocking the PDF generation function to throw an error
global.URL.createObjectURL = jest.fn(() => 'mockURL');
global.window.open = jest.fn();
global.URL.createObjectURL = vi.fn(() => 'mockURL');
global.window.open = vi.fn();

fireEvent.click(await findByText('Download Tag'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import { BrowserRouter } from 'react-router-dom';
import { I18nextProvider } from 'react-i18next';
import i18nForTest from 'utils/i18nForTest';
import userEvent from '@testing-library/user-event';
import { vi, expect, it } from 'vitest';

describe('DynamicDropDown component', () => {
test('renders and handles selection correctly', async () => {
it('renders and handles selection correctly', async () => {
const formData = { fieldName: 'value2' };
const setFormData = jest.fn();
const setFormData = vi.fn();

render(
<BrowserRouter>
Expand Down Expand Up @@ -60,10 +61,10 @@ describe('DynamicDropDown component', () => {
expect(dropdownButton).toHaveTextContent('Label 2');
});
});
test('calls custom handleChange function when provided', async () => {
it('calls custom handleChange function when provided', async () => {
const formData = { fieldName: 'value1' };
const setFormData = jest.fn();
const customHandleChange = jest.fn();
const setFormData = vi.fn();
const customHandleChange = vi.fn();

render(
<BrowserRouter>
Expand Down Expand Up @@ -103,9 +104,9 @@ describe('DynamicDropDown component', () => {
);
expect(setFormData).not.toHaveBeenCalled();
});
test('handles keyboard navigation correctly', async () => {
it('handles keyboard navigation correctly', async () => {
const formData = { fieldName: 'value1' };
const setFormData = jest.fn();
const setFormData = vi.fn();

render(
<BrowserRouter>
Expand Down
Loading

0 comments on commit 5a66210

Please sign in to comment.