Skip to content

Commit f2db004

Browse files
committed
fix: add missing tests
1 parent a8fdf1d commit f2db004

22 files changed

Lines changed: 3459 additions & 1 deletion
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { render, screen } from '@testing-library/react';
2+
import { describe, expect, it } from 'vitest';
3+
import Avatar from '@/components/Avatar';
4+
import '@testing-library/jest-dom';
5+
6+
describe('Avatar', () => {
7+
it('renders avatar with initial', () => {
8+
render(<Avatar initial="J" size="sm" />);
9+
expect(screen.getByText('J')).toBeInTheDocument();
10+
});
11+
12+
it('applies size class sm', () => {
13+
const { container } = render(<Avatar initial="A" size="sm" />);
14+
expect(container.querySelector('.initial-avatar-sm')).toBeInTheDocument();
15+
});
16+
17+
it('applies size class md', () => {
18+
const { container } = render(<Avatar initial="B" size="md" />);
19+
expect(container.querySelector('.initial-avatar-md')).toBeInTheDocument();
20+
});
21+
22+
it('applies background color class for initial letter', () => {
23+
const { container } = render(<Avatar initial="C" size="sm" />);
24+
const avatar = container.querySelector('.initial-avatar-sm');
25+
expect(avatar).toHaveClass('initial-bg-c');
26+
});
27+
28+
it('applies lowercase color class for uppercase initial', () => {
29+
const { container } = render(<Avatar initial="Z" size="sm" />);
30+
const avatar = container.querySelector('.initial-avatar-sm');
31+
expect(avatar).toHaveClass('initial-bg-z');
32+
});
33+
34+
it('does not apply color class for empty initial', () => {
35+
const { container } = render(<Avatar initial="" size="sm" />);
36+
const avatar = container.querySelector('.initial-avatar-sm');
37+
expect(avatar).not.toHaveClass(/initial-bg-/);
38+
});
39+
40+
it('renders single character initial', () => {
41+
render(<Avatar initial="X" size="md" />);
42+
expect(screen.getByText('X')).toBeInTheDocument();
43+
});
44+
45+
it('renders first character only when multiple characters provided', () => {
46+
render(<Avatar initial="AB" size="sm" />);
47+
const element = screen.getByText('AB');
48+
expect(element).toHaveClass('initial-bg-a');
49+
});
50+
51+
it('combines size and color classes', () => {
52+
const { container } = render(<Avatar initial="M" size="md" />);
53+
const avatar = container.querySelector('div');
54+
expect(avatar?.className).toMatch(/initial-avatar-md/);
55+
expect(avatar?.className).toMatch(/initial-bg-m/);
56+
});
57+
58+
it('renders with different sizes', () => {
59+
const { rerender, container } = render(<Avatar initial="T" size="sm" />);
60+
expect(container.querySelector('.initial-avatar-sm')).toBeInTheDocument();
61+
62+
rerender(<Avatar initial="T" size="md" />);
63+
expect(container.querySelector('.initial-avatar-md')).toBeInTheDocument();
64+
});
65+
66+
it('handles numeric initial', () => {
67+
render(<Avatar initial="1" size="sm" />);
68+
expect(screen.getByText('1')).toBeInTheDocument();
69+
});
70+
71+
it('handles special character initial', () => {
72+
render(<Avatar initial="@" size="md" />);
73+
expect(screen.getByText('@')).toBeInTheDocument();
74+
});
75+
76+
it('applies both size class and bg class together', () => {
77+
const { container } = render(<Avatar initial="K" size="md" />);
78+
const element = container.querySelector('div');
79+
expect(element).toHaveClass('initial-avatar-md');
80+
expect(element).toHaveClass('initial-bg-k');
81+
});
82+
83+
it('renders with sm size and applies correct classes', () => {
84+
const { container } = render(<Avatar initial="S" size="sm" />);
85+
const div = container.querySelector('div');
86+
expect(div).toHaveClass('initial-avatar-sm', 'initial-bg-s');
87+
});
88+
});
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { render, screen } from '@testing-library/react';
2+
import { describe, expect, it, vi } from 'vitest';
3+
import CardContainer from '@/components/Card/CardContainer';
4+
import '@testing-library/jest-dom';
5+
6+
// Mock Carbon Grid component
7+
vi.mock('@carbon/react', () => ({
8+
Grid: ({ className, children }: any) => (
9+
<div data-testid="carbon-grid" className={className}>
10+
{children}
11+
</div>
12+
),
13+
}));
14+
15+
describe('CardContainer', () => {
16+
it('renders Grid component', () => {
17+
render(<CardContainer>Content</CardContainer>);
18+
expect(screen.getByTestId('carbon-grid')).toBeInTheDocument();
19+
});
20+
21+
it('renders children content', () => {
22+
render(
23+
<CardContainer>
24+
<span data-testid="child-content">Test Content</span>
25+
</CardContainer>
26+
);
27+
expect(screen.getByTestId('child-content')).toBeInTheDocument();
28+
});
29+
30+
it('applies card-container-grid class', () => {
31+
render(<CardContainer>Content</CardContainer>);
32+
expect(screen.getByTestId('carbon-grid')).toHaveClass('card-container-grid');
33+
});
34+
35+
it('applies default-grid class', () => {
36+
render(<CardContainer>Content</CardContainer>);
37+
expect(screen.getByTestId('carbon-grid')).toHaveClass('default-grid');
38+
});
39+
40+
it('applies custom className when provided', () => {
41+
render(
42+
<CardContainer className="custom-class">
43+
Content
44+
</CardContainer>
45+
);
46+
expect(screen.getByTestId('carbon-grid')).toHaveClass('custom-class');
47+
});
48+
49+
it('applies both default and custom classes', () => {
50+
render(
51+
<CardContainer className="my-custom-class">
52+
Content
53+
</CardContainer>
54+
);
55+
const grid = screen.getByTestId('carbon-grid');
56+
expect(grid).toHaveClass('card-container-grid');
57+
expect(grid).toHaveClass('default-grid');
58+
expect(grid).toHaveClass('my-custom-class');
59+
});
60+
61+
it('renders multiple children', () => {
62+
render(
63+
<CardContainer>
64+
<div data-testid="child-1">First</div>
65+
<div data-testid="child-2">Second</div>
66+
<div data-testid="child-3">Third</div>
67+
</CardContainer>
68+
);
69+
expect(screen.getByTestId('child-1')).toBeInTheDocument();
70+
expect(screen.getByTestId('child-2')).toBeInTheDocument();
71+
expect(screen.getByTestId('child-3')).toBeInTheDocument();
72+
});
73+
74+
it('renders with no children', () => {
75+
const { container } = render(<CardContainer />);
76+
expect(container.querySelector('[data-testid="carbon-grid"]')).toBeInTheDocument();
77+
});
78+
79+
it('renders with string children', () => {
80+
render(<CardContainer>Just text</CardContainer>);
81+
expect(screen.getByText('Just text')).toBeInTheDocument();
82+
});
83+
84+
it('renders with no className', () => {
85+
render(<CardContainer>Content</CardContainer>);
86+
const grid = screen.getByTestId('carbon-grid');
87+
expect(grid.className).toContain('card-container-grid');
88+
expect(grid.className).toContain('default-grid');
89+
});
90+
91+
it('combines multiple class names correctly', () => {
92+
render(
93+
<CardContainer className="first-class second-class">
94+
Content
95+
</CardContainer>
96+
);
97+
const grid = screen.getByTestId('carbon-grid');
98+
expect(grid).toHaveClass('card-container-grid');
99+
expect(grid).toHaveClass('default-grid');
100+
expect(grid).toHaveClass('first-class');
101+
expect(grid).toHaveClass('second-class');
102+
});
103+
104+
it('renders complex component children', () => {
105+
const ComplexChild = () => <div data-testid="complex">Complex Component</div>;
106+
render(
107+
<CardContainer>
108+
<ComplexChild />
109+
</CardContainer>
110+
);
111+
expect(screen.getByTestId('complex')).toBeInTheDocument();
112+
});
113+
114+
it('maintains children order', () => {
115+
const { container } = render(
116+
<CardContainer>
117+
<div data-testid="first">1</div>
118+
<div data-testid="second">2</div>
119+
<div data-testid="third">3</div>
120+
</CardContainer>
121+
);
122+
const children = container.querySelectorAll('[data-testid^="second"], [data-testid^="first"], [data-testid^="third"]');
123+
expect(children.length).toBe(3);
124+
});
125+
});

0 commit comments

Comments
 (0)