Skip to content

Commit

Permalink
solving codecov errors
Browse files Browse the repository at this point in the history
Signed-off-by: NishantSinghhhhh <[email protected]>
  • Loading branch information
NishantSinghhhhh committed Dec 6, 2024
1 parent 84eb82f commit b1e8d07
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 176 deletions.
130 changes: 14 additions & 116 deletions src/components/Avatar/Avatar.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
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 { render } from '@testing-library/react';
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';
const link = new StaticMockLink([], true);

/**
* Avatar.spec.tsx
Expand All @@ -19,7 +11,6 @@ const link = new StaticMockLink([], true);
* different test cases like rendering with props, handling custom styles,
* verifying behavior for invalid/valid sizes, and handling undefined names.
*
*/

vi.mock('state/store', () => ({
Expand All @@ -41,134 +32,41 @@ vi.mock('utils/i18nForTest', () => ({
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', () => {
describe('Avatar component', () => {
test('renders 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>,
<Avatar name={testName} alt={testAlt} size={testSize} />,
);

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', () => {
test('renders 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>,
<Avatar
name={testName}
alt="Dummy Avatar"
// className={testStyle}
data-testid={testDataTestId}
/>,
);

const avatarElement = getByAltText(testAlt);
const avatarElement = getByAltText('Dummy Avatar');

expect(avatarElement).toBeInTheDocument();
expect(avatarElement.getAttribute('src')).toBeDefined();
expect(avatarElement.getAttribute('class')).toContain(testStyle);

Check failure on line 69 in src/components/Avatar/Avatar.spec.tsx

View workflow job for this annotation

GitHub Actions / Test Application

src/components/Avatar/Avatar.spec.tsx > Avatar component > renders with custom style and data-testid

AssertionError: expected '' to contain 'custom-avatar-style' - Expected + Received - custom-avatar-style ❯ src/components/Avatar/Avatar.spec.tsx:69:49
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 valid customUrl', () => {
const url = 'https://example.com/avatar.png';
const { getByAltText } = renderAvatar({ customUrl: url });

const avatar = getByAltText('Dummy Avatar');
expect(avatar).toHaveAttribute('src', url);
});

test('falls back on invalid customUrl', () => {
const { getByAltText } = renderAvatar({ customUrl: 'invalid-url' });

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

// 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: 2 additions & 15 deletions src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ 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 @@ -28,26 +27,16 @@ interface InterfaceAvatarProps {
* @returns JSX.Element - The rendered avatar image component.
*/
const Avatar = ({
name = 'Guest',
name,
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 @@ -64,8 +53,6 @@ const Avatar = ({
alt={alt}
className={avatarStyle ? avatarStyle : ''}
data-testid={dataTestId ? dataTestId : ''}
height={size || 128}
width={size || 128}
/>
</div>
);
Expand Down
59 changes: 14 additions & 45 deletions src/components/CheckIn/tagTemplate.ts

Large diffs are not rendered by default.

0 comments on commit b1e8d07

Please sign in to comment.