File size: 3,906 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
 * @jest-environment jsdom
 */
// @ts-nocheck - TODO: Fix TypeScript issues

import { render, screen } from '@testing-library/react';
import React from 'react';
import { UserData } from 'calypso/lib/user/user';
import UserProfileHeader from '../index';

jest.mock( '@wordpress/icons', () => ( {
	external: 'mock-external-icon',
	Icon: ( { icon } ) => <span data-testid="icon">{ icon }</span>,
} ) );

jest.mock( 'calypso/blocks/reader-avatar', () => ( { author, iconSize } ) => (
	<div data-testid="reader-avatar" data-author-id={ author.ID } data-icon-size={ iconSize }>
		{ author.avatar_URL && <img src={ author.avatar_URL } alt="avatar" data-testid="avatar-img" /> }
	</div>
) );

jest.mock( 'calypso/components/section-nav', () => ( { children } ) => (
	<div data-testid="section-nav">{ children }</div>
) );

jest.mock( 'calypso/components/section-nav/tabs', () => ( { children } ) => (
	<div data-testid="nav-tabs">{ children }</div>
) );

jest.mock( 'calypso/components/section-nav/item', () => ( { children, path, selected } ) => (
	<a href={ path } data-testid="nav-item" data-selected={ selected ? 'true' : 'false' }>
		{ children }
	</a>
) );

describe( 'UserProfileHeader', () => {
	const defaultUser: UserData = {
		ID: 123,
		user_login: 'testuser',
		display_name: 'Test User',
		avatar_URL: 'https://example.com/avatar.jpg',
		profile_URL: 'https://wordpress.com/testuser',
		bio: undefined,
	};

	beforeEach( () => {
		jest.clearAllMocks();
	} );

	test( 'should render the avatar with correct user information', () => {
		render( <UserProfileHeader user={ defaultUser } /> );

		const avatars = screen.getAllByTestId( 'reader-avatar' );
		expect( avatars[ 0 ] ).toBeInTheDocument();
		expect( avatars[ 0 ] ).toHaveAttribute( 'data-author-id', defaultUser.ID.toString() );

		// Test that desktop and mobile versions are properly rendered
		const desktopAvatar = screen.getByTestId( 'desktop-avatar' );
		expect( desktopAvatar ).toBeInTheDocument();

		const mobileAvatar = screen.getByTestId( 'mobile-avatar' );
		expect( mobileAvatar ).toBeInTheDocument();
	} );

	test( 'should render the user display name', () => {
		render( <UserProfileHeader user={ defaultUser } /> );

		// Check if display name is rendered
		const displayNameEl = screen.getByText( defaultUser.display_name ?? '' );
		// @ts-expect-error -- jest-dom matchers are available globally
		expect( displayNameEl ).toBeInTheDocument();
	} );

	test( 'should render navigation tabs with Posts, Lists, and Recommended Blogs options', () => {
		render( <UserProfileHeader user={ defaultUser } /> );

		// Check if navigation section is rendered
		expect( screen.getByTestId( 'section-nav' ) ).toBeInTheDocument();
		expect( screen.getByTestId( 'nav-tabs' ) ).toBeInTheDocument();

		// Check for navigation items
		const navItems = screen.getAllByTestId( 'nav-item' );
		expect( navItems.length ).toBe( 3 ); // Posts, Lists, and Recommended Blogs

		// Check nav item content - should have Posts, Lists, and Recommended Blogs
		const navTexts = navItems.map( ( item ) => item.textContent );
		expect( navTexts ).toContain( 'Posts' );
		expect( navTexts ).toContain( 'Lists' );
		expect( navTexts ).toContain( 'Recommended Blogs' );
	} );

	test( 'should not render bio section when user has no bio', () => {
		render( <UserProfileHeader user={ defaultUser } /> );

		// Bio section should not be present
		const bioSection = document.querySelector( '.user-profile-header__bio' );
		expect( bioSection ).not.toBeInTheDocument();
	} );

	test( 'should render bio section when user has a bio', () => {
		const userWithBio = {
			...defaultUser,
			bio: 'This is my test biography that describes me as a test user.',
		};

		render( <UserProfileHeader user={ userWithBio } /> );

		// Bio section should be present
		const bioText = screen.getByText( userWithBio.bio );
		expect( bioText ).toBeInTheDocument();
	} );
} );