refactor: convert Author component to hooks (#150)

This commit is contained in:
Andrew Hughson
2019-10-06 14:44:48 +01:00
committed by Priscila Oliveira
parent a365ec548a
commit d2f1f1c06a
4 changed files with 51 additions and 79 deletions

View File

@@ -1,24 +1,15 @@
import React from 'react';
import { mount } from 'enzyme';
import { DetailContext } from '../../pages/Version';
import Authors from './Author';
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 withAuthorComponent = (packageMeta: React.ContextType<typeof DetailContext>['packageMeta']): JSX.Element => (
<DetailContext.Provider value={{ packageMeta }}>
<Authors />
</DetailContext.Provider>
);
describe('<Author /> component', () => {
beforeEach(() => {
@@ -36,13 +27,12 @@ describe('<Author /> component', () => {
url: '',
avatar: 'https://www.gravatar.com/avatar/000000',
},
dist: { fileCount: 0, unpackedSize: 0 },
},
_uplinks: {},
};
// @ts-ignore
mockPackageMeta.mockImplementation(() => packageMeta);
const wrapper = mount(<Authors />);
const wrapper = mount(withAuthorComponent(packageMeta));
expect(wrapper.html()).toMatchSnapshot();
});
@@ -51,14 +41,13 @@ describe('<Author /> component', () => {
latest: {
name: 'verdaccio',
version: '4.0.0',
dist: { fileCount: 0, unpackedSize: 0 },
},
_uplinks: {},
};
// @ts-ignore
mockPackageMeta.mockImplementation(() => packageMeta);
const wrapper = mount(<Authors />);
expect(wrapper.html()).toEqual('');
const wrapper = mount(withAuthorComponent(packageMeta));
expect(wrapper.html()).toBeNull();
});
test('should render the component when there is no author email', () => {
@@ -71,13 +60,12 @@ describe('<Author /> component', () => {
url: '',
avatar: 'https://www.gravatar.com/avatar/000000',
},
dist: { fileCount: 0, unpackedSize: 0 },
},
_uplinks: {},
};
// @ts-ignore
mockPackageMeta.mockImplementation(() => packageMeta);
const wrapper = mount(<Authors />);
const wrapper = mount(withAuthorComponent(packageMeta));
expect(wrapper.html()).toMatchSnapshot();
});
});