|
| 1 | +import { render, screen } from '@testing-library/svelte'; |
| 2 | +import { describe, expect, test } from 'vitest'; |
| 3 | + |
| 4 | +import { Avatar } from './index.js'; |
| 5 | + |
| 6 | +describe('Avatar', () => { |
| 7 | + test('renders img with the given src when src is provided', () => { |
| 8 | + const props = { src: 'data:image/png;base64,abc', name: 'Alice' }; |
| 9 | + |
| 10 | + render(Avatar, { props }); |
| 11 | + |
| 12 | + const img = screen.getByRole('img'); |
| 13 | + expect(img).toHaveAttribute('src', 'data:image/png;base64,abc'); |
| 14 | + }); |
| 15 | + |
| 16 | + test('renders initials chip with first letter of name when src is null', () => { |
| 17 | + const props = { src: null, name: 'Alice' }; |
| 18 | + |
| 19 | + render(Avatar, { props }); |
| 20 | + |
| 21 | + expect(screen.queryByRole('img')).not.toBeInTheDocument(); |
| 22 | + expect(screen.getByText('A')).toBeInTheDocument(); |
| 23 | + }); |
| 24 | + |
| 25 | + test('uppercases the initial', () => { |
| 26 | + const props = { src: null, name: 'alice' }; |
| 27 | + |
| 28 | + render(Avatar, { props }); |
| 29 | + |
| 30 | + expect(screen.getByText('A')).toBeInTheDocument(); |
| 31 | + }); |
| 32 | + |
| 33 | + test('trims leading whitespace before taking the initial', () => { |
| 34 | + const props = { src: null, name: ' alice' }; |
| 35 | + |
| 36 | + render(Avatar, { props }); |
| 37 | + |
| 38 | + expect(screen.getByText('A')).toBeInTheDocument(); |
| 39 | + }); |
| 40 | +}); |
0 commit comments