From 6bd38b812032857bb19af8978d48f6f8969af6cf Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Wed, 8 Jan 2020 12:16:13 +0100 Subject: [PATCH] fix: remove prevent default and use react context (#411) * fix: remove prevent default and use react context * chore: remove string check --- .../DetailSidebar/DetailSidebar.tsx | 2 +- .../DetailSidebarFundButton.test.tsx | 103 ++++++++++++++++++ .../DetailSidebar/DetailSidebarFundButton.tsx | 30 ++--- 3 files changed, 120 insertions(+), 15 deletions(-) create mode 100644 src/components/DetailSidebar/DetailSidebarFundButton.test.tsx diff --git a/src/components/DetailSidebar/DetailSidebar.tsx b/src/components/DetailSidebar/DetailSidebar.tsx index 23a0f9a..95a5ea5 100644 --- a/src/components/DetailSidebar/DetailSidebar.tsx +++ b/src/components/DetailSidebar/DetailSidebar.tsx @@ -38,7 +38,7 @@ const DetailSidebar: React.FC = () => { /> - {packageMeta?.latest?.funding && } + diff --git a/src/components/DetailSidebar/DetailSidebarFundButton.test.tsx b/src/components/DetailSidebar/DetailSidebarFundButton.test.tsx new file mode 100644 index 0000000..286b16f --- /dev/null +++ b/src/components/DetailSidebar/DetailSidebarFundButton.test.tsx @@ -0,0 +1,103 @@ +import React from 'react'; +import _ from 'lodash'; + +import { render } from '../../utils/test-react-testing-library'; +import { DetailContext, DetailContextProps } from '../../pages/Version'; + +import DetailSidebarFundButton from './DetailSidebarFundButton'; + +const ComponentToBeRendered: React.FC<{ contextValue: DetailContextProps }> = ({ contextValue }) => ( + + + +); + +const detailContextValue: DetailContextProps = { + packageName: 'foo', + readMe: 'test', + enableLoading: () => {}, + isLoading: false, + hasNotBeenFound: false, + packageMeta: { + _uplinks: {}, + latest: { + name: '@verdaccio/local-storage', + version: '8.0.1-next.1', + dist: { fileCount: 0, unpackedSize: 0, tarball: 'http://localhost:8080/bootstrap/-/bootstrap-4.3.1.tgz' }, + homepage: 'https://verdaccio.org', + bugs: { + url: 'https://github.com/verdaccio/monorepo/issues', + }, + }, + }, +}; + +describe('test DetailSidebarFundButton', () => { + test('should not display the button if fund is missing', () => { + const wrapper = render(); + + expect(wrapper.queryByText('Fund')).toBeNull(); + }); + + test('should not display the button if url is missing', () => { + const value = _.merge(detailContextValue, { + packageMeta: { + latest: { + funding: {}, + }, + }, + }); + + const wrapper = render(); + + expect(wrapper.queryByText('Fund')).toBeNull(); + }); + + test('should not display the button if url is not a string', () => { + const value = _.merge(detailContextValue, { + packageMeta: { + latest: { + funding: { + url: null, + }, + }, + }, + }); + + const wrapper = render(); + + expect(wrapper.queryByText('Fund')).toBeNull(); + }); + + test('should not display the button if url is not an url', () => { + const value = _.merge(detailContextValue, { + packageMeta: { + latest: { + funding: { + url: 'somethign different as url', + }, + }, + }, + }); + + const wrapper = render(); + + expect(wrapper.queryByText('Fund')).toBeNull(); + }); + + test('should display the button if url is a valid url', () => { + const value = _.merge(detailContextValue, { + packageMeta: { + latest: { + funding: { + url: 'https://opencollective.com/verdaccio', + }, + }, + }, + }); + + const wrapper = render(); + + expect(wrapper.getByText('Fund')).toBeTruthy(); + }); +}); diff --git a/src/components/DetailSidebar/DetailSidebarFundButton.tsx b/src/components/DetailSidebar/DetailSidebarFundButton.tsx index 5e41363..9e343aa 100644 --- a/src/components/DetailSidebar/DetailSidebarFundButton.tsx +++ b/src/components/DetailSidebar/DetailSidebarFundButton.tsx @@ -1,10 +1,12 @@ -import React, { MouseEvent } from 'react'; +import React, { useContext } from 'react'; import styled from '@emotion/styled'; import Favorite from '@material-ui/icons/Favorite'; import Button from '../../muiComponents/Button'; import Link from '../Link'; +import { isURL } from '../../utils/url'; import { Theme } from '../../design-tokens/theme'; +import { DetailContext } from '../../pages/Version'; const StyledLink = styled(Link)<{ theme?: Theme }>(({ theme }) => ({ marginTop: theme && theme.spacing(1), @@ -21,21 +23,21 @@ const StyledFundStrong = styled('strong')({ marginRight: 3, }); -interface Props { - to: string; -} - /* eslint-disable react/jsx-no-bind */ -const DetailSidebarFundButton: React.FC = ({ to }) => { - const preventDefault = (event: MouseEvent) => event.preventDefault(); +const DetailSidebarFundButton: React.FC = () => { + const detailContext = useContext(DetailContext); + + const { packageMeta } = detailContext; + + const fundingUrl = packageMeta?.latest?.funding?.url as string; + + if (!isURL(fundingUrl)) { + return null; + } + return ( - -