Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/shared/src/components/ShareBar.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ describe('ShareBar Test Suite:', () => {
);
});

it('should open the share to squad composer with the wrapped post when sharing a share without commentary', async () => {
const post = { ...sharePost, title: undefined };
renderComponent(true, squads, post);
const btn = await screen.findByTestId(`social-share-@${squads[0].handle}`);

btn.click();
await waitFor(() => {
expect(screen.getByRole('textbox', { name: 'Link URL' })).toHaveValue(
post.sharedPost!.permalink,
);
});
expect(screen.getByText(post.sharedPost!.title)).toBeInTheDocument();
});

it('should render the Facebook button and navigate to correct link', async () => {
renderComponent();
const btn = await screen.findByTestId(`social-share-Facebook`);
Expand Down
6 changes: 4 additions & 2 deletions packages/shared/src/components/ShareBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useContext, useMemo, useState } from 'react';
import classNames from 'classnames';
import { CopyIcon, FacebookIcon, TwitterIcon, WhatsappIcon } from './icons';
import type { Post } from '../graphql/posts';
import { getPostToShare } from '../graphql/posts';
import { useCopyPostLink } from '../hooks/useCopyPostLink';
import { getShareLink, ShareProvider } from '../lib/share';
import { useLogContext } from '../contexts/LogContext';
Expand Down Expand Up @@ -85,11 +86,12 @@ export default function ShareBar({

const onShareToSquad = (squad: Squad) => {
logEvent(postLogEvent(LogEvent.StartShareToSquad, post));
const postToShare = getPostToShare(post);
openModal({
type: LazyModal.SmartComposer,
props: {
initialUrl: post.permalink,
preview: post,
initialUrl: postToShare.permalink,
preview: postToShare,
initialSquadId: squad.id,
onPosted: () => logEvent(postLogEvent(LogEvent.ShareToSquad, post)),
},
Expand Down
16 changes: 14 additions & 2 deletions packages/shared/src/components/modals/ShareModal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React from 'react';
import nock from 'nock';
import { useRouter } from 'next/router';
import ShareModal from './ShareModal';
import Post from '../../../__tests__/fixture/post';
import Post, { sharePost } from '../../../__tests__/fixture/post';
import { getFacebookShareLink } from '../../lib/share';
import { Origin } from '../../lib/log';
import Comment from '../../../__tests__/fixture/comment';
Expand Down Expand Up @@ -38,6 +38,7 @@ const renderComponent = (
loggedIn = true,
hasSquads = true,
comment?: typeof defaultComment,
post = defaultPost,
): RenderResult => {
return render(
<TestBootProvider
Expand All @@ -49,7 +50,7 @@ const renderComponent = (
>
<ShareModal
origin={Origin.Feed}
post={defaultPost}
post={post}
comment={comment}
isOpen
onRequestClose={onRequestClose}
Expand Down Expand Up @@ -106,6 +107,17 @@ describe('ShareModal Test Suite:', () => {
);
});

it('should open the share to squad composer with the wrapped post when sharing a share without commentary', async () => {
const post = { ...sharePost, title: undefined, private: false };
renderComponent(true, true, undefined, post);
const btn = await screen.findByTestId(`social-share-@${squads[0].handle}`);
fireEvent.click(btn);
expect(
await screen.findByRole('textbox', { name: 'Link URL' }),
).toHaveValue(post.sharedPost!.permalink);
expect(screen.getByText(post.sharedPost!.title)).toBeInTheDocument();
});

it('should render the Facebook button and navigate to the correct link', async () => {
renderComponent(false);
const btn = await screen.findByTestId(`social-share-Facebook`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render, screen, waitFor } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import React from 'react';
import nock from 'nock';
import Post from '../../../../__tests__/fixture/post';
import Post, { sharePost } from '../../../../__tests__/fixture/post';
import loggedUser from '../../../../__tests__/fixture/loggedUser';
import { generateTestSquad } from '../../../../__tests__/fixture/squads';
import { settingsContext } from '../../../../__tests__/helpers/boot';
Expand Down Expand Up @@ -48,6 +48,7 @@ const squads = Array.from({ length: 6 }, (_, index) =>
const renderComponent = (
withSquads = true,
integrations?: UserIntegration[],
post = defaultPost,
): RenderResult => {
const client = new QueryClient();

Expand All @@ -72,7 +73,7 @@ const renderComponent = (
<SettingsContext.Provider value={settingsContext}>
<NotificationsContextProvider>
<LazyModalElement />
<DiscussionShareRow post={defaultPost} withSquads={withSquads} />
<DiscussionShareRow post={post} withSquads={withSquads} />
</NotificationsContextProvider>
</SettingsContext.Provider>
</AuthContextProvider>
Expand All @@ -96,6 +97,20 @@ describe('DiscussionShareRow', () => {
);
});

it('seeds the composer with the wrapped post when the post is a share without commentary', async () => {
const post = { ...sharePost, title: undefined };
renderComponent(true, undefined, post);

screen.getByRole('button', { name: 'Share to Web team 0' }).click();

await waitFor(() => {
expect(screen.getByRole('textbox', { name: 'Link URL' })).toHaveValue(
post.sharedPost!.permalink,
);
});
expect(screen.getByText(post.sharedPost!.title)).toBeInTheDocument();
});

it('caps the inline squads at four', () => {
renderComponent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ReactElement } from 'react';
import React from 'react';
import classNames from 'classnames';
import type { Post } from '../../../graphql/posts';
import { getPostToShare } from '../../../graphql/posts';
import type { Squad } from '../../../graphql/sources';
import { Button } from '../../buttons/Button';
import { ButtonSize, ButtonVariant } from '../../buttons/common';
Expand Down Expand Up @@ -71,11 +72,12 @@ export const DiscussionShareRow = ({

const onShareToSquad = (squad: Squad) => {
logEvent(postLogEvent(LogEvent.StartShareToSquad, post));
const postToShare = getPostToShare(post);
openModal({
type: LazyModal.SmartComposer,
props: {
initialUrl: post.permalink,
preview: post,
initialUrl: postToShare.permalink,
preview: postToShare,
initialSquadId: squad.id,
onPosted: () => logEvent(postLogEvent(LogEvent.ShareToSquad, post)),
},
Expand Down
6 changes: 4 additions & 2 deletions packages/shared/src/components/widgets/SocialShare.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Dispatch, ReactElement, SetStateAction } from 'react';
import React, { useContext } from 'react';
import { ShareProvider, addLogQueryParams } from '../../lib/share';
import type { Post } from '../../graphql/posts';
import { getPostToShare } from '../../graphql/posts';
import type { FeedItemPosition } from '../../lib/feed';
import { postLogEvent } from '../../lib/feed';
import { ActiveFeedContext } from '../../contexts';
Expand Down Expand Up @@ -64,6 +65,7 @@ export const SocialShare = ({
const { logOpts } = useContext(ActiveFeedContext);
const { openNativeSharePost } = useSharePost(Origin.Share);
const [squadToShare, setSquadToShare] = shareToSquadState;
const postToShare = getPostToShare(post);
const emailTitle = post?.title
? `Via Daily.Dev: ${post.title}`
: 'Via Daily.Dev';
Expand Down Expand Up @@ -118,8 +120,8 @@ export const SocialShare = ({
{squadToShare && ( // using lazy modal would result to dep-cycle since this whole component is used in the share modal
<SmartComposerModal
isOpen
initialUrl={post.permalink}
preview={post}
initialUrl={postToShare.permalink}
preview={postToShare}
initialSquadId={squadToShare.id}
onPosted={onSharedSuccessfully}
parentSelector={parentSelector}
Expand Down
28 changes: 28 additions & 0 deletions packages/shared/src/graphql/posts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
isSocialTwitterShareLike,
isVideoPost,
getReadPostButtonText,
getPostToShare,
} from './posts';
import { mockGraphQL } from '../../__tests__/helpers/graphql';
import type { Connection } from './common';
Expand Down Expand Up @@ -270,3 +271,30 @@ describe('social twitter helpers', () => {
expect(isShareLikePost(post)).toBeTruthy();
});
});

describe('getPostToShare', () => {
const sharedPost = { id: 'p2', title: 'Shared', image: 'image' } as Post;

it('should resolve a share without commentary to the post it wraps', () => {
const post = {
id,
type: PostType.Share,
title: undefined,
sharedPost,
} as Post;

expect(getPostToShare(post)).toBe(sharedPost);
});

it('should keep a share with commentary', () => {
const post = { id, type: PostType.Share, title: 'Read this', sharedPost };

expect(getPostToShare(post as Post)).toBe(post);
});

it('should keep any other post', () => {
const post = { id, type: PostType.Article, title: 'Article' } as Post;

expect(getPostToShare(post)).toBe(post);
});
});
13 changes: 13 additions & 0 deletions packages/shared/src/graphql/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ export const getReadArticleHref = (
): string | undefined =>
getPostReadTarget(post).target?.permalink ?? post.permalink;

/**
* A share without commentary has nothing of its own to reshare, so resharing
* it shares the post it wraps, the same way the API resolves it.
*/
export const getPostToShare = <
T extends Pick<Post, 'type' | 'title' | 'sharedPost'>,
>(
post: T,
): T | SharedPost =>
post.type === PostType.Share && !post.title && post.sharedPost
? post.sharedPost
: post;

export const getPostTitle = (
post:
| {
Expand Down
Loading