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

refactor: convert Dist component to hooks #156

Merged
merged 3 commits into from
Oct 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 13 additions & 33 deletions src/components/Dist/Dist.test.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
import React from 'react';
import { mount } from 'enzyme';
import Dist from './Dist';

const mockPackageMeta = jest.fn(() => ({
latest: {
homepage: 'https://verdaccio.tld',
bugs: {
url: 'https://verdaccio.tld/bugs',
},
dist: {
tarball: 'https://verdaccio.tld/download',
},
},
}));
import { DetailContext } from '../../pages/Version';
import Dist from './Dist';

jest.mock('../../pages/Version', () => ({
DetailContextConsumer: component => {
return component.children({ packageMeta: mockPackageMeta() });
},
}));
const withDistComponent = (packageMeta: React.ContextType<typeof DetailContext>['packageMeta']): JSX.Element => (
<DetailContext.Provider value={{ packageMeta }}>
<Dist />
</DetailContext.Provider>
);

describe('<Dist /> component', () => {
beforeEach(() => {
jest.resetModules();
});

test('should render the component in default state', () => {
const packageMeta = {
latest: {
Expand All @@ -36,12 +22,10 @@ describe('<Dist /> component', () => {
},
license: '',
},
_uplinks: {},
};

// @ts-ignore
mockPackageMeta.mockImplementation(() => packageMeta);

const wrapper = mount(<Dist />);
const wrapper = mount(withDistComponent(packageMeta));
expect(wrapper.html()).toMatchSnapshot();
});

Expand All @@ -56,12 +40,10 @@ describe('<Dist /> component', () => {
},
license: 'MIT',
},
_uplinks: {},
};

// @ts-ignore
mockPackageMeta.mockImplementation(() => packageMeta);

const wrapper = mount(<Dist />);
const wrapper = mount(withDistComponent(packageMeta));
expect(wrapper.html()).toMatchSnapshot();
});

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

// @ts-ignore
mockPackageMeta.mockImplementation(() => packageMeta);

const wrapper = mount(<Dist />);
const wrapper = mount(withDistComponent(packageMeta));
expect(wrapper.html()).toMatchSnapshot();
});
});
74 changes: 32 additions & 42 deletions src/components/Dist/Dist.tsx
Original file line number Diff line number Diff line change
@@ -1,56 +1,46 @@
import React, { Component } from 'react';
import React, { FC, useContext } from 'react';

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

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

class Dist extends Component {
public render(): JSX.Element {
return (
<DetailContextConsumer>
{(context: Partial<VersionPageConsumerProps>) => {
return context && context.packageMeta && this.renderDist(context.packageMeta);
}}
</DetailContextConsumer>
);
}

private renderChips(dist, license: PackageMetaInterface['latest']['license']): (JSX.Element | undefined)[] {
const distDict = {
'file-count': dist.fileCount,
size: dist.unpackedSize && fileSizeSI(dist.unpackedSize),
license,
};

const chipsList = Object.keys(distDict).map((dist, key) => {
if (!distDict[dist]) return;
Copy link
Contributor Author

@bighuggies bighuggies Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a bug or intentional, but this will hide the chips for file count or size if they happen to be 0. I've done a strict refactor and included the same behavior.


const value = dist === 'license' ? formatLicense(distDict[dist]) : distDict[dist];
const label = (
const DistChip: FC<{ name: string }> = ({ name, children }) =>
children ? (
<DistChips
// lint rule conflicting with prettier
/* eslint-disable react/jsx-wrap-multilines */
label={
<>
{/* eslint-disable-next-line */}
<b>{dist.replace('-', ' ')}</b>: {value}
<b>{name}</b>
{': '}
{children}
</>
);
return <DistChips key={key} label={label} />;
});
}
/* eslint-enable */
/>
) : null;

return chipsList;
}
const Dist: FC = () => {
const { packageMeta } = useContext(DetailContext);

private renderDist = (packageMeta: PackageMetaInterface) => {
const { dist, license } = packageMeta && packageMeta.latest;
if (!packageMeta) {
return null;
}

return (
<List subheader={<Heading variant="subtitle1">{'Latest Distribution'}</Heading>}>
<DistListItem button={true}>{this.renderChips(dist, license)}</DistListItem>
</List>
);
};
}
const { dist, license } = packageMeta && packageMeta.latest;

return (
<List subheader={<Heading variant="subtitle1">{'Latest Distribution'}</Heading>}>
<DistListItem button={true}>
<DistChip name="file count">{dist.fileCount}</DistChip>
<DistChip name="size">{dist.unpackedSize && fileSizeSI(dist.unpackedSize)}</DistChip>
<DistChip name="license">{formatLicense(license)}</DistChip>
</DistListItem>
</List>
);
};

export default Dist;