|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import { cleanup, render } from '@testing-library/react'; |
| 4 | + |
| 5 | +import { AvatarStack } from '../AvatarStack'; |
| 6 | +import { WithComponents } from '../../../context'; |
| 7 | + |
| 8 | +afterEach(cleanup); |
| 9 | + |
| 10 | +const member1 = { id: '1', imageUrl: 'img1.png', userName: 'Alice' }; |
| 11 | +const member2 = { id: '2', imageUrl: 'img2.png', userName: 'Bob' }; |
| 12 | +const member3 = { id: '3', imageUrl: 'img3.png', userName: 'Charlie' }; |
| 13 | +const member4 = { id: '4', imageUrl: 'img4.png', userName: 'Diana' }; |
| 14 | + |
| 15 | +describe('AvatarStack', () => { |
| 16 | + it('should render nothing when displayInfo is empty or not provided', () => { |
| 17 | + const r1 = render(<AvatarStack displayInfo={[]} size='md' />); |
| 18 | + expect(r1.queryByTestId('avatar-stack')).not.toBeInTheDocument(); |
| 19 | + |
| 20 | + const r2 = render(<AvatarStack size='md' />); |
| 21 | + expect(r2.queryByTestId('avatar-stack')).not.toBeInTheDocument(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should render avatars for each item in displayInfo', () => { |
| 25 | + const { getAllByTestId } = render( |
| 26 | + <AvatarStack displayInfo={[member1, member2]} size='md' />, |
| 27 | + ); |
| 28 | + expect(getAllByTestId('avatar')).toHaveLength(2); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should render the correct avatar images', () => { |
| 32 | + const { getAllByTestId } = render( |
| 33 | + <AvatarStack displayInfo={[member1, member2]} size='md' />, |
| 34 | + ); |
| 35 | + const images = getAllByTestId('avatar-img'); |
| 36 | + expect(images[0]).toHaveAttribute('src', 'img1.png'); |
| 37 | + expect(images[1]).toHaveAttribute('src', 'img2.png'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should apply the size class to the root element', () => { |
| 41 | + const { getByTestId } = render(<AvatarStack displayInfo={[member1]} size='sm' />); |
| 42 | + expect(getByTestId('avatar-stack')).toHaveClass('str-chat__avatar-stack--size-sm'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should not apply a size class when size is null', () => { |
| 46 | + const { getByTestId } = render(<AvatarStack displayInfo={[member1]} size={null} />); |
| 47 | + const root = getByTestId('avatar-stack'); |
| 48 | + expect(root).toBeInTheDocument(); |
| 49 | + expect(root.className).not.toContain('str-chat__avatar-stack--size-'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should render at most 3 avatars', () => { |
| 53 | + const { getAllByTestId } = render( |
| 54 | + <AvatarStack displayInfo={[member1, member2, member3, member4]} size='md' />, |
| 55 | + ); |
| 56 | + expect(getAllByTestId('avatar')).toHaveLength(3); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should render overflow count badge when more than 3 items', () => { |
| 60 | + const { getByTestId } = render( |
| 61 | + <AvatarStack displayInfo={[member1, member2, member3, member4]} size='md' />, |
| 62 | + ); |
| 63 | + const badge = getByTestId('avatar-stack-count-badge'); |
| 64 | + expect(badge).toBeInTheDocument(); |
| 65 | + expect(badge).toHaveTextContent('+1'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should not render overflow count badge when 3 or fewer items', () => { |
| 69 | + const { queryByTestId } = render( |
| 70 | + <AvatarStack displayInfo={[member1, member2, member3]} size='md' />, |
| 71 | + ); |
| 72 | + expect(queryByTestId('avatar-stack-count-badge')).not.toBeInTheDocument(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should render with a custom component wrapper', () => { |
| 76 | + const { getByTestId } = render( |
| 77 | + <AvatarStack component='section' displayInfo={[member1]} size='md' />, |
| 78 | + ); |
| 79 | + expect(getByTestId('avatar-stack').tagName).toBe('SECTION'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should default to div wrapper', () => { |
| 83 | + const { getByTestId } = render(<AvatarStack displayInfo={[member1]} size='md' />); |
| 84 | + expect(getByTestId('avatar-stack').tagName).toBe('DIV'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should use custom Avatar from ComponentContext', () => { |
| 88 | + const CustomAvatar = ({ userName }: { userName?: string }) => ( |
| 89 | + <div data-testid='custom-avatar'>{userName}</div> |
| 90 | + ); |
| 91 | + |
| 92 | + const { getAllByTestId, queryAllByTestId } = render( |
| 93 | + <WithComponents overrides={{ Avatar: CustomAvatar }}> |
| 94 | + <AvatarStack displayInfo={[member1, member2]} size='md' /> |
| 95 | + </WithComponents>, |
| 96 | + ); |
| 97 | + |
| 98 | + expect(getAllByTestId('custom-avatar')).toHaveLength(2); |
| 99 | + expect(getAllByTestId('custom-avatar')[0]).toHaveTextContent('Alice'); |
| 100 | + expect(getAllByTestId('custom-avatar')[1]).toHaveTextContent('Bob'); |
| 101 | + expect(queryAllByTestId('avatar')).toHaveLength(0); |
| 102 | + }); |
| 103 | +}); |
0 commit comments