Skip to content

Commit b6b0ecd

Browse files
authored
Merge pull request conventional-changelog#86 from griffithtp/fix/83_support-deprecated-license-object-properties
fix: 83 support deprecated license object properties
2 parents 283464f + 13c7aa6 commit b6b0ecd

File tree

7 files changed

+133
-50
lines changed

7 files changed

+133
-50
lines changed

src/components/Dist/Dist.test.tsx

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import React from 'react';
2+
import { shallow } from 'enzyme';
3+
4+
describe('<Dist /> component', () => {
5+
beforeEach(() => {
6+
jest.resetModules();
7+
});
8+
9+
test('should render the component in default state', () => {
10+
const packageMeta = {
11+
latest: {
12+
name: 'verdaccio',
13+
version: '4.0.0',
14+
dist: {
15+
fileCount: 7,
16+
unpackedSize: 10,
17+
},
18+
license: '',
19+
},
20+
};
21+
jest.doMock('../../pages/version/Version', () => ({
22+
DetailContextConsumer: component => {
23+
return component.children({ packageMeta });
24+
},
25+
}));
26+
27+
const Dist = require('./Dist').default;
28+
const wrapper = shallow(<Dist />);
29+
expect(wrapper.html()).toMatchSnapshot();
30+
});
31+
32+
test('should render the component with license as string', () => {
33+
const packageMeta = {
34+
latest: {
35+
name: 'verdaccio',
36+
version: '4.0.0',
37+
dist: {
38+
fileCount: 7,
39+
unpackedSize: 10,
40+
},
41+
license: 'MIT',
42+
},
43+
};
44+
jest.doMock('../../pages/version/Version', () => ({
45+
DetailContextConsumer: component => {
46+
return component.children({ packageMeta });
47+
},
48+
}));
49+
50+
const Dist = require('./Dist').default;
51+
const wrapper = shallow(<Dist />);
52+
expect(wrapper.html()).toMatchSnapshot();
53+
});
54+
55+
test('should render the component with license as object', () => {
56+
const packageMeta = {
57+
latest: {
58+
name: 'verdaccio',
59+
version: '4.0.0',
60+
dist: {
61+
fileCount: 7,
62+
unpackedSize: 10,
63+
},
64+
license: {
65+
type: 'MIT',
66+
url: 'https://www.opensource.org/licenses/mit-license.php',
67+
},
68+
},
69+
};
70+
jest.doMock('../../pages/version/Version', () => ({
71+
DetailContextConsumer: component => {
72+
return component.children({ packageMeta });
73+
},
74+
}));
75+
76+
const Dist = require('./Dist').default;
77+
const wrapper = shallow(<Dist />);
78+
expect(wrapper.html()).toMatchSnapshot();
79+
});
80+
});

src/components/Dist/Dist.tsx

+14-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { DetailContextConsumer, VersionPageConsumerProps } from '../../pages/ver
66
import { Heading, DistListItem, DistChips } from './styles';
77
import fileSizeSI from '../../utils/file-size';
88
import { PackageMetaInterface } from 'types/packageMeta';
9+
import { formatLicense } from '../../utils/package';
910

1011
class Dist extends Component {
1112
public render(): JSX.Element {
@@ -18,28 +19,25 @@ class Dist extends Component {
1819
);
1920
}
2021

21-
private renderChips(dist, license: string): JSX.Element | never[] {
22+
private renderChips(dist, license: PackageMetaInterface['latest']['license']): (JSX.Element | undefined)[] {
2223
const distDict = {
2324
'file-count': dist.fileCount,
2425
size: dist.unpackedSize && fileSizeSI(dist.unpackedSize),
2526
license,
2627
};
2728

28-
const chipsList = Object.keys(distDict).reduce((componentList, title, key) => {
29-
// @ts-ignore
30-
const value = distDict[title];
31-
if (value) {
32-
const label = (
33-
<span>
34-
{/* eslint-disable-next-line */}
35-
<b>{title.split('-').join(' ')}</b>:{value}
36-
</span>
37-
);
38-
// @ts-ignore is not assignable to parameter of type 'never'
39-
componentList.push(<DistChips key={key} label={label} />);
40-
}
41-
return componentList;
42-
}, []);
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 = (
34+
<>
35+
{/* eslint-disable-next-line */}
36+
<b>{dist.replace('-', ' ')}</b>: {value}
37+
</>
38+
);
39+
return <DistChips key={key} label={label} />;
40+
});
4341

4442
return chipsList;
4543
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<Dist /> component should render the component in default state 1`] = `"<ul class=\\"MuiList-root-1 MuiList-padding-2 MuiList-subheader-4\\"><h3 class=\\"MuiTypography-root-5 MuiTypography-subheading-12 css-hyrz44 estxrtg0\\">Latest Distribution</h3><li class=\\"MuiListItem-root-41 MuiListItem-default-44 MuiListItem-gutters-49 css-z8a2h0 estxrtg1\\"><div role=\\"button\\" class=\\"MuiChip-root-53 css-1le6jk6 estxrtg2\\" tabindex=\\"-1\\"><span class=\\"MuiChip-label-72\\"><b>file count</b>: 7</span></div><div role=\\"button\\" class=\\"MuiChip-root-53 css-1le6jk6 estxrtg2\\" tabindex=\\"-1\\"><span class=\\"MuiChip-label-72\\"><b>size</b>: 10.00 Bytes</span></div></li></ul>"`;
4+
5+
exports[`<Dist /> component should render the component with license as object 1`] = `"<ul class=\\"MuiList-root-155 MuiList-padding-156 MuiList-subheader-158\\"><h3 class=\\"MuiTypography-root-159 MuiTypography-subheading-166 css-hyrz44 estxrtg0\\">Latest Distribution</h3><li class=\\"MuiListItem-root-195 MuiListItem-default-198 MuiListItem-gutters-203 css-z8a2h0 estxrtg1\\"><div role=\\"button\\" class=\\"MuiChip-root-207 css-1le6jk6 estxrtg2\\" tabindex=\\"-1\\"><span class=\\"MuiChip-label-226\\"><b>file count</b>: 7</span></div><div role=\\"button\\" class=\\"MuiChip-root-207 css-1le6jk6 estxrtg2\\" tabindex=\\"-1\\"><span class=\\"MuiChip-label-226\\"><b>size</b>: 10.00 Bytes</span></div><div role=\\"button\\" class=\\"MuiChip-root-207 css-1le6jk6 estxrtg2\\" tabindex=\\"-1\\"><span class=\\"MuiChip-label-226\\"><b>license</b>: MIT</span></div></li></ul>"`;
6+
7+
exports[`<Dist /> component should render the component with license as string 1`] = `"<ul class=\\"MuiList-root-78 MuiList-padding-79 MuiList-subheader-81\\"><h3 class=\\"MuiTypography-root-82 MuiTypography-subheading-89 css-hyrz44 estxrtg0\\">Latest Distribution</h3><li class=\\"MuiListItem-root-118 MuiListItem-default-121 MuiListItem-gutters-126 css-z8a2h0 estxrtg1\\"><div role=\\"button\\" class=\\"MuiChip-root-130 css-1le6jk6 estxrtg2\\" tabindex=\\"-1\\"><span class=\\"MuiChip-label-149\\"><b>file count</b>: 7</span></div><div role=\\"button\\" class=\\"MuiChip-root-130 css-1le6jk6 estxrtg2\\" tabindex=\\"-1\\"><span class=\\"MuiChip-label-149\\"><b>size</b>: 10.00 Bytes</span></div><div role=\\"button\\" class=\\"MuiChip-root-130 css-1le6jk6 estxrtg2\\" tabindex=\\"-1\\"><span class=\\"MuiChip-label-149\\"><b>license</b>: MIT</span></div></li></ul>"`;

src/components/Package/Package.tsx

+21-23
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,29 @@ import HomeIcon from '@material-ui/icons/Home';
66
import ListItem from '@material-ui/core/ListItem';
77
import Tooltip from '@material-ui/core/Tooltip';
88

9+
import { PackageMetaInterface } from 'types/packageMeta';
910
import Tag from '../Tag';
1011
import fileSizeSI from '../../utils/file-size';
1112
import { formatDate, formatDateDistance } from '../../utils/package';
12-
13+
import {
14+
Author,
15+
Avatar,
16+
Description,
17+
Details,
18+
GridRightAligned,
19+
Icon,
20+
IconButton,
21+
OverviewItem,
22+
PackageList,
23+
PackageListItem,
24+
PackageListItemText,
25+
PackageTitle,
26+
Published,
27+
TagContainer,
28+
Text,
29+
WrapperLink,
30+
} from './styles';
31+
import { isURL } from '../../utils/url';
1332
interface Author {
1433
name: string;
1534
avatar?: string;
@@ -30,32 +49,11 @@ export interface PackageInterface {
3049
author: Author;
3150
description?: string;
3251
keywords?: string[];
33-
license?: string | null;
52+
license?: PackageMetaInterface['latest']['license'];
3453
homepage?: string;
3554
bugs?: Bugs;
3655
dist?: Dist;
3756
}
38-
// interface Props {} & PackageInterface;
39-
40-
import {
41-
Author,
42-
Avatar,
43-
Description,
44-
Details,
45-
GridRightAligned,
46-
Icon,
47-
IconButton,
48-
OverviewItem,
49-
PackageList,
50-
PackageListItem,
51-
PackageListItemText,
52-
PackageTitle,
53-
Published,
54-
TagContainer,
55-
Text,
56-
WrapperLink,
57-
} from './styles';
58-
import { isURL } from '../../utils/url';
5957

6058
const Package: React.FC<PackageInterface> = ({
6159
author: { name: authorName, avatar: authorAvatar },

src/utils/package.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ describe('formatLicense', (): void => {
1313
});
1414

1515
test('should check license field for other value', (): void => {
16-
expect(formatLicense(null)).toBeNull();
17-
expect(formatLicense({})).toBeNull();
18-
expect(formatLicense([])).toBeNull();
16+
expect(formatLicense(null)).toBeUndefined();
17+
expect(formatLicense({})).toBeUndefined();
18+
expect(formatLicense([])).toBeUndefined();
1919
});
2020
});
2121

src/utils/package.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ import { isObject } from 'util';
66

77
export const TIMEFORMAT = 'DD.MM.YYYY, HH:mm:ss';
88

9-
export interface License {
10-
type: string;
11-
url: string;
12-
}
13-
149
/**
1510
* Formats license field for webui.
1611
* @see https://docs.npmjs.com/files/package.json#license
1712
*/
1813
// License should use type License defined above, but conflicts with the unit test that provide array or empty object
1914
/* eslint-disable @typescript-eslint/no-explicit-any */
20-
export function formatLicense(license: any): string | null {
15+
export function formatLicense(license: any): string | undefined {
2116
if (isString(license)) {
2217
return license;
2318
}
@@ -26,7 +21,7 @@ export function formatLicense(license: any): string | null {
2621
return license.type;
2722
}
2823

29-
return null;
24+
return;
3025
}
3126

3227
export interface Repository {

types/packageMeta.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ export interface PackageMetaInterface {
55
fileCount: number;
66
unpackedSize: number;
77
};
8-
license: string;
8+
license?: Partial<LicenseInterface> | string;
99
};
1010
_uplinks: {};
1111
}
12+
13+
interface LicenseInterface {
14+
type: string;
15+
url: string;
16+
}

0 commit comments

Comments
 (0)