1
0
mirror of https://github.com/SomboChea/ui synced 2024-11-24 15:04:27 +07:00

refactor: migrate Uplinks to function component (#165)

This commit is contained in:
Antoine Chalifour 2019-10-07 14:13:05 +02:00 committed by Priscila Oliveira
parent 0ca89dcbe7
commit 950f6defca
3 changed files with 63 additions and 47 deletions

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow, mount } from 'enzyme';
import { DetailContext } from '../../pages/Version';
import UpLinks from './UpLinks'; import UpLinks from './UpLinks';
describe('<UpLinks /> component', () => { describe('<UpLinks /> component', () => {
@ -8,4 +9,33 @@ describe('<UpLinks /> component', () => {
const wrapper = shallow(<UpLinks />); const wrapper = shallow(<UpLinks />);
expect(wrapper.html()).toMatchSnapshot(); expect(wrapper.html()).toMatchSnapshot();
}); });
test('should render the component with uplinks', () => {
const packageMeta = {
latest: {
name: 'verdaccio',
version: '4.0.0',
author: {
name: 'verdaccio user',
url: '',
avatar: 'https://www.gravatar.com/avatar/000000',
},
dist: { fileCount: 0, unpackedSize: 0 },
},
_uplinks: {
npmjs: {
etag: '"W/"252f0a131cedd3ea82dfefd6fa049558""',
fetched: 1529779934081,
},
},
};
const wrapper = mount(
<DetailContext.Provider value={{ packageMeta }}>
<UpLinks />
</DetailContext.Provider>
);
expect(wrapper.html()).toMatchSnapshot();
});
}); });

View File

@ -1,58 +1,42 @@
import React, { ReactElement } from 'react'; import React, { useContext } from 'react';
import List from '@material-ui/core/List'; import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem'; import ListItem from '@material-ui/core/ListItem';
import { DetailContextConsumer } from '../../pages/Version'; import { DetailContext } from '../../pages/Version';
import NoItems from '../NoItems'; import NoItems from '../NoItems';
import { formatDateDistance } from '../../utils/package'; import { formatDateDistance } from '../../utils/package';
import { Heading, Spacer, ListItemText } from './styles'; import { Heading, Spacer, ListItemText } from './styles';
class UpLinks extends React.PureComponent<{}> { const UpLinks: React.FC = () => {
public render(): ReactElement<HTMLElement> { const { packageMeta } = useContext(DetailContext);
return (
<DetailContextConsumer> if (!packageMeta || !packageMeta._uplinks || !packageMeta.latest) {
{context => { return null;
return (
context &&
context.packageMeta &&
context.packageMeta &&
context.packageMeta._uplinks &&
context.packageMeta.latest &&
this.renderContent(context.packageMeta._uplinks, context.packageMeta.latest)
);
}}
</DetailContextConsumer>
);
} }
public renderUpLinksList = uplinks => ( const { _uplinks: uplinks, latest } = packageMeta;
<List>
{Object.keys(uplinks) if (Object.keys(uplinks).length === 0) {
.reverse() return <NoItems text={`${latest.name} has no uplinks.`} />;
.map(name => ( }
<ListItem key={name}>
<ListItemText>{name}</ListItemText> return (
<Spacer /> <>
<ListItemText>{`${formatDateDistance(uplinks[name].fetched)} ago`}</ListItemText> <Heading variant="subtitle1">{'Uplinks'}</Heading>
</ListItem> <List>
))} {Object.keys(uplinks)
</List> .reverse()
.map(name => (
<ListItem key={name}>
<ListItemText>{name}</ListItemText>
<Spacer />
<ListItemText>{`${formatDateDistance(uplinks[name].fetched)} ago`}</ListItemText>
</ListItem>
))}
</List>
</>
); );
};
public renderContent(uplinks, { name }): ReactElement<HTMLElement> {
if (Object.keys(uplinks).length > 0) {
return (
uplinks && (
<>
<Heading variant="subtitle1">{'Uplinks'}</Heading>
{this.renderUpLinksList(uplinks)}
</>
)
);
}
return <NoItems text={`${name} has no uplinks.`} />;
}
}
export default UpLinks; export default UpLinks;

View File

@ -1,3 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`<UpLinks /> component should render the component in default state 1`] = `""`; exports[`<UpLinks /> component should render the component in default state 1`] = `null`;
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>"`;