diff --git a/packages/shared/src/components/ShareBar.spec.tsx b/packages/shared/src/components/ShareBar.spec.tsx index 010c6b341c..275c4e34b4 100644 --- a/packages/shared/src/components/ShareBar.spec.tsx +++ b/packages/shared/src/components/ShareBar.spec.tsx @@ -5,11 +5,11 @@ import React from 'react'; import nock from 'nock'; import { useRouter } from 'next/router'; import ShareBar from './ShareBar'; -import Post from '../../__tests__/fixture/post'; +import Post, { sharePost } from '../../__tests__/fixture/post'; import { AuthContextProvider } from '../contexts/AuthContext'; import loggedUser from '../../__tests__/fixture/loggedUser'; import { generateTestSquad } from '../../__tests__/fixture/squads'; -import { getFacebookShareLink } from '../lib/share'; +import { getFacebookShareLink, getTwitterShareLink } from '../lib/share'; import { LazyModalElement } from './modals/LazyModalElement'; import { NotificationsContextProvider } from '../contexts/NotificationsContext'; @@ -50,6 +50,7 @@ const manySquads = Array.from({ length: 5 }, (_, index) => const renderComponent = ( loggedIn = true, customSquads: typeof squads = squads, + post = defaultPost, ): RenderResult => { const client = new QueryClient(); @@ -66,7 +67,7 @@ const renderComponent = ( > - + , @@ -137,6 +138,20 @@ describe('ShareBar Test Suite:', () => { }); }); + it('should fall back to shared post title on Twitter share when shared post has no title', async () => { + const post = { ...sharePost, title: undefined }; + renderComponent(true, squads, post); + + fireEvent.click(await screen.findByTestId('social-share-X')); + + await waitFor(() => { + expect(mockWindowOpen).toHaveBeenCalledWith( + getTwitterShareLink(post.commentsPermalink, post.sharedPost!.title), + '_blank', + ); + }); + }); + it('should render the copy link button and copy link to clipboard', async () => { renderComponent(); const btn = await screen.findByTestId('social-share-Copy link'); diff --git a/packages/shared/src/components/ShareBar.tsx b/packages/shared/src/components/ShareBar.tsx index ae4d342ae6..6b9461f11c 100644 --- a/packages/shared/src/components/ShareBar.tsx +++ b/packages/shared/src/components/ShareBar.tsx @@ -65,7 +65,7 @@ export default function ShareBar({ post }: ShareBarProps): ReactElement { const shareLink = getShareLink({ provider, link: shortLink, - text: post?.title, + text: post?.title || post?.sharedPost?.title, }); window.open(shareLink, '_blank'); };