|
| 1 | +import { screen, waitFor } from '@testing-library/react'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { holdRequests, serveFapi } from '../../../__tests__/feature/fake-fapi'; |
| 5 | +import { fapiClient, fapiEmailAddress, fapiSession, fapiUser } from '../../../__tests__/feature/fapi'; |
| 6 | +import { renderWithClerk } from '../../../__tests__/feature/render'; |
| 7 | +import type { UserAvatarProps } from '../user-avatar'; |
| 8 | +import { UserAvatar } from '../user-avatar'; |
| 9 | + |
| 10 | +const PIXEL = |
| 11 | + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='; |
| 12 | + |
| 13 | +const email = fapiEmailAddress({ id: 'idn_alice', email_address: 'alice@example.com' }); |
| 14 | + |
| 15 | +const alice = fapiUser({ id: 'user_1', first_name: 'Alice', last_name: 'Smith', email_addresses: [email] }); |
| 16 | + |
| 17 | +function signedInAs(user = alice) { |
| 18 | + return { client: fapiClient([fapiSession({ id: 'sess_1', user })]) }; |
| 19 | +} |
| 20 | + |
| 21 | +function tree(props: UserAvatarProps = {}) { |
| 22 | + return ( |
| 23 | + <div data-testid='host'> |
| 24 | + <UserAvatar {...props} /> |
| 25 | + </div> |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +const host = () => screen.getByTestId('host'); |
| 30 | + |
| 31 | +describe('UserAvatar', () => { |
| 32 | + it('renders its fallback while Clerk is still loading', async () => { |
| 33 | + serveFapi(signedInAs()); |
| 34 | + const client = holdRequests('get', '/v1/client'); |
| 35 | + const rendering = renderWithClerk(tree({ fallback: <output data-testid='fallback'>Loading</output> })); |
| 36 | + |
| 37 | + await waitFor(() => expect(client.requests).toHaveLength(1)); |
| 38 | + expect(screen.getByTestId('fallback')).toBeInTheDocument(); |
| 39 | + |
| 40 | + client.release(); |
| 41 | + await rendering; |
| 42 | + expect(screen.queryByTestId('fallback')).toBeNull(); |
| 43 | + expect(screen.getByRole('img', { name: 'Alice Smith' })).toBeInTheDocument(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('renders nothing, not even its fallback, when nobody is signed in', async () => { |
| 47 | + serveFapi({ client: fapiClient() }); |
| 48 | + await renderWithClerk(tree({ fallback: <output>Loading</output> })); |
| 49 | + |
| 50 | + expect(host()).toBeEmptyDOMElement(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('holds the initials in the placeholder for a user with no profile image', async () => { |
| 54 | + serveFapi(signedInAs()); |
| 55 | + await renderWithClerk(tree()); |
| 56 | + |
| 57 | + const avatar = screen.getByRole('img', { name: 'Alice Smith' }); |
| 58 | + expect(avatar).toHaveTextContent('AS'); |
| 59 | + expect(avatar.querySelector('img')).toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('shows the profile image once it loads', async () => { |
| 63 | + serveFapi(signedInAs(fapiUser({ ...alice, image_url: PIXEL, has_image: true }))); |
| 64 | + await renderWithClerk(tree()); |
| 65 | + |
| 66 | + const avatar = screen.getByRole('img', { name: 'Alice Smith' }); |
| 67 | + await waitFor(() => expect(avatar.querySelector('img')).toHaveAttribute('src', PIXEL)); |
| 68 | + expect(avatar).not.toHaveTextContent('AS'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('is named by the identifier, and holds its first letter, for a user with no name', async () => { |
| 72 | + serveFapi(signedInAs(fapiUser({ id: 'user_2', email_addresses: [email] }))); |
| 73 | + await renderWithClerk(tree()); |
| 74 | + |
| 75 | + expect(screen.getByRole('img', { name: 'alice@example.com' })).toHaveTextContent('A'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('takes the shape and size it is given', async () => { |
| 79 | + serveFapi(signedInAs()); |
| 80 | + await renderWithClerk(tree({ shape: 'square', size: 'lg' })); |
| 81 | + |
| 82 | + const avatar = screen.getByRole('img', { name: 'Alice Smith' }); |
| 83 | + expect(avatar).toHaveClass('cl-user-avatar', 'cl-avatar'); |
| 84 | + expect(avatar).toHaveAttribute('data-shape', 'square'); |
| 85 | + expect(avatar).toHaveAttribute('data-size', 'lg'); |
| 86 | + }); |
| 87 | +}); |
0 commit comments