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 14 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
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ Fixes #<!--Add related issue number here.-->

**Have you read the [contributing guide](https://github.com/PalisadoesFoundation/talawa-admin/blob/master/CONTRIBUTING.md)?**

<!--Yes or No-->
<!--Yes or No-->
96 changes: 96 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"@babel/preset-env": "^7.26.0",
"@babel/preset-react": "^7.25.7",
"@babel/preset-typescript": "^7.26.0",
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.0.1",
"@testing-library/user-event": "^12.1.10",
Expand Down Expand Up @@ -155,6 +156,8 @@
"postcss-modules": "^6.0.0",
"sass": "^1.80.7",
"tsx": "^4.19.1",
"typedoc": "^0.27.3",
"typescript": "^5.6.3",
"vite-plugin-svgr": "^4.2.0",
"vitest": "^2.1.5",
"whatwg-fetch": "^3.6.20"
Expand Down
164 changes: 164 additions & 0 deletions src/components/Avatar/Avatar.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import { MockedProvider } from '@apollo/react-testing';
import { I18nextProvider } from 'react-i18next';
import '@testing-library/jest-dom';
import { describe, test, expect, vi } from 'vitest';
import { store } from 'state/store';
import Avatar from './Avatar';
import i18nForTest from 'utils/i18nForTest';
import { StaticMockLink } from 'utils/StaticMockLink';

// Setup mock for StaticMockLink
const link = new StaticMockLink([], true);

// Mock store and i18nForTest
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,
})),
}));

// Test suite for Avatar component
describe('Testing Avatar component', () => {
// Helper function for rendering Avatar component with props
const renderAvatar = (props = {}): ReturnType<typeof render> => {
return render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Avatar {...props} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);
};

// Test for rendering with name and alt attribute
test('should render with name and alt attribute', () => {
const testName = 'John Doe';
const testAlt = 'Test Alt Text';
const testSize = 64;

const { getByAltText } = render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Avatar name={testName} alt={testAlt} size={testSize} />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);

const avatarElement = getByAltText(testAlt);
expect(avatarElement).toBeInTheDocument();
expect(avatarElement.getAttribute('src')).toBeDefined();
});

// Test for custom style and data-testid
test('should render with custom style and data-testid', () => {
const testName = 'Jane Doe';
const testAlt = 'Dummy Avatar';
const testStyle = 'custom-avatar-style';
const testDataTestId = 'custom-avatar-test-id';

const { getByAltText } = render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Avatar
name={testName}
avatarStyle={testStyle}
dataTestId={testDataTestId}
/>
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);

const avatarElement = getByAltText(testAlt);
expect(avatarElement).toBeInTheDocument();
expect(avatarElement.getAttribute('src')).toBeDefined();
expect(avatarElement.getAttribute('class')).toContain(testStyle);
expect(avatarElement.getAttribute('data-testid')).toBe(testDataTestId);
});

// Error Handling for Undefined Name
test('handles undefined name gracefully', () => {
renderAvatar({ name: undefined });

const avatarElement = screen.getByAltText('Dummy Avatar');
expect(avatarElement).toBeInTheDocument();
expect(avatarElement.getAttribute('src')).toContain('data:image/svg+xml');
});

// Valid Sizes Test
const validSizes = [32, 64, 128];
validSizes.forEach((size) => {
test(`accepts valid size ${size}`, () => {
renderAvatar({ name: 'Test User', size });

const avatarElement = screen.getByAltText('Dummy Avatar');
expect(avatarElement).toHaveAttribute('width', size.toString());
expect(avatarElement).toHaveAttribute('height', size.toString());
});
});

// Invalid Sizes Test
const invalidSizes = [0, -1, 257, 'string'];
invalidSizes.forEach((size) => {
test(`falls back to default size when invalid size ${size} is provided`, () => {
renderAvatar({ name: 'Test User', size });

const avatarElement = screen.getByAltText('Dummy Avatar');
expect(avatarElement).toHaveAttribute('width');
expect(avatarElement).toHaveAttribute('height');
});
});

// Custom URL Test
test('uses custom URL when provided', () => {
const customUrl = 'https://example.com/custom-avatar.png';

renderAvatar({
name: 'John Doe',
customUrl,
});

const avatarElement = screen.getByAltText('Dummy Avatar');
expect(avatarElement.getAttribute('src')).toBe(customUrl);
});

// Fallback to generated avatar when custom URL is invalid
test('falls back to generated avatar when custom URL is invalid', () => {
renderAvatar({
name: 'John Doe',
customUrl: '',
});

const avatarElement = screen.getByAltText('Dummy Avatar');
expect(avatarElement.getAttribute('src')).toContain('data:image/svg+xml');
});
});
17 changes: 15 additions & 2 deletions src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { initials } from '@dicebear/collection';
import styles from 'components/Avatar/Avatar.module.css';

interface InterfaceAvatarProps {
name: string;
name?: string;
alt?: string;
size?: number;
containerStyle?: string;
avatarStyle?: string;
dataTestId?: string;
radius?: number;
customUrl?: string;
}

/**
Expand All @@ -27,16 +28,26 @@ interface InterfaceAvatarProps {
* @returns JSX.Element - The rendered avatar image component.
*/
const Avatar = ({
name,
name = 'Guest',
alt = 'Dummy Avatar',
size,
avatarStyle,
containerStyle,
dataTestId,
radius,
customUrl,
}: InterfaceAvatarProps): JSX.Element => {
// Memoize the avatar creation to avoid unnecessary recalculations
const avatar = useMemo(() => {
if (customUrl) {
try {
new URL(customUrl);
return customUrl;
} catch {
console.warn('Invalid custom URL provided to Avatar component');
}
}

return createAvatar(initials, {
size: size || 128,
seed: name,
Expand All @@ -53,6 +64,8 @@ const Avatar = ({
alt={alt}
className={avatarStyle ? avatarStyle : ''}
data-testid={dataTestId ? dataTestId : ''}
height={size || 128}
width={size || 128}
/>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
Expand Down
Loading