Skip to content

Commit

Permalink
Chore(deps): Replace next-share with react-share and add Storyboo…
Browse files Browse the repository at this point in the history
…k stories for Social component (#1390)

* chore(deps): remove `next-share` dependency and add `react-share` dependency

* refactor: replace `next-share` with `react-share` for social sharing buttons

* feat: add Storybook stories for Social component with various configurations

* test: update Social component tests to use FontAwesome icons instead of test IDs
  • Loading branch information
drikusroor authored Nov 20, 2024
1 parent 39575aa commit 2c9bc6e
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 51 deletions.
58 changes: 33 additions & 25 deletions frontend/.pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"classnames": "^2.2.6",
"email-validator": "^2.0.4",
"file-saver": "^2.0.5",
"next-share": "^0.27.0",
"qs": "^6.10.3",
"react": "18.3.1",
"react-dom": "18.3.1",
Expand All @@ -19,6 +18,7 @@
"react-router": "^6.25.1",
"react-router-dom": "^6.25.1",
"react-select": "^5.4.0",
"react-share": "^5.1.1",
"react-transition-group": "^4.4.5",
"sass": "^1.69.5",
"typescript": "^5.3.3",
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/components/Social/Social.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ describe('Social Component', () => {

it('renders all social media buttons when all apps are included', () => {
render(<Social social={mockSocial} />);
expect(screen.getByTestId('facebook-share')).toBeDefined();
expect(screen.getByTestId('whatsapp-share')).toBeDefined();
expect(screen.getByTestId('twitter-share')).toBeDefined();
expect(screen.getByTestId('weibo-share')).toBeDefined();
expect(document.querySelector('.fa-facebook-f')).toBeDefined();
expect(document.querySelector('.fa-whatsapp')).toBeDefined();
expect(document.querySelector('.fa-x-twitter')).toBeDefined();
expect(document.querySelector('.fa-weibo')).toBeDefined();
});

it('renders only specified social media buttons', () => {
const limitedSocial: ISocial = { ...mockSocial, channels: ['facebook', 'twitter'] };
render(<Social social={limitedSocial} />);
expect(screen.getByTestId('facebook-share')).toBeDefined();
expect(screen.getByTestId('twitter-share')).toBeDefined();
expect(screen.queryByTestId('whatsapp-share')).toBeNull();
expect(screen.queryByTestId('weibo-share')).toBeNull();
expect(document.querySelector('.fa-facebook-f')).toBeDefined();
expect(document.querySelector('.fa-x-twitter')).toBeDefined();
expect(document.querySelector('.fa-whatsapp')).toBeNull();
expect(document.querySelector('.fa-weibo')).toBeNull();
});

it('renders share button when navigator.share is available', () => {
Expand Down
6 changes: 1 addition & 5 deletions frontend/src/components/Social/Social.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRef } from "react";
import {
FacebookShareButton, TwitterShareButton, WeiboShareButton, WhatsappShareButton
} from 'next-share'
} from 'react-share'
import ISocial from "@/types/Social";

export interface SocialProps {
Expand Down Expand Up @@ -42,7 +42,6 @@ const Social = ({ social }: SocialProps) => {
url={social.url}
title={social.content}
hashtag={social.tags[0]}
blankTarget={true}
>
<i className="fa-brands fa-facebook-f fa-2x"></i>
</FacebookShareButton>
Expand All @@ -51,7 +50,6 @@ const Social = ({ social }: SocialProps) => {
<WhatsappShareButton
url={social.url}
title={social.content}
blankTarget={true}
>
<i className="fa-brands fa-whatsapp fa-2x"></i>
</WhatsappShareButton>
Expand All @@ -61,7 +59,6 @@ const Social = ({ social }: SocialProps) => {
url={social.url}
title={social.content}
hashtags={social.tags}
blankTarget={true}
>
<i className="fa-brands fa-x-twitter fa-2x"></i>
</TwitterShareButton>
Expand All @@ -70,7 +67,6 @@ const Social = ({ social }: SocialProps) => {
<WeiboShareButton
url={social.url}
title={social.content}
blankTarget={true}
>
<i className="fa-brands fa-weibo fa-2x"></i>
</WeiboShareButton>
Expand Down
98 changes: 98 additions & 0 deletions frontend/src/stories/Social.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import type { Meta, StoryObj } from '@storybook/react';
import Social from '@/components/Social/Social';
import ISocial from '@/types/Social';

const meta: Meta<typeof Social> = {
title: 'Components/Social',
component: Social,
parameters: {
layout: 'centered',
},
decorators: [
(Story) => (
<div style={{ width: '100%', padding: '1rem', backgroundColor: '#f5f5f5' }}>
<Story />
</div>
),
],
};

export default meta;
type Story = StoryObj<typeof Social>;

const defaultSocialProps: ISocial = {
channels: ['facebook', 'twitter', 'whatsapp', 'weibo', 'share', 'clipboard'],
url: 'https://example.com/share',
content: 'Check out this awesome content!',
tags: ['storybook', 'testing', 'react'],
};

export const AllChannels: Story = {
args: {
social: defaultSocialProps,
},
};

export const SocialMediaOnly: Story = {
args: {
social: {
...defaultSocialProps,
channels: ['facebook', 'twitter', 'whatsapp', 'weibo'],
},
},
};

export const MinimalChannels: Story = {
args: {
social: {
...defaultSocialProps,
channels: ['facebook', 'twitter'],
},
},
};

export const SystemShareOnly: Story = {
args: {
social: {
...defaultSocialProps,
channels: ['share', 'clipboard'],
},
},
};

export const CustomContent: Story = {
args: {
social: {
...defaultSocialProps,
content: '🎉 Amazing news! Join us for this special event!',
tags: ['event', 'special', 'celebration'],
},
},
};

export const LongUrl: Story = {
args: {
social: {
...defaultSocialProps,
url: 'https://example.com/very/long/url/with/multiple/parameters?param1=value1&param2=value2&param3=value3',
},
},
};

export const NoTags: Story = {
args: {
social: {
...defaultSocialProps,
tags: [],
},
},
};

export const SingleChannel: Story = {
args: {
social: {
...defaultSocialProps,
channels: ['facebook'],
},
},
};
32 changes: 20 additions & 12 deletions frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4363,7 +4363,6 @@ __metadata:
file-saver: "npm:^2.0.5"
happy-dom: "npm:^15.10.2"
history: "npm:^5.3.0"
next-share: "npm:^0.27.0"
prop-types: "npm:15.8.1"
qs: "npm:^6.10.3"
react: "npm:18.3.1"
Expand All @@ -4373,6 +4372,7 @@ __metadata:
react-router: "npm:^6.25.1"
react-router-dom: "npm:^6.25.1"
react-select: "npm:^5.4.0"
react-share: "npm:^5.1.1"
react-transition-group: "npm:^4.4.5"
sass: "npm:^1.69.5"
storybook: "npm:^8.4.2"
Expand Down Expand Up @@ -4978,6 +4978,13 @@ __metadata:
languageName: node
linkType: hard

"classnames@npm:^2.3.2":
version: 2.5.1
resolution: "classnames@npm:2.5.1"
checksum: 10c0/afff4f77e62cea2d79c39962980bf316bacb0d7c49e13a21adaadb9221e1c6b9d3cdb829d8bb1b23c406f4e740507f37e1dcf506f7e3b7113d17c5bab787aa69
languageName: node
linkType: hard

"clean-stack@npm:^2.0.0":
version: 2.2.0
resolution: "clean-stack@npm:2.2.0"
Expand Down Expand Up @@ -7631,17 +7638,6 @@ __metadata:
languageName: node
linkType: hard

"next-share@npm:^0.27.0":
version: 0.27.0
resolution: "next-share@npm:0.27.0"
dependencies:
jsonp: "npm:^0.2.1"
peerDependencies:
react: ">=17.0.2"
checksum: 10c0/0b1e6a408f1cacf42d4f9d63c31ad52939f8838a6239e46031ea64565483f01528dde730fcccb1a607ba1061a7ff169bdfb33df5ff0d33eec4dba9cd402904e7
languageName: node
linkType: hard

"node-gyp@npm:latest":
version: 10.0.1
resolution: "node-gyp@npm:10.0.1"
Expand Down Expand Up @@ -8224,6 +8220,18 @@ __metadata:
languageName: node
linkType: hard

"react-share@npm:^5.1.1":
version: 5.1.1
resolution: "react-share@npm:5.1.1"
dependencies:
classnames: "npm:^2.3.2"
jsonp: "npm:^0.2.1"
peerDependencies:
react: ^17 || ^18
checksum: 10c0/f2959d324d456197f29779d95333110fa1f2708ea5859bf0e02efd7ea09346bb12e2577217279162813a75542e11cfcb0159bf5c9718187018dc968efccafdb8
languageName: node
linkType: hard

"react-side-effect@npm:^2.1.0":
version: 2.1.2
resolution: "react-side-effect@npm:2.1.2"
Expand Down

0 comments on commit 2c9bc6e

Please sign in to comment.