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

Commit 8ce4922

Browse files
committed
refactor: convert Dist component to hooks
1 parent f84fd79 commit 8ce4922

File tree

2 files changed

+45
-75
lines changed

2 files changed

+45
-75
lines changed

src/components/Dist/Dist.test.tsx

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
3-
import Dist from './Dist';
43

5-
const mockPackageMeta = jest.fn(() => ({
6-
latest: {
7-
homepage: 'https://verdaccio.tld',
8-
bugs: {
9-
url: 'https://verdaccio.tld/bugs',
10-
},
11-
dist: {
12-
tarball: 'https://verdaccio.tld/download',
13-
},
14-
},
15-
}));
4+
import { DetailContext } from '../../pages/Version';
5+
import Dist from './Dist';
166

17-
jest.mock('../../pages/Version', () => ({
18-
DetailContextConsumer: component => {
19-
return component.children({ packageMeta: mockPackageMeta() });
20-
},
21-
}));
7+
const withDistComponent = (packageMeta: React.ContextType<typeof DetailContext>['packageMeta']): JSX.Element => (
8+
<DetailContext.Provider value={{ packageMeta }}>
9+
<Dist />
10+
</DetailContext.Provider>
11+
);
2212

2313
describe('<Dist /> component', () => {
24-
beforeEach(() => {
25-
jest.resetModules();
26-
});
27-
2814
test('should render the component in default state', () => {
2915
const packageMeta = {
3016
latest: {
@@ -36,12 +22,10 @@ describe('<Dist /> component', () => {
3622
},
3723
license: '',
3824
},
25+
_uplinks: {},
3926
};
4027

41-
// @ts-ignore
42-
mockPackageMeta.mockImplementation(() => packageMeta);
43-
44-
const wrapper = mount(<Dist />);
28+
const wrapper = mount(withDistComponent(packageMeta));
4529
expect(wrapper.html()).toMatchSnapshot();
4630
});
4731

@@ -56,12 +40,10 @@ describe('<Dist /> component', () => {
5640
},
5741
license: 'MIT',
5842
},
43+
_uplinks: {},
5944
};
6045

61-
// @ts-ignore
62-
mockPackageMeta.mockImplementation(() => packageMeta);
63-
64-
const wrapper = mount(<Dist />);
46+
const wrapper = mount(withDistComponent(packageMeta));
6547
expect(wrapper.html()).toMatchSnapshot();
6648
});
6749

@@ -79,12 +61,10 @@ describe('<Dist /> component', () => {
7961
url: 'https://www.opensource.org/licenses/mit-license.php',
8062
},
8163
},
64+
_uplinks: {},
8265
};
8366

84-
// @ts-ignore
85-
mockPackageMeta.mockImplementation(() => packageMeta);
86-
87-
const wrapper = mount(<Dist />);
67+
const wrapper = mount(withDistComponent(packageMeta));
8868
expect(wrapper.html()).toMatchSnapshot();
8969
});
9070
});

src/components/Dist/Dist.tsx

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,46 @@
1-
import React, { Component } from 'react';
1+
import React, { FC, useContext } from 'react';
22

33
import List from '@material-ui/core/List';
44

5-
import { VersionPageConsumerProps, DetailContextConsumer } from '../../pages/Version';
5+
import { DetailContext } from '../../pages/Version';
66
import { Heading, DistListItem, DistChips } from './styles';
77
import fileSizeSI from '../../utils/file-size';
8-
import { PackageMetaInterface } from 'types/packageMeta';
98
import { formatLicense } from '../../utils/package';
109

11-
class Dist extends Component {
12-
public render(): JSX.Element {
13-
return (
14-
<DetailContextConsumer>
15-
{(context: Partial<VersionPageConsumerProps>) => {
16-
return context && context.packageMeta && this.renderDist(context.packageMeta);
17-
}}
18-
</DetailContextConsumer>
19-
);
20-
}
21-
22-
private renderChips(dist, license: PackageMetaInterface['latest']['license']): (JSX.Element | undefined)[] {
23-
const distDict = {
24-
'file-count': dist.fileCount,
25-
size: dist.unpackedSize && fileSizeSI(dist.unpackedSize),
26-
license,
27-
};
28-
29-
const chipsList = Object.keys(distDict).map((dist, key) => {
30-
if (!distDict[dist]) return;
31-
32-
const value = dist === 'license' ? formatLicense(distDict[dist]) : distDict[dist];
33-
const label = (
10+
const DistChip: FC<{ name: string }> = ({ name, children }) =>
11+
children ? (
12+
<DistChips
13+
// lint rule conflicting with prettier
14+
/* eslint-disable react/jsx-wrap-multilines */
15+
label={
3416
<>
35-
{/* eslint-disable-next-line */}
36-
<b>{dist.replace('-', ' ')}</b>: {value}
17+
<b>{name}</b>
18+
{': '}
19+
{children}
3720
</>
38-
);
39-
return <DistChips key={key} label={label} />;
40-
});
21+
}
22+
/* eslint-enable */
23+
/>
24+
) : null;
4125

42-
return chipsList;
43-
}
26+
const Dist: FC = () => {
27+
const { packageMeta } = useContext(DetailContext);
4428

45-
private renderDist = (packageMeta: PackageMetaInterface) => {
46-
const { dist, license } = packageMeta && packageMeta.latest;
29+
if (!packageMeta) {
30+
return null;
31+
}
4732

48-
return (
49-
<List subheader={<Heading variant="subtitle1">{'Latest Distribution'}</Heading>}>
50-
<DistListItem button={true}>{this.renderChips(dist, license)}</DistListItem>
51-
</List>
52-
);
53-
};
54-
}
33+
const { dist, license } = packageMeta && packageMeta.latest;
34+
35+
return (
36+
<List subheader={<Heading variant="subtitle1">{'Latest Distribution'}</Heading>}>
37+
<DistListItem button={true}>
38+
<DistChip name="file count">{dist.fileCount}</DistChip>
39+
<DistChip name="size">{dist.unpackedSize && fileSizeSI(dist.unpackedSize)}</DistChip>
40+
<DistChip name="license">{formatLicense(license)}</DistChip>
41+
</DistListItem>
42+
</List>
43+
);
44+
};
5545

5646
export default Dist;

0 commit comments

Comments
 (0)