Skip to content

Commit 0630cff

Browse files
Refactor: Adding vitest to Avatar component (#2622)
1 parent afa614a commit 0630cff

File tree

2 files changed

+92
-50
lines changed

2 files changed

+92
-50
lines changed

src/components/Avatar/Avatar.spec.tsx

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import { describe, test, expect, vi } from 'vitest';
5+
import Avatar from './Avatar';
6+
7+
/**
8+
* Unit tests for the `Avatar` component.
9+
*
10+
* The tests ensure the `Avatar` component renders correctly with various props.
11+
* Mocked dependencies are used to isolate the component and verify its behavior.
12+
*
13+
*/
14+
15+
vi.mock('state/store', () => ({
16+
store: {
17+
getState: vi.fn(() => ({
18+
auth: {
19+
user: null,
20+
loading: false,
21+
},
22+
})),
23+
subscribe: vi.fn(),
24+
dispatch: vi.fn(),
25+
},
26+
}));
27+
28+
vi.mock('utils/i18nForTest', () => ({
29+
__esModule: true,
30+
default: vi.fn(() => ({
31+
t: (key: string) => key,
32+
})),
33+
}));
34+
35+
describe('Avatar component', () => {
36+
/**
37+
* Test: Verifies the `Avatar` component renders correctly with the `name` and `alt` attributes.
38+
*
39+
* Steps:
40+
* 1. Render the `Avatar` component with `name`, `alt`, and `size` props.
41+
* 2. Check if the avatar image is present in the document.
42+
* 3. Validate the `src` attribute is defined.
43+
*/
44+
test('renders with name and alt attribute', () => {
45+
const testName = 'John Doe';
46+
const testAlt = 'Test Alt Text';
47+
const testSize = 64;
48+
49+
const { getByAltText } = render(
50+
<Avatar name={testName} alt={testAlt} size={testSize} />,
51+
);
52+
53+
const avatarElement = getByAltText(testAlt);
54+
55+
expect(avatarElement).toBeInTheDocument();
56+
expect(avatarElement.getAttribute('src')).toBeDefined();
57+
});
58+
59+
/**
60+
* Test: Verifies the `Avatar` component renders correctly with custom style and `data-testid`.
61+
*
62+
* Steps:
63+
* 1. Render the `Avatar` component with `avatarStyle` and `dataTestId` props.
64+
* 2. Check if the avatar image is present in the document.
65+
* 3. Validate the `className` contains the custom style.
66+
* 4. Validate the `data-testid` attribute matches the expected value.
67+
*/
68+
69+
test('renders with custom style and data-testid', () => {
70+
const testName = 'Jane Doe';
71+
const testStyle = 'custom-avatar-style';
72+
const testDataTestId = 'custom-avatar-test-id';
73+
74+
const { getByAltText } = render(
75+
<Avatar
76+
name={testName}
77+
alt="Dummy Avatar"
78+
avatarStyle={testStyle}
79+
dataTestId={testDataTestId}
80+
/>,
81+
);
82+
83+
const avatarElement = getByAltText('Dummy Avatar');
84+
85+
expect(avatarElement).toBeInTheDocument();
86+
expect(avatarElement.getAttribute('src')).toBeDefined();
87+
88+
expect(avatarElement.className).toContain(testStyle);
89+
90+
expect(avatarElement.getAttribute('data-testid')).toBe(testDataTestId);
91+
});
92+
});

src/components/Avatar/Avatar.test.tsx

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)