Skip to content
This repository was archived by the owner on Jan 16, 2022. It is now read-only.

Commit 6bd38b8

Browse files
authored
fix: remove prevent default and use react context (#411)
* fix: remove prevent default and use react context * chore: remove string check
1 parent 6e2bfac commit 6bd38b8

File tree

3 files changed

+120
-15
lines changed

3 files changed

+120
-15
lines changed

src/components/DetailSidebar/DetailSidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const DetailSidebar: React.FC = () => {
3838
/>
3939
<ActionBar />
4040
<Install />
41-
{packageMeta?.latest?.funding && <DetailSidebarFundButton to={packageMeta.latest.funding.url} />}
41+
<DetailSidebarFundButton />
4242
<Repository />
4343
<Engines />
4444
<Dist />
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import React from 'react';
2+
import _ from 'lodash';
3+
4+
import { render } from '../../utils/test-react-testing-library';
5+
import { DetailContext, DetailContextProps } from '../../pages/Version';
6+
7+
import DetailSidebarFundButton from './DetailSidebarFundButton';
8+
9+
const ComponentToBeRendered: React.FC<{ contextValue: DetailContextProps }> = ({ contextValue }) => (
10+
<DetailContext.Provider value={contextValue}>
11+
<DetailSidebarFundButton />
12+
</DetailContext.Provider>
13+
);
14+
15+
const detailContextValue: DetailContextProps = {
16+
packageName: 'foo',
17+
readMe: 'test',
18+
enableLoading: () => {},
19+
isLoading: false,
20+
hasNotBeenFound: false,
21+
packageMeta: {
22+
_uplinks: {},
23+
latest: {
24+
name: '@verdaccio/local-storage',
25+
version: '8.0.1-next.1',
26+
dist: { fileCount: 0, unpackedSize: 0, tarball: 'http://localhost:8080/bootstrap/-/bootstrap-4.3.1.tgz' },
27+
homepage: 'https://verdaccio.org',
28+
bugs: {
29+
url: 'https://github.com/verdaccio/monorepo/issues',
30+
},
31+
},
32+
},
33+
};
34+
35+
describe('test DetailSidebarFundButton', () => {
36+
test('should not display the button if fund is missing', () => {
37+
const wrapper = render(<ComponentToBeRendered contextValue={detailContextValue} />);
38+
39+
expect(wrapper.queryByText('Fund')).toBeNull();
40+
});
41+
42+
test('should not display the button if url is missing', () => {
43+
const value = _.merge(detailContextValue, {
44+
packageMeta: {
45+
latest: {
46+
funding: {},
47+
},
48+
},
49+
});
50+
51+
const wrapper = render(<ComponentToBeRendered contextValue={value} />);
52+
53+
expect(wrapper.queryByText('Fund')).toBeNull();
54+
});
55+
56+
test('should not display the button if url is not a string', () => {
57+
const value = _.merge(detailContextValue, {
58+
packageMeta: {
59+
latest: {
60+
funding: {
61+
url: null,
62+
},
63+
},
64+
},
65+
});
66+
67+
const wrapper = render(<ComponentToBeRendered contextValue={value} />);
68+
69+
expect(wrapper.queryByText('Fund')).toBeNull();
70+
});
71+
72+
test('should not display the button if url is not an url', () => {
73+
const value = _.merge(detailContextValue, {
74+
packageMeta: {
75+
latest: {
76+
funding: {
77+
url: 'somethign different as url',
78+
},
79+
},
80+
},
81+
});
82+
83+
const wrapper = render(<ComponentToBeRendered contextValue={value} />);
84+
85+
expect(wrapper.queryByText('Fund')).toBeNull();
86+
});
87+
88+
test('should display the button if url is a valid url', () => {
89+
const value = _.merge(detailContextValue, {
90+
packageMeta: {
91+
latest: {
92+
funding: {
93+
url: 'https://opencollective.com/verdaccio',
94+
},
95+
},
96+
},
97+
});
98+
99+
const wrapper = render(<ComponentToBeRendered contextValue={value} />);
100+
101+
expect(wrapper.getByText('Fund')).toBeTruthy();
102+
});
103+
});

src/components/DetailSidebar/DetailSidebarFundButton.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import React, { MouseEvent } from 'react';
1+
import React, { useContext } from 'react';
22
import styled from '@emotion/styled';
33
import Favorite from '@material-ui/icons/Favorite';
44

55
import Button from '../../muiComponents/Button';
66
import Link from '../Link';
7+
import { isURL } from '../../utils/url';
78
import { Theme } from '../../design-tokens/theme';
9+
import { DetailContext } from '../../pages/Version';
810

911
const StyledLink = styled(Link)<{ theme?: Theme }>(({ theme }) => ({
1012
marginTop: theme && theme.spacing(1),
@@ -21,21 +23,21 @@ const StyledFundStrong = styled('strong')({
2123
marginRight: 3,
2224
});
2325

24-
interface Props {
25-
to: string;
26-
}
27-
2826
/* eslint-disable react/jsx-no-bind */
29-
const DetailSidebarFundButton: React.FC<Props> = ({ to }) => {
30-
const preventDefault = (event: MouseEvent<HTMLButtonElement>) => event.preventDefault();
27+
const DetailSidebarFundButton: React.FC = () => {
28+
const detailContext = useContext(DetailContext);
29+
30+
const { packageMeta } = detailContext;
31+
32+
const fundingUrl = packageMeta?.latest?.funding?.url as string;
33+
34+
if (!isURL(fundingUrl)) {
35+
return null;
36+
}
37+
3138
return (
32-
<StyledLink external={true} to={to}>
33-
<Button
34-
color="primary"
35-
fullWidth={true}
36-
onClick={preventDefault}
37-
startIcon={<StyledFavoriteIcon />}
38-
variant="outlined">
39+
<StyledLink external={true} to={fundingUrl}>
40+
<Button color="primary" fullWidth={true} startIcon={<StyledFavoriteIcon />} variant="outlined">
3941
<StyledFundStrong>{'Fund'}</StyledFundStrong>
4042
{'this package'}
4143
</Button>

0 commit comments

Comments
 (0)