Skip to content

Commit 4a3a629

Browse files
test: add unit tests for avatar
1 parent 8c9c3a0 commit 4a3a629

2 files changed

Lines changed: 90 additions & 1 deletion

File tree

app/pages/playground/avatars.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Avatar from '@/components/ui/Avatar.vue';
1111
<Avatar :img="'https://placehold.co/64x64'" size="md" />
1212
</div>
1313
<div class="p-4">
14-
<Avatar size="lg" />
14+
<Avatar :img="'https://placehold.co/96x96'" size="lg" />
1515
</div>
1616
<div class="p-4">
1717
<Avatar :img="'https://placehold.co/368x368'" size="xl" variant="default" />
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { mountSuspended } from '@nuxt/test-utils/runtime';
3+
import Avatar from '@/components/ui/Avatar.vue';
4+
5+
describe('Avatar Component', () => {
6+
it('renders AvatarImg with the provided img src', async () => {
7+
const wrapper = await mountSuspended(Avatar, {
8+
props: {
9+
img: 'https://placehold.co/64x64',
10+
},
11+
});
12+
13+
const img = wrapper.find('img');
14+
expect(img.exists()).toBe(true);
15+
expect(img.attributes('src')).toContain('https://placehold.co/64x64');
16+
expect(img.attributes('alt')).toBe('User Avatar');
17+
});
18+
19+
it('renders fallback if image fails or not found', async () => {
20+
const wrapper = await mountSuspended(Avatar, {
21+
props: { img: '' }, // missing or broken image
22+
});
23+
const fallback = wrapper.findComponent({ name: 'AvatarFallback' });
24+
expect(fallback.exists()).toBe(true);
25+
expect(wrapper.text()).toContain('?');
26+
});
27+
28+
it('applies correct classes for default props (variant: primary, size: md)', async () => {
29+
const wrapper = await mountSuspended(Avatar);
30+
const root = wrapper.findComponent({ name: 'AvatarRoot' });
31+
const classes = root.classes();
32+
33+
expect(classes).toContain('cursor-pointer');
34+
expect(classes).toContain('hover:brightness-96');
35+
expect(classes).toContain('size-16');
36+
});
37+
38+
it('applies correct classes for variant: default', async () => {
39+
const wrapper = await mountSuspended(Avatar, {
40+
props: { variant: 'default' },
41+
});
42+
const root = wrapper.findComponent({ name: 'AvatarRoot' });
43+
const classes = root.classes();
44+
45+
expect(classes).toContain('cursor-default');
46+
expect(classes).not.toContain('hover:brightness-96');
47+
});
48+
49+
it('applies correct classes for variant: primary', async () => {
50+
const wrapper = await mountSuspended(Avatar, {
51+
props: { variant: 'primary' },
52+
});
53+
const root = wrapper.findComponent({ name: 'AvatarRoot' });
54+
const classes = root.classes();
55+
56+
expect(classes).toContain('cursor-pointer');
57+
expect(classes).toContain('hover:brightness-96');
58+
});
59+
60+
it('applies correct classes for size: sm', async () => {
61+
const wrapper = await mountSuspended(Avatar, {
62+
props: { size: 'sm' },
63+
});
64+
const root = wrapper.findComponent({ name: 'AvatarRoot' });
65+
expect(root.classes()).toContain('size-10');
66+
});
67+
68+
it('applies correct classes for size: lg', async () => {
69+
const wrapper = await mountSuspended(Avatar, {
70+
props: { size: 'lg' },
71+
});
72+
const root = wrapper.findComponent({ name: 'AvatarRoot' });
73+
expect(root.classes()).toContain('size-24');
74+
});
75+
76+
it('applies correct classes for size: xl', async () => {
77+
const wrapper = await mountSuspended(Avatar, {
78+
props: { size: 'xl' },
79+
});
80+
const root = wrapper.findComponent({ name: 'AvatarRoot' });
81+
expect(root.classes()).toContain('size-92');
82+
});
83+
84+
it('renders default image if no img prop is provided', async () => {
85+
const wrapper = await mountSuspended(Avatar);
86+
const img = wrapper.find('img');
87+
expect(img.attributes('src')).toContain('/default_profile.png');
88+
});
89+
});

0 commit comments

Comments
 (0)