Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
duplixx committed Nov 1, 2024
1 parent 66de4cb commit bfcf39b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 78 deletions.
103 changes: 60 additions & 43 deletions src/components/MemberDetail/EventsAttendedMemberModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,100 +4,117 @@ import { MockedProvider } from '@apollo/client/testing';
import { BrowserRouter } from 'react-router-dom';
import EventsAttendedMemberModal from './EventsAttendedMemberModal';

// Mock translations
jest.mock('react-i18next', () => ({
useTranslation: () => ({ t: (key: string) => key }),
useTranslation: () => ({
t: (key: string) => key,
i18n: { changeLanguage: () => Promise.resolve() },
}),
}));

const mockEvents = [
{ _id: 'event1' },
{ _id: 'event2' },
{ _id: 'event3' },
{ _id: 'event4' },
{ _id: 'event5' },
{ _id: 'event6' },
];

describe('MemberAttendedEventsModal', () => {
it('renders modal when show is true', () => {
// Mock event data
const mockEvents = Array.from({ length: 6 }, (_, index) => ({
_id: `event${index + 1}`,
name: `Event ${index + 1}`,
date: new Date().toISOString().split('T')[0],
isRecurring: false,
attendees: 5,
description: `Description ${index + 1}`,
location: `Location ${index + 1}`,
startTime: '10:00',
endTime: '11:00',
}));

describe('EventsAttendedMemberModal', () => {
const defaultProps = {
eventsAttended: mockEvents,
setShow: jest.fn(),
show: true,
};

beforeEach(() => {
jest.clearAllMocks();
});

test('renders modal with correct title when show is true', () => {
render(
<MockedProvider>
<BrowserRouter>
<EventsAttendedMemberModal
eventsAttended={mockEvents}
setShow={() => {}}
show={true}
/>
<EventsAttendedMemberModal {...defaultProps} />
</BrowserRouter>
</MockedProvider>,
);

expect(screen.getByText('Events Attended List')).toBeInTheDocument();
expect(screen.getByText('Showing 1 - 5 of 6 Events')).toBeInTheDocument();
});

it('displays no events message when eventsAttended is empty', () => {
test('displays empty state message when no events', () => {
render(
<MockedProvider>
<BrowserRouter>
<EventsAttendedMemberModal
eventsAttended={[]}
setShow={() => {}}
show={true}
/>
<EventsAttendedMemberModal {...defaultProps} eventsAttended={[]} />
</BrowserRouter>
</MockedProvider>,
);

expect(screen.getByText('noeventsAttended')).toBeInTheDocument();
});

it('renders correct number of events per page', () => {
test('renders correct number of events per page', () => {
render(
<MockedProvider>
<BrowserRouter>
<EventsAttendedMemberModal
eventsAttended={mockEvents}
setShow={() => {}}
show={true}
/>
<EventsAttendedMemberModal {...defaultProps} />
</BrowserRouter>
</MockedProvider>,
);

expect(screen.getAllByTestId('custom-row')).toHaveLength(5);
const eventRows = screen.getAllByTestId('custom-row');
expect(eventRows).toHaveLength(5);
expect(screen.getByText('Event 1')).toBeInTheDocument();
expect(screen.getByText('Event 5')).toBeInTheDocument();
});

it('updates page when pagination is clicked', () => {
test('handles pagination correctly', () => {
render(
<MockedProvider>
<BrowserRouter>
<EventsAttendedMemberModal
eventsAttended={mockEvents}
setShow={() => {}}
show={true}
/>
<EventsAttendedMemberModal {...defaultProps} />
</BrowserRouter>
</MockedProvider>,
);

fireEvent.click(screen.getByRole('button', { name: '2' }));
expect(screen.getByText('Showing 1 - 5 of 6 Events')).toBeInTheDocument();
expect(screen.getByText('Event 6')).toBeInTheDocument();
});

it('calls setShow when close button is clicked', () => {
test('closes modal when close button is clicked', () => {
const mockSetShow = jest.fn();
render(
<MockedProvider>
<BrowserRouter>
<EventsAttendedMemberModal
eventsAttended={mockEvents}
setShow={mockSetShow}
show={true}
/>
<EventsAttendedMemberModal {...defaultProps} setShow={mockSetShow} />
</BrowserRouter>
</MockedProvider>,
);

fireEvent.click(screen.getByRole('button', { name: 'Close' }));
expect(mockSetShow).toHaveBeenCalledWith(false);
expect(mockSetShow).toHaveBeenCalledTimes(1);
});

test('displays correct pagination info', () => {
render(
<MockedProvider>
<BrowserRouter>
<EventsAttendedMemberModal {...defaultProps} />
</BrowserRouter>
</MockedProvider>,
);

expect(screen.getByText('Showing 1 - 5 of 6 Events')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: '2' }));
expect(screen.getByText('Showing 6 - 6 of 6 Events')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { act } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';
import { act, fireEvent, render, screen } from '@testing-library/react';

import { MockedProvider } from '@apollo/react-testing';
import { I18nextProvider } from 'react-i18next';

Expand Down
10 changes: 5 additions & 5 deletions src/screens/EventManagement/EventManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,8 @@ const EventManagement = (): JSX.Element => {

const handleBack = (): void => {
/*istanbul ignore next*/
userRole === 'USER'
? navigate(`/user/events/${orgId}`)
: navigate(`/orgevents/${orgId}`);
const route = userRole === 'USER' ? 'user/events' : 'orgevents';
navigate(`/${route}/${orgId}`);
};

return (
Expand Down Expand Up @@ -197,7 +196,7 @@ const EventManagement = (): JSX.Element => {
/* istanbul ignore next */
() => setTab(value)
}
className={`d-flex gap-2 ${tab === value && 'text-secondary'}`}
className={`d-flex gap-2 ${tab === value ? 'text-secondary' : ''}`}
>
{icon} {t(value)}
</Dropdown.Item>
Expand Down Expand Up @@ -250,10 +249,11 @@ const EventManagement = (): JSX.Element => {
<h2>Event Statistics</h2>
</div>
);
default:
return null;
}
})()}
</div>
);
};

export default EventManagement;
1 change: 0 additions & 1 deletion src/screens/MemberDetail/MemberDetail.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { USER_DETAILS } from 'GraphQl/Queries/Queries';
import i18nForTest from 'utils/i18nForTest';
import { StaticMockLink } from 'utils/StaticMockLink';
import MemberDetail, { getLanguageName, prettyDate } from './MemberDetail';
import { toast } from 'react-toastify';

const MOCKS1 = [
{
Expand Down
27 changes: 0 additions & 27 deletions src/screens/UserPortal/Settings/Settings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,33 +109,6 @@ const Mocks2 = [
},
];

const mockMaritalStatusEnum = [
{
value: 'SINGLE',
label: 'Single',
},
{
value: 'ENGAGED',
label: 'Engaged',
},
{
value: 'MARRIED',
label: 'Married',
},
{
value: 'DIVORCED',
label: 'Divorced',
},
{
value: 'WIDOWED',
label: 'Widowed',
},
{
value: 'SEPARATED',
label: 'Separated',
},
];

const link = new StaticMockLink(MOCKS, true);
const link1 = new StaticMockLink(Mocks1, true);
const link2 = new StaticMockLink(Mocks2, true);
Expand Down

0 comments on commit bfcf39b

Please sign in to comment.