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

Commit 27c05da

Browse files
Merge branch 'master' into refactor/116_convert_class_to_func_router
2 parents 28e3f5d + 950f6de commit 27c05da

File tree

3 files changed

+62
-46
lines changed

3 files changed

+62
-46
lines changed
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11
import React from 'react';
2-
import { shallow } from 'enzyme';
2+
import { shallow, mount } from 'enzyme';
33

4+
import { DetailContext } from '../../pages/Version';
45
import UpLinks from './UpLinks';
56

67
describe('<UpLinks /> component', () => {
78
test('should render the component in default state', () => {
89
const wrapper = shallow(<UpLinks />);
910
expect(wrapper.html()).toMatchSnapshot();
1011
});
12+
13+
test('should render the component with uplinks', () => {
14+
const packageMeta = {
15+
latest: {
16+
name: 'verdaccio',
17+
version: '4.0.0',
18+
author: {
19+
name: 'verdaccio user',
20+
url: '',
21+
avatar: 'https://www.gravatar.com/avatar/000000',
22+
},
23+
dist: { fileCount: 0, unpackedSize: 0 },
24+
},
25+
_uplinks: {
26+
npmjs: {
27+
etag: '"W/"252f0a131cedd3ea82dfefd6fa049558""',
28+
fetched: 1529779934081,
29+
},
30+
},
31+
};
32+
33+
const wrapper = mount(
34+
<DetailContext.Provider value={{ packageMeta }}>
35+
<UpLinks />
36+
</DetailContext.Provider>
37+
);
38+
39+
expect(wrapper.html()).toMatchSnapshot();
40+
});
1141
});

src/components/UpLinks/UpLinks.tsx

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,42 @@
1-
import React, { ReactElement } from 'react';
1+
import React, { useContext } from 'react';
22
import List from '@material-ui/core/List';
33
import ListItem from '@material-ui/core/ListItem';
44

5-
import { DetailContextConsumer } from '../../pages/Version';
5+
import { DetailContext } from '../../pages/Version';
66
import NoItems from '../NoItems';
77
import { formatDateDistance } from '../../utils/package';
88

99
import { Heading, Spacer, ListItemText } from './styles';
1010

11-
class UpLinks extends React.PureComponent<{}> {
12-
public render(): ReactElement<HTMLElement> {
13-
return (
14-
<DetailContextConsumer>
15-
{context => {
16-
return (
17-
context &&
18-
context.packageMeta &&
19-
context.packageMeta &&
20-
context.packageMeta._uplinks &&
21-
context.packageMeta.latest &&
22-
this.renderContent(context.packageMeta._uplinks, context.packageMeta.latest)
23-
);
24-
}}
25-
</DetailContextConsumer>
26-
);
11+
const UpLinks: React.FC = () => {
12+
const { packageMeta } = useContext(DetailContext);
13+
14+
if (!packageMeta || !packageMeta._uplinks || !packageMeta.latest) {
15+
return null;
2716
}
2817

29-
public renderUpLinksList = uplinks => (
30-
<List>
31-
{Object.keys(uplinks)
32-
.reverse()
33-
.map(name => (
34-
<ListItem key={name}>
35-
<ListItemText>{name}</ListItemText>
36-
<Spacer />
37-
<ListItemText>{`${formatDateDistance(uplinks[name].fetched)} ago`}</ListItemText>
38-
</ListItem>
39-
))}
40-
</List>
41-
);
18+
const { _uplinks: uplinks, latest } = packageMeta;
4219

43-
public renderContent(uplinks, { name }): ReactElement<HTMLElement> {
44-
if (Object.keys(uplinks).length > 0) {
45-
return (
46-
uplinks && (
47-
<>
48-
<Heading variant="subtitle1">{'Uplinks'}</Heading>
49-
{this.renderUpLinksList(uplinks)}
50-
</>
51-
)
52-
);
53-
}
54-
return <NoItems text={`${name} has no uplinks.`} />;
20+
if (Object.keys(uplinks).length === 0) {
21+
return <NoItems text={`${latest.name} has no uplinks.`} />;
5522
}
56-
}
23+
24+
return (
25+
<>
26+
<Heading variant="subtitle1">{'Uplinks'}</Heading>
27+
<List>
28+
{Object.keys(uplinks)
29+
.reverse()
30+
.map(name => (
31+
<ListItem key={name}>
32+
<ListItemText>{name}</ListItemText>
33+
<Spacer />
34+
<ListItemText>{`${formatDateDistance(uplinks[name].fetched)} ago`}</ListItemText>
35+
</ListItem>
36+
))}
37+
</List>
38+
</>
39+
);
40+
};
5741

5842
export default UpLinks;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`<UpLinks /> component should render the component in default state 1`] = `""`;
3+
exports[`<UpLinks /> component should render the component in default state 1`] = `null`;
4+
5+
exports[`<UpLinks /> component should render the component with uplinks 1`] = `"<h6 class=\\"MuiTypography-root css-1ikpjfo e14i1sy10 MuiTypography-subtitle1\\">Uplinks</h6><ul class=\\"MuiList-root MuiList-padding\\"><li class=\\"MuiListItem-root MuiListItem-gutters\\"><div class=\\"MuiListItemText-root css-5tz9yo e14i1sy12\\"><span class=\\"MuiTypography-root MuiListItemText-primary MuiTypography-body1\\">npmjs</span></div><div class=\\"css-1l1cv61 e14i1sy11\\"></div><div class=\\"MuiListItemText-root css-5tz9yo e14i1sy12\\"><span class=\\"MuiTypography-root MuiListItemText-primary MuiTypography-body1\\">over 1 year ago</span></div></li></ul>"`;

0 commit comments

Comments
 (0)