Skip to content

Commit a5202f0

Browse files
test(react-chat): add comprehensive tests for AgentCard component
Co-Authored-By: [email protected] <[email protected]>
1 parent 3b92711 commit a5202f0

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

packages/react-chat/src/components/AgentCard/AgentCard.story.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ const meta: Meta<typeof AgentCard> = {
66
title: 'Components/AgentCard',
77
component: AgentCard,
88
tags: ['autodocs'],
9+
argTypes: {
10+
avatarColor: {
11+
control: 'color',
12+
description: 'Background color of the avatar',
13+
},
14+
name: {
15+
control: 'text',
16+
description: 'Name of the agent',
17+
},
18+
timestamp: {
19+
control: 'text',
20+
description: 'Timestamp to display',
21+
},
22+
initials: {
23+
control: 'text',
24+
description: 'Initials to show in the avatar',
25+
},
26+
},
927
args: {
1028
name: 'John Smith',
1129
timestamp: '2 hours ago',
@@ -31,6 +49,21 @@ export const LongName: Story = {
3149
},
3250
};
3351

52+
export const Interactive: Story = {
53+
args: {
54+
name: 'Jane Doe',
55+
timestamp: '5 minutes ago',
56+
initials: 'JD',
57+
},
58+
parameters: {
59+
pseudo: {
60+
hover: true,
61+
},
62+
},
63+
};
64+
3465
/**
66+
* A button component that displays agent information with an avatar.
67+
*
3568
* @see {@link https://voiceflow.github.io/react-chat/?path=/story/components-agent-card--default}
3669
*/

packages/react-chat/src/components/AgentCard/__tests__/index.test.tsx

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import { render, screen } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
34
import { describe, expect, it, vi } from 'vitest';
45
import '@testing-library/jest-dom';
56

@@ -27,16 +28,47 @@ describe('AgentCard', () => {
2728
expect(avatar).toHaveStyle(`background-color: ${customColor}`);
2829
});
2930

30-
it('handles click events', () => {
31+
it('handles click events', async () => {
3132
const handleClick = vi.fn();
33+
const user = userEvent.setup();
3234
render(<AgentCard {...defaultProps} onClick={handleClick} />);
33-
screen.getByRole('button').click();
34-
expect(handleClick).toHaveBeenCalled();
35+
36+
const button = screen.getByRole('button');
37+
await user.click(button);
38+
expect(handleClick).toHaveBeenCalledTimes(1);
3539
});
3640

3741
it('applies custom className', () => {
3842
const customClass = 'custom-class';
3943
const { container } = render(<AgentCard {...defaultProps} className={customClass} />);
4044
expect(container.firstChild).toHaveClass(customClass);
4145
});
46+
47+
it('applies default avatar color when not specified', () => {
48+
const { container } = render(<AgentCard {...defaultProps} />);
49+
const avatar = container.querySelector(`.${ClassName.AVATAR}`);
50+
expect(avatar).toHaveStyle({ backgroundColor: '#4a9b57' });
51+
});
52+
53+
it('maintains correct button attributes', () => {
54+
render(<AgentCard {...defaultProps} />);
55+
const button = screen.getByRole('button');
56+
expect(button).toHaveAttribute('type', 'button');
57+
});
58+
59+
it('renders with correct text styles', () => {
60+
render(<AgentCard {...defaultProps} />);
61+
62+
const nameElement = screen.getByText(defaultProps.name);
63+
expect(nameElement).toHaveStyle({
64+
fontSize: '14px',
65+
fontWeight: '500',
66+
});
67+
68+
const timestampElement = screen.getByText(defaultProps.timestamp);
69+
expect(timestampElement).toHaveStyle({
70+
fontSize: '12px',
71+
color: 'rgb(115, 115, 118)', // $darkGrey
72+
});
73+
});
4274
});

packages/react-chat/src/components/AgentCard/index.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const AgentCard: React.FC<AgentCardProps> = ({
3434
className,
3535
...props
3636
}) => (
37-
<AgentButton className={className} {...props}>
37+
<AgentButton type="button" className={className} {...props}>
3838
<Avatar
3939
size="small"
4040
avatar=""
@@ -58,6 +58,19 @@ const AgentCard: React.FC<AgentCardProps> = ({
5858
/**
5959
* A button component that displays agent information with an avatar.
6060
*/
61+
/**
62+
* A button component that displays agent information with an avatar.
63+
*
64+
* @example
65+
* ```tsx
66+
* <AgentCard
67+
* name="John Smith"
68+
* timestamp="2 hours ago"
69+
* initials="JS"
70+
* avatarColor="#4a9b57"
71+
* />
72+
* ```
73+
*/
6174
export default Object.assign(AgentCard, {
6275
Button: AgentButton,
6376
Container: ContentContainer,

packages/react-chat/src/components/AgentCard/styled.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export const AgentButton = styled(tag('button'), {
1616
border: 'none',
1717
cursor: 'pointer',
1818
width: '100%',
19+
boxShadow: `
20+
0px 1px 3px 1px #161A1E03,
21+
0px 4px 8px -6px #161A1E14,
22+
0px 1px 5px -4px #161A1E14,
23+
0px 0px 0px 1px #161A1E0A,
24+
0px 1px 0px 0px #161A1E05
25+
`,
1926

2027
'&:hover': {
2128
backgroundColor: '$lightGrey',
@@ -35,9 +42,13 @@ export const AgentName = styled(tag('span', 'name'), {
3542
fontSize: '14px',
3643
fontWeight: 500,
3744
color: '$black',
45+
margin: 0,
46+
padding: 0,
3847
});
3948

4049
export const AgentTimestamp = styled(tag('span', 'timestamp'), {
4150
fontSize: '12px',
4251
color: '$darkGrey',
52+
margin: 0,
53+
padding: 0,
4354
});

0 commit comments

Comments
 (0)