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

Commit b74ca22

Browse files
alfonsoarjuanpicado
authored andcommitted
fix: refactor/116 RegistryInfoContent is converted to functional component (#229)
* refactor:116[PackageList] component is converted to functional * Refactor:#116 - Registry info content is converted to functional component * refactor/116 - fix lint error * refactor:116 - more lint errors * refactor/116 - lint error * refactor:116 - remove snapshot * refactor: address code review comments #116 * refactor: fix lint error * refactor: code review changes * refactor add missed file * refactor: lint error * refactor: lint * refactor: lint * refactor: fix lint error
1 parent 803da1c commit b74ca22

File tree

4 files changed

+87
-90
lines changed

4 files changed

+87
-90
lines changed

src/components/PackageList/PackageList.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Fragment, ReactElement } from 'react';
1+
import React, { Fragment, ReactNode } from 'react';
22

33
import Package from '../Package';
44
import Help from '../Help';
@@ -12,22 +12,23 @@ interface Props {
1212
packages: PackageInterface[];
1313
}
1414

15-
export const PackageList: React.FC<Props> = props => {
16-
const renderPackages: () => ReactElement<HTMLElement>[] = () => {
17-
return props.packages.map((pkg, i) => {
18-
const { name, version, description, time, keywords, dist, homepage, bugs, author } = pkg;
15+
export const PackageList: React.FC<Props> = ({ packages }) => {
16+
const renderPackages: () => ReactNode[] = () => {
17+
return packages.map(({ name, version, description, time, keywords, dist, homepage, bugs, author, license }, i) => {
1918
// TODO: move format license to API side.
20-
const license = formatLicense(pkg.license);
19+
const _license = formatLicense(license);
2120
return (
2221
<Fragment key={i}>
2322
{i !== 0 && <Divider />}
24-
<Package {...{ name, dist, version, author, description, license, time, keywords, homepage, bugs }} />
23+
<Package
24+
{...{ name, dist, version, author, description, license: _license, time, keywords, homepage, bugs }}
25+
/>
2526
</Fragment>
2627
);
2728
});
2829
};
2930

30-
const hasPackages: () => boolean = () => props.packages.length > 0;
31+
const hasPackages: () => boolean = () => packages.length > 0;
3132

3233
return (
3334
<div className={'package-list-items'}>
Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
import React from 'react';
2-
import { mount } from 'enzyme';
2+
import { render, cleanup, fireEvent } from '@testing-library/react';
33

44
import RegistryInfoContent from './RegistryInfoContent';
55

66
describe('<RegistryInfoContent /> component', () => {
7-
test('should render the component in default state with npm tab', () => {
8-
const wrapper = mount(<RegistryInfoContent registryUrl="https://registry.verdaccio.org" scope="@" />);
9-
expect(wrapper.html()).toMatchSnapshot();
7+
afterEach(() => {
8+
cleanup();
109
});
1110

12-
test('should render the component in default state with pnpm tab', () => {
13-
const wrapper = mount(<RegistryInfoContent registryUrl="https://registry.verdaccio.org" scope="@" />);
14-
wrapper.setState({ tabPosition: 1 });
15-
expect(wrapper.html()).toMatchSnapshot();
11+
test('should load the component with no data', () => {
12+
const { getByTestId } = render(<RegistryInfoContent registryUrl={''} scope={''} />);
13+
const unorderedListOfTodos = getByTestId('tabs-el');
14+
expect(unorderedListOfTodos.children.length).toBe(1);
1615
});
1716

18-
test('should render the component in default state with yarn tab', () => {
19-
const wrapper = mount(<RegistryInfoContent registryUrl="https://registry.verdaccio.org" scope="@" />);
20-
wrapper.setState({ tabPosition: 2 });
21-
expect(wrapper.html()).toMatchSnapshot();
17+
test('should load the appropiate tab content when the tab is clicked', () => {
18+
const props = { registryUrl: 'http://localhost:4872', scope: '@' };
19+
const pnpmTabTextContent = `pnpm adduser --registry ${props.registryUrl}`;
20+
21+
// Render the component.
22+
const { container, getByTestId } = render(
23+
<RegistryInfoContent registryUrl={props.registryUrl} scope={props.scope} />
24+
);
25+
26+
// Assert the text content for pnpm tab is not present intially
27+
expect(container.textContent).not.toContain(pnpmTabTextContent);
28+
29+
const pnpmTab = getByTestId('pnpm-tab');
30+
fireEvent.click(pnpmTab);
31+
32+
// Assert the text content is correct after clicking on the tab.
33+
expect(container.textContent).toContain(pnpmTabTextContent);
2234
});
2335
});
Lines changed: 54 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React, { useState } from 'react';
22
import { css } from 'emotion';
33

44
import CopyToClipBoard from '../CopyToClipBoard';
@@ -11,86 +11,77 @@ import Tab from '../../muiComponents/Tab';
1111
import { CommandContainer } from './styles';
1212
import { Props, State } from './types';
1313

14-
/* eslint react/prop-types:0 */
15-
function TabContainer({ children }): JSX.Element {
16-
return (
17-
<CommandContainer>
18-
<Typography
19-
className={css`
20-
padding: 0;
21-
min-height: 170;
22-
`}
23-
component="div">
24-
{children}
25-
</Typography>
26-
</CommandContainer>
27-
);
28-
}
14+
const RegistryInfoContent: React.FC<Props> = props => {
15+
const [tabPosition, setTabPosition] = useState<State['tabPosition']>(0);
16+
const handleChange = (event: React.ChangeEvent<{}>, tabPosition: number): void => {
17+
event.preventDefault();
18+
setTabPosition(tabPosition);
19+
};
2920

30-
class RegistryInfoContent extends Component<Props, State> {
31-
public state = {
32-
tabPosition: 0,
21+
const renderNpmTab = (scope: string, registryUrl: string): JSX.Element => {
22+
return (
23+
<>
24+
<CopyToClipBoard text={getCLISetConfigRegistry(`${NODE_MANAGER.npm} set`, scope, registryUrl)} />
25+
<CopyToClipBoard text={getCLISetRegistry(`${NODE_MANAGER.npm} adduser`, registryUrl)} />
26+
<CopyToClipBoard text={getCLIChangePassword(NODE_MANAGER.npm, registryUrl)} />
27+
</>
28+
);
3329
};
3430

35-
public render(): JSX.Element {
36-
return <div>{this.renderTabs()}</div>;
37-
}
31+
const renderPnpmTab = (scope: string, registryUrl: string): JSX.Element => {
32+
return (
33+
<>
34+
<CopyToClipBoard text={getCLISetConfigRegistry(`${NODE_MANAGER.pnpm} set`, scope, registryUrl)} />
35+
<CopyToClipBoard text={getCLISetRegistry(`${NODE_MANAGER.pnpm} adduser`, registryUrl)} />
36+
<CopyToClipBoard text={getCLIChangePassword(NODE_MANAGER.pnpm, registryUrl)} />
37+
</>
38+
);
39+
};
3840

39-
private handleChange = (event: React.ChangeEvent<{}>, tabPosition: number) => {
40-
event.preventDefault();
41-
this.setState({ tabPosition });
41+
const renderYarnTab = (scope: string, registryUrl: string): JSX.Element => {
42+
return <CopyToClipBoard text={getCLISetConfigRegistry(`${NODE_MANAGER.yarn} config set`, scope, registryUrl)} />;
4243
};
4344

44-
private renderTabs(): JSX.Element {
45-
const { scope, registryUrl } = this.props;
46-
const { tabPosition } = this.state;
45+
const renderTabs = (): JSX.Element => {
46+
const { scope, registryUrl } = props;
4747

4848
return (
49-
<React.Fragment>
49+
<>
5050
<Tabs
51+
data-testid={'tabs-el'}
5152
indicatorColor="primary"
52-
onChange={this.handleChange}
53+
onChange={handleChange}
5354
textColor="primary"
5455
value={tabPosition}
5556
variant="fullWidth">
56-
<Tab label={NODE_MANAGER.npm} />
57-
<Tab label={NODE_MANAGER.pnpm} />
58-
<Tab label={NODE_MANAGER.yarn} />
57+
<Tab data-testid={'npm-tab'} label={NODE_MANAGER.npm} />
58+
<Tab data-testid={'pnpm-tab'} label={NODE_MANAGER.pnpm} />
59+
<Tab data-testid={'yarn-tab'} label={NODE_MANAGER.yarn} />
5960
</Tabs>
60-
{tabPosition === 0 && <TabContainer>{this.renderNpmTab(scope, registryUrl)}</TabContainer>}
61-
{tabPosition === 1 && <TabContainer>{this.renderPNpmTab(scope, registryUrl)}</TabContainer>}
62-
{tabPosition === 2 && <TabContainer>{this.renderYarnTab(scope, registryUrl)}</TabContainer>}
63-
</React.Fragment>
61+
{tabPosition === 0 && <TabContainer>{renderNpmTab(scope, registryUrl)}</TabContainer>}
62+
{tabPosition === 1 && <TabContainer>{renderPnpmTab(scope, registryUrl)}</TabContainer>}
63+
{tabPosition === 2 && <TabContainer>{renderYarnTab(scope, registryUrl)}</TabContainer>}
64+
</>
6465
);
65-
}
66+
};
6667

67-
private renderNpmTab(scope: string, registryUrl: string): JSX.Element {
68+
/* eslint react/prop-types:0 */
69+
const TabContainer = ({ children }): JSX.Element => {
6870
return (
69-
<React.Fragment>
70-
<CopyToClipBoard text={getCLISetConfigRegistry(`${NODE_MANAGER.npm} set`, scope, registryUrl)} />
71-
<CopyToClipBoard text={getCLISetRegistry(`${NODE_MANAGER.npm} adduser`, registryUrl)} />
72-
<CopyToClipBoard text={getCLIChangePassword(NODE_MANAGER.npm, registryUrl)} />
73-
</React.Fragment>
71+
<CommandContainer>
72+
<Typography
73+
className={css`
74+
padding: 0;
75+
min-height: 170;
76+
`}
77+
component="div">
78+
{children}
79+
</Typography>
80+
</CommandContainer>
7481
);
75-
}
76-
77-
private renderPNpmTab(scope: string, registryUrl: string): JSX.Element {
78-
return (
79-
<React.Fragment>
80-
<CopyToClipBoard text={getCLISetConfigRegistry(`${NODE_MANAGER.pnpm} set`, scope, registryUrl)} />
81-
<CopyToClipBoard text={getCLISetRegistry(`${NODE_MANAGER.pnpm} adduser`, registryUrl)} />
82-
<CopyToClipBoard text={getCLIChangePassword(NODE_MANAGER.pnpm, registryUrl)} />
83-
</React.Fragment>
84-
);
85-
}
82+
};
8683

87-
private renderYarnTab(scope: string, registryUrl: string): JSX.Element {
88-
return (
89-
<React.Fragment>
90-
<CopyToClipBoard text={getCLISetConfigRegistry(`${NODE_MANAGER.yarn} config set`, scope, registryUrl)} />
91-
</React.Fragment>
92-
);
93-
}
94-
}
84+
return <div>{renderTabs()}</div>;
85+
};
9586

9687
export default RegistryInfoContent;

src/components/RegistryInfoContent/__snapshots__/RegistryInfoContent.test.tsx.snap

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)