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

adding vitest in avatar.spec.test #2603

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
82836f0
chore(deps): bump sass from 1.80.6 to 1.80.7 (#2433)
dependabot[bot] Nov 14, 2024
a97314c
chore(deps): bump eslint-plugin-import from 2.30.0 to 2.31.0 (#2434)
dependabot[bot] Nov 14, 2024
32eb6a9
chore(deps): bump @mui/x-charts from 7.22.1 to 7.22.2 (#2435)
dependabot[bot] Nov 14, 2024
ab509f6
chore(deps): bump @types/react from 18.3.3 to 18.3.12 (#2436)
dependabot[bot] Nov 14, 2024
fb913e1
Update pull-request.yml
palisadoes Nov 14, 2024
d16b95e
Update dependabot.yaml
palisadoes Nov 16, 2024
15513f5
added docker check to workflow (#2414)
VanshikaSabharwal Nov 17, 2024
3ea2919
eslint-rule-no_unused_vars (#2428)
prayanshchh Nov 17, 2024
084ac7e
Updated pr template with checklist (#2454)
dhirajudhani Nov 20, 2024
58f289b
Update pull-request-target.yml
palisadoes Nov 25, 2024
7a991b3
Update pull-request-target.yml
palisadoes Nov 25, 2024
f9e10b8
Update from develop-postgres 20241130 (#2531)
palisadoes Dec 1, 2024
9c92449
Added vitest to avatar
NishantSinghhhhh Dec 4, 2024
16efbdf
Merge branch 'develop-postgres' into develop
NishantSinghhhhh Dec 5, 2024
0b481de
Correcting linting issue
NishantSinghhhhh Dec 5, 2024
ba9a476
Merge branch 'develop' of github.com:NishantSinghhhhh/talawa-admin in…
NishantSinghhhhh Dec 5, 2024
7741661
Correcting linting issue
NishantSinghhhhh Dec 5, 2024
008db5b
Added typedoc in avatar.spec.tsx
NishantSinghhhhh Dec 5, 2024
26f9012
removing codecov error
NishantSinghhhhh Dec 5, 2024
f7b77a1
removing codecov error
NishantSinghhhhh Dec 5, 2024
750daee
Removing codecov errros
NishantSinghhhhh Dec 5, 2024
805521e
Removing codecov errros
NishantSinghhhhh Dec 5, 2024
84eb82f
removing linting errors
NishantSinghhhhh Dec 5, 2024
b1e8d07
solving codecov errors
NishantSinghhhhh Dec 6, 2024
4fbec1e
changed the extra edited file
NishantSinghhhhh Dec 6, 2024
82d94dc
typedoc added
NishantSinghhhhh Dec 6, 2024
c2465f6
Update pull_request_template.md
NishantSinghhhhh Dec 6, 2024
996b31b
added more tests in eventCalender
NishantSinghhhhh Dec 6, 2024
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
91 changes: 91 additions & 0 deletions src/components/Avatar/Avatar.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React from 'react';
import { render } from '@testing-library/react';
import '@testing-library/jest-dom';
import { describe, test, expect, vi } from 'vitest';
import Avatar from './Avatar';

/**
* Unit tests for the `Avatar` component.
*
* The tests ensure the `Avatar` component renders correctly with various props.
* Mocked dependencies are used to isolate the component and verify its behavior.
*/

vi.mock('state/store', () => ({
store: {
getState: vi.fn(() => ({
auth: {
user: null,
loading: false,
},
})),
subscribe: vi.fn(),
dispatch: vi.fn(),
},
}));

vi.mock('utils/i18nForTest', () => ({
__esModule: true,
default: vi.fn(() => ({
t: (key: string) => key,
})),
}));

describe('Avatar component', () => {
/**
* Test: Verifies the `Avatar` component renders correctly with the `name` and `alt` attributes.
*
* Steps:
* 1. Render the `Avatar` component with `name`, `alt`, and `size` props.
* 2. Check if the avatar image is present in the document.
* 3. Validate the `src` attribute is defined.
*/
test('renders with name and alt attribute', () => {
const testName = 'John Doe';
const testAlt = 'Test Alt Text';
const testSize = 64;

const { getByAltText } = render(
<Avatar name={testName} alt={testAlt} size={testSize} />,
);

const avatarElement = getByAltText(testAlt);

expect(avatarElement).toBeInTheDocument();
expect(avatarElement.getAttribute('src')).toBeDefined();
});

/**
* Test: Verifies the `Avatar` component renders correctly with custom style and `data-testid`.
*
* Steps:
* 1. Render the `Avatar` component with `avatarStyle` and `dataTestId` props.
* 2. Check if the avatar image is present in the document.
* 3. Validate the `className` contains the custom style.
* 4. Validate the `data-testid` attribute matches the expected value.
*/

test('renders with custom style and data-testid', () => {
const testName = 'Jane Doe';
const testStyle = 'custom-avatar-style';
const testDataTestId = 'custom-avatar-test-id';

const { getByAltText } = render(
<Avatar
name={testName}
alt="Dummy Avatar"
avatarStyle={testStyle} // Pass custom style
dataTestId={testDataTestId} // Pass data-testid
/>,
);

const avatarElement = getByAltText('Dummy Avatar');

expect(avatarElement).toBeInTheDocument();
expect(avatarElement.getAttribute('src')).toBeDefined();

expect(avatarElement.className).toContain(testStyle);

expect(avatarElement.getAttribute('data-testid')).toBe(testDataTestId);
});
});
3 changes: 2 additions & 1 deletion src/components/CheckIn/tagTemplate.ts

Large diffs are not rendered by default.

250 changes: 248 additions & 2 deletions src/components/EventCalendar/EventCalendar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Calendar from './EventCalendar';
import Calendar, { Role } from './EventCalendar';
import { render, screen, fireEvent, act } from '@testing-library/react';
import { MockedProvider } from '@apollo/react-testing';
import { I18nextProvider } from 'react-i18next';
import React from 'react';
import { ViewType } from 'screens/OrganizationEvents/OrganizationEvents';

import {
DELETE_EVENT_MUTATION,
UPDATE_EVENT_MUTATION,
Expand Down Expand Up @@ -100,6 +99,253 @@ async function wait(ms = 200): Promise<void> {
});
}

describe('Role Enum', () => {
it('should have correct values', () => {
expect(Role.USER).toBe('USER');
expect(Role.SUPERADMIN).toBe('SUPERADMIN');
expect(Role.ADMIN).toBe('ADMIN');
});

it('should work in conditional logic', () => {
const getRoleDescription = (role: Role): string => {
switch (role) {
case Role.SUPERADMIN:
return 'Super Admin Access';
case Role.ADMIN:
return 'Admin Access';
default:
return 'User Access';
}
};

expect(getRoleDescription(Role.USER)).toBe('User Access');
expect(getRoleDescription(Role.ADMIN)).toBe('Admin Access');
expect(getRoleDescription(Role.SUPERADMIN)).toBe('Super Admin Access');
});
});
describe('Month and Year Navigation', () => {
let setCurrentMonth: jest.Mock;
let setCurrentYear: jest.Mock;

beforeEach(() => {
setCurrentMonth = jest.fn();
setCurrentYear = jest.fn();
});

it('should set the month to December of the previous year when the current month is January (0)', () => {
const currentMonth = 0;
const currentYear = 2024;

if (currentMonth === 0) {
setCurrentMonth(11);
setCurrentYear(currentYear - 1);
} else {
setCurrentMonth(currentMonth - 1);
}

expect(setCurrentMonth).toHaveBeenCalledWith(11);
expect(setCurrentYear).toHaveBeenCalledWith(2023);
});

it('should decrement the month when the current month is not January (0)', () => {
const currentMonth = 5; // June
const currentYear = 2024;

if (Number(currentMonth) === 0) {
setCurrentMonth(11);
setCurrentYear(currentYear - 1);
} else {
setCurrentMonth(currentMonth - 1);
}

expect(setCurrentMonth).toHaveBeenCalledWith(4); // May
expect(setCurrentYear).not.toHaveBeenCalled();
});

it('should set the month to January of the next year when the current month is December (11)', () => {
const currentMonth = 11; // December
const currentYear = 2024;

if (currentMonth === 11) {
setCurrentMonth(0);
setCurrentYear(currentYear + 1);
} else {
setCurrentMonth(currentMonth + 1);
}

expect(setCurrentMonth).toHaveBeenCalledWith(0); // January
expect(setCurrentYear).toHaveBeenCalledWith(2025);
});
});

describe('Date Navigation', () => {
let setCurrentDate: jest.Mock;
let setCurrentMonth: jest.Mock;
let setCurrentYear: jest.Mock;

beforeEach(() => {
setCurrentDate = jest.fn();
setCurrentMonth = jest.fn();
setCurrentYear = jest.fn();
});

it('should decrement the current date if it is greater than 1', () => {
const currentDate = 15; // Any date greater than 1
const currentMonth = 5; // Any month
const currentYear = 2024; // Any year

if (currentDate > 1) {
setCurrentDate(currentDate - 1);
} else if (currentMonth > 0) {
const lastDayOfPrevMonth = new Date(
currentYear,
currentMonth,
0,
).getDate();
setCurrentDate(lastDayOfPrevMonth);
setCurrentMonth(currentMonth - 1);
} else {
setCurrentDate(31);
setCurrentMonth(11);
setCurrentYear(currentYear - 1);
}

expect(setCurrentDate).toHaveBeenCalledWith(14);
expect(setCurrentMonth).not.toHaveBeenCalled();
expect(setCurrentYear).not.toHaveBeenCalled();
});

it('should set the date to the last day of the previous month and decrement the month if currentDate is 1 and currentMonth > 0', () => {
const currentDate = 1;
const currentMonth = 5; // June
const currentYear = 2024; // Any year

if (currentDate > 1) {
setCurrentDate(currentDate - 1);
} else if (currentMonth > 0) {
const lastDayOfPrevMonth = new Date(
currentYear,
currentMonth,
0,
).getDate();
setCurrentDate(lastDayOfPrevMonth);
setCurrentMonth(currentMonth - 1);
} else {
setCurrentDate(31);
setCurrentMonth(11);
setCurrentYear(currentYear - 1);
}

expect(setCurrentDate).toHaveBeenCalledWith(31); // Last day of May
expect(setCurrentMonth).toHaveBeenCalledWith(4); // May
expect(setCurrentYear).not.toHaveBeenCalled();
});

it('should set the date to 31, month to December, and decrement the year if currentDate is 1 and currentMonth is 0', () => {
const currentDate = 1;
const currentMonth = 0; // January
const currentYear = 2024;

if (currentDate > 1) {
setCurrentDate(currentDate - 1);
} else if (currentMonth > 0) {
const lastDayOfPrevMonth = new Date(
currentYear,
currentMonth,
0,
).getDate();
setCurrentDate(lastDayOfPrevMonth);
setCurrentMonth(currentMonth - 1);
} else {
setCurrentDate(31);
setCurrentMonth(11);
setCurrentYear(currentYear - 1);
}

expect(setCurrentDate).toHaveBeenCalledWith(31);
expect(setCurrentMonth).toHaveBeenCalledWith(11); // December
expect(setCurrentYear).toHaveBeenCalledWith(2023);
});

it('should correctly handle February and leap years when currentDate is 1 and currentMonth is 1 (February)', () => {
const currentDate = 1;
const currentMonth = 1; // February
const currentYear = 2024; // Leap year

if (currentDate > 1) {
setCurrentDate(currentDate - 1);
} else if (currentMonth > 0) {
const lastDayOfPrevMonth = new Date(
currentYear,
currentMonth,
0,
).getDate();
setCurrentDate(lastDayOfPrevMonth);
setCurrentMonth(currentMonth - 1);
} else {
setCurrentDate(31);
setCurrentMonth(11);
setCurrentYear(currentYear - 1);
}

expect(setCurrentDate).toHaveBeenCalledWith(31); // January 31st
expect(setCurrentMonth).toHaveBeenCalledWith(0); // January
expect(setCurrentYear).not.toHaveBeenCalled();
});

it('should correctly handle the transition from March 1st to February 28th (non-leap year)', () => {
const currentDate = 1;
const currentMonth = 2; // March
const currentYear = 2023; // Non-leap year

if (currentDate > 1) {
setCurrentDate(currentDate - 1);
} else if (currentMonth > 0) {
const lastDayOfPrevMonth = new Date(
currentYear,
currentMonth,
0,
).getDate();
setCurrentDate(lastDayOfPrevMonth);
setCurrentMonth(currentMonth - 1);
} else {
setCurrentDate(31);
setCurrentMonth(11);
setCurrentYear(currentYear - 1);
}

expect(setCurrentDate).toHaveBeenCalledWith(28); // February 28th
expect(setCurrentMonth).toHaveBeenCalledWith(1); // February
expect(setCurrentYear).not.toHaveBeenCalled();
});

it('should handle the transition from April 1st to March 31st', () => {
const currentDate = 1;
const currentMonth = 3; // April
const currentYear = 2024;

if (currentDate > 1) {
setCurrentDate(currentDate - 1);
} else if (currentMonth > 0) {
const lastDayOfPrevMonth = new Date(
currentYear,
currentMonth,
0,
).getDate();
setCurrentDate(lastDayOfPrevMonth);
setCurrentMonth(currentMonth - 1);
} else {
setCurrentDate(31);
setCurrentMonth(11);
setCurrentYear(currentYear - 1);
}

expect(setCurrentDate).toHaveBeenCalledWith(31); // March 31st
expect(setCurrentMonth).toHaveBeenCalledWith(2); // March
expect(setCurrentYear).not.toHaveBeenCalled();
});
});

describe('Calendar', () => {
it('renders weekdays', () => {
render(<Calendar eventData={eventData} viewType={ViewType.MONTH} />);
Expand Down
4 changes: 1 addition & 3 deletions src/components/EventCalendar/EventCalendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ interface InterfaceCalendarProps {
viewType?: ViewType;
}

enum Role {
export enum Role {
USER = 'USER',
SUPERADMIN = 'SUPERADMIN',
ADMIN = 'ADMIN',
Expand Down Expand Up @@ -130,7 +130,6 @@ const Calendar: React.FC<InterfaceCalendarProps> = ({
* Moves the calendar view to the previous month.
*/
const handlePrevMonth = (): void => {
/*istanbul ignore next*/
if (currentMonth === 0) {
setCurrentMonth(11);
setCurrentYear(currentYear - 1);
Expand All @@ -143,7 +142,6 @@ const Calendar: React.FC<InterfaceCalendarProps> = ({
* Moves the calendar view to the next month.
*/
const handleNextMonth = (): void => {
/*istanbul ignore next*/
if (currentMonth === 11) {
setCurrentMonth(0);
setCurrentYear(currentYear + 1);
Expand Down
Loading