From f1f8f8ae3f73fd0cecb28d75a161f75f78603a10 Mon Sep 17 00:00:00 2001 From: Andrew Hughson Date: Sat, 5 Oct 2019 09:33:31 +0100 Subject: [PATCH] fix: convert Dist component to hooks (#156) --- src/components/Dist/Dist.test.tsx | 46 ++++++-------------- src/components/Dist/Dist.tsx | 72 +++++++++++++------------------ 2 files changed, 44 insertions(+), 74 deletions(-) diff --git a/src/components/Dist/Dist.test.tsx b/src/components/Dist/Dist.test.tsx index 3ae7d4e..3948be4 100644 --- a/src/components/Dist/Dist.test.tsx +++ b/src/components/Dist/Dist.test.tsx @@ -1,30 +1,16 @@ import React from 'react'; import { mount } from 'enzyme'; + +import { DetailContext } from '../../pages/Version'; 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', - }, - }, -})); - -jest.mock('../../pages/Version', () => ({ - DetailContextConsumer: component => { - return component.children({ packageMeta: mockPackageMeta() }); - }, -})); +const withDistComponent = (packageMeta: React.ContextType['packageMeta']): JSX.Element => ( + + + +); describe(' component', () => { - beforeEach(() => { - jest.resetModules(); - }); - test('should render the component in default state', () => { const packageMeta = { latest: { @@ -36,12 +22,10 @@ describe(' component', () => { }, license: '', }, + _uplinks: {}, }; - // @ts-ignore - mockPackageMeta.mockImplementation(() => packageMeta); - - const wrapper = mount(); + const wrapper = mount(withDistComponent(packageMeta)); expect(wrapper.html()).toMatchSnapshot(); }); @@ -56,12 +40,10 @@ describe(' component', () => { }, license: 'MIT', }, + _uplinks: {}, }; - // @ts-ignore - mockPackageMeta.mockImplementation(() => packageMeta); - - const wrapper = mount(); + const wrapper = mount(withDistComponent(packageMeta)); expect(wrapper.html()).toMatchSnapshot(); }); @@ -79,12 +61,10 @@ describe(' component', () => { url: 'https://www.opensource.org/licenses/mit-license.php', }, }, + _uplinks: {}, }; - // @ts-ignore - mockPackageMeta.mockImplementation(() => packageMeta); - - const wrapper = mount(); + const wrapper = mount(withDistComponent(packageMeta)); expect(wrapper.html()).toMatchSnapshot(); }); }); diff --git a/src/components/Dist/Dist.tsx b/src/components/Dist/Dist.tsx index 4901170..013c68d 100644 --- a/src/components/Dist/Dist.tsx +++ b/src/components/Dist/Dist.tsx @@ -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 ( - - {(context: Partial) => { - return context && context.packageMeta && this.renderDist(context.packageMeta); - }} - - ); - } - - 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; - - const value = dist === 'license' ? formatLicense(distDict[dist]) : distDict[dist]; - const label = ( +const DistChip: FC<{ name: string }> = ({ name, children }) => + children ? ( + - {/* eslint-disable-next-line */} - {dist.replace('-', ' ')}: {value} + {name} + {': '} + {children} - ); - return ; - }); + } + /* eslint-enable */ + /> + ) : null; - return chipsList; +const Dist: FC = () => { + const { packageMeta } = useContext(DetailContext); + + if (!packageMeta) { + return null; } - private renderDist = (packageMeta: PackageMetaInterface) => { - const { dist, license } = packageMeta && packageMeta.latest; + const { dist, license } = packageMeta && packageMeta.latest; - return ( - {'Latest Distribution'}}> - {this.renderChips(dist, license)} - - ); - }; -} + return ( + {'Latest Distribution'}}> + + {dist.fileCount} + {dist.unpackedSize && fileSizeSI(dist.unpackedSize)} + {formatLicense(license)} + + + ); +}; export default Dist;