|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import { render, screen } from '@testing-library/react'; |
| 4 | +import { IntlProvider } from 'react-intl'; |
| 5 | +import { useSelector } from 'react-redux'; |
| 6 | +import { MemoryRouter } from 'react-router-dom'; |
| 7 | + |
| 8 | +import { getConfig } from '@edx/frontend-platform'; |
| 9 | + |
| 10 | +import { AvatarOutlineAndLabelColors, ThreadType } from '../../../data/constants'; |
| 11 | +import DiscussionContext from '../../common/context'; |
| 12 | +import { useAlertBannerVisible } from '../../data/hooks'; |
| 13 | +import PostHeader, { PostAvatar } from './PostHeader'; |
| 14 | + |
| 15 | +jest.mock('react-redux', () => ({ useSelector: jest.fn() })); |
| 16 | +jest.mock('@edx/frontend-platform', () => ({ getConfig: jest.fn() })); |
| 17 | +jest.mock('../../data/hooks', () => ({ useAlertBannerVisible: jest.fn() })); |
| 18 | + |
| 19 | +const defaultPostUsers = { |
| 20 | + 'test-user': { |
| 21 | + profile: { image: { hasImage: true, imageUrlSmall: 'http://avatar.test/img.png' } }, |
| 22 | + }, |
| 23 | +}; |
| 24 | + |
| 25 | +const ctxValue = { courseId: 'course-v1:edX+DemoX+Demo_Course', enableInContextSidebar: false }; |
| 26 | + |
| 27 | +function renderWithContext(ui) { |
| 28 | + return render( |
| 29 | + <IntlProvider locale="en"> |
| 30 | + <MemoryRouter> |
| 31 | + <DiscussionContext.Provider value={ctxValue}> |
| 32 | + {ui} |
| 33 | + </DiscussionContext.Provider> |
| 34 | + </MemoryRouter> |
| 35 | + </IntlProvider>, |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +describe('PostAvatar', () => { |
| 40 | + beforeEach(() => { |
| 41 | + jest.clearAllMocks(); |
| 42 | + useSelector.mockReturnValue({ imageUrlSmall: 'http://redux-avatar.png' }); |
| 43 | + getConfig.mockReturnValue({ ENABLE_PROFILE_IMAGE: 'true' }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('renders avatar with profile image when ENABLE_PROFILE_IMAGE=true', () => { |
| 47 | + renderWithContext( |
| 48 | + <PostAvatar |
| 49 | + author="test-user" |
| 50 | + postType={ThreadType.DISCUSSION} |
| 51 | + authorLabel="Staff" |
| 52 | + postUsers={defaultPostUsers} |
| 53 | + />, |
| 54 | + ); |
| 55 | + |
| 56 | + const avatarImg = screen.getByAltText('test-user'); |
| 57 | + expect(avatarImg).toHaveAttribute('src', 'http://avatar.test/img.png'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('falls back to redux avatar if no profile image', () => { |
| 61 | + renderWithContext( |
| 62 | + <PostAvatar |
| 63 | + author="test-user" |
| 64 | + postType={ThreadType.DISCUSSION} |
| 65 | + authorLabel="Staff" |
| 66 | + postUsers={{ 'test-user': { profile: { image: { hasImage: false, imageUrlSmall: null } } } }} |
| 67 | + />, |
| 68 | + ); |
| 69 | + |
| 70 | + const avatarImg = screen.getByAltText('test-user'); |
| 71 | + expect(avatarImg).toHaveAttribute('src', 'http://redux-avatar.png'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('applies Staff outline class if authorLabel provided', () => { |
| 75 | + renderWithContext( |
| 76 | + <PostAvatar |
| 77 | + author="test-user" |
| 78 | + postType={ThreadType.DISCUSSION} |
| 79 | + authorLabel="Staff" |
| 80 | + postUsers={defaultPostUsers} |
| 81 | + />, |
| 82 | + ); |
| 83 | + |
| 84 | + const avatar = screen.getByAltText('test-user'); |
| 85 | + expect(avatar.className).toMatch(`outline-${AvatarOutlineAndLabelColors.Staff}`); |
| 86 | + }); |
| 87 | + |
| 88 | + it('applies anonymous outline class if no authorLabel', () => { |
| 89 | + const { container } = renderWithContext( |
| 90 | + <PostAvatar |
| 91 | + author="test-user" |
| 92 | + postType={ThreadType.DISCUSSION} |
| 93 | + authorLabel={null} |
| 94 | + postUsers={defaultPostUsers} |
| 95 | + />, |
| 96 | + ); |
| 97 | + |
| 98 | + expect(container.querySelector('.outline-anonymous')).toBeInTheDocument(); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe('PostHeader', () => { |
| 103 | + beforeEach(() => { |
| 104 | + jest.clearAllMocks(); |
| 105 | + useSelector.mockReturnValue({ imageUrlSmall: 'http://redux-avatar.png' }); |
| 106 | + getConfig.mockReturnValue({ ENABLE_PROFILE_IMAGE: 'true' }); |
| 107 | + useAlertBannerVisible.mockReturnValue(false); |
| 108 | + }); |
| 109 | + |
| 110 | + const renderHeader = (props = {}) => renderWithContext( |
| 111 | + <PostHeader |
| 112 | + author="test-user" |
| 113 | + authorLabel="Staff" |
| 114 | + abuseFlagged={false} |
| 115 | + closed={false} |
| 116 | + createdAt="2025-09-23T10:00:00Z" |
| 117 | + lastEdit={null} |
| 118 | + postUsers={defaultPostUsers} |
| 119 | + title="Sample Post Title" |
| 120 | + postType={ThreadType.DISCUSSION} |
| 121 | + hasEndorsed={false} |
| 122 | + preview={false} |
| 123 | + {...props} |
| 124 | + />, |
| 125 | + ); |
| 126 | + |
| 127 | + it('renders post title and author', () => { |
| 128 | + renderHeader(); |
| 129 | + expect(screen.getByText('Sample Post Title')).toBeInTheDocument(); |
| 130 | + expect(screen.getByText('test-user')).toBeInTheDocument(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('adds answered badge for endorsed QUESTION preview', () => { |
| 134 | + renderHeader({ postType: ThreadType.QUESTION, hasEndorsed: true, preview: true }); |
| 135 | + expect(screen.getByText(/answered/i)).toBeInTheDocument(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('adds mt-10px class if alert banner is visible', () => { |
| 139 | + useAlertBannerVisible.mockReturnValue(true); |
| 140 | + const { container } = renderHeader({ preview: false }); |
| 141 | + expect(container.firstChild).toHaveClass('mt-10px'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('falls back to anonymous if no author provided', () => { |
| 145 | + renderHeader({ author: '' }); |
| 146 | + expect(screen.getByText(/anonymous/i)).toBeInTheDocument(); |
| 147 | + }); |
| 148 | +}); |
0 commit comments