From f6e62d95bbf63c3744a2549831d4b8a44fc2c463 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Sun, 7 Jul 2019 19:30:01 +0200 Subject: [PATCH] chore: adds tests for action bar component (#88) --- src/components/ActionBar/ActionBar.test.tsx | 48 +++++++++++++++++++ src/components/ActionBar/ActionBar.tsx | 18 +++---- .../__snapshots__/ActionBar.test.tsx.snap | 3 ++ 3 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 src/components/ActionBar/ActionBar.test.tsx create mode 100644 src/components/ActionBar/__snapshots__/ActionBar.test.tsx.snap diff --git a/src/components/ActionBar/ActionBar.test.tsx b/src/components/ActionBar/ActionBar.test.tsx new file mode 100644 index 0000000..8c22f84 --- /dev/null +++ b/src/components/ActionBar/ActionBar.test.tsx @@ -0,0 +1,48 @@ +import React from 'react'; +import { shallow } from 'enzyme'; + +describe(' component', () => { + beforeEach(() => { + jest.resetModules(); + }); + + test('should render the component in default state', () => { + const packageMeta = { + latest: { + homepage: 'https://verdaccio.tld', + bugs: { + url: 'https://verdaccio.tld/bugs', + }, + dist: { + tarball: 'https://verdaccio.tld/download' + }, + }, + }; + + jest.doMock('../../pages/version/Version', () => ({ + DetailContextConsumer: component => { + return component.children({ packageMeta }); + }, + })); + + const ActionBar = require('./ActionBar').default; + const wrapper = shallow(); + expect(wrapper.html()).toMatchSnapshot(); + }); + + test('when there is no action bar data', () => { + const packageMeta = { + latest: {}, + }; + + jest.doMock('../../pages/version/Version', () => ({ + DetailContextConsumer: component => { + return component.children({ packageMeta }); + }, + })); + + const ActionBar = require('./ActionBar').default; + const wrapper = shallow(); + expect(wrapper.html()).toEqual(''); + }); +}); diff --git a/src/components/ActionBar/ActionBar.tsx b/src/components/ActionBar/ActionBar.tsx index b8a0fbf..f034a17 100644 --- a/src/components/ActionBar/ActionBar.tsx +++ b/src/components/ActionBar/ActionBar.tsx @@ -44,7 +44,7 @@ class ActionBar extends Component { ); } - private renderActionBarListItems = packageMeta => { + private renderActionBar = ({ packageMeta }) => { // @ts-ignore const { latest: { bugs: { url: issue } = {}, homepage, dist: { tarball } = {} } = {} } = packageMeta; @@ -68,15 +68,15 @@ class ActionBar extends Component { return component; }, []); - return ( - <> - {renderList} - - ); - }; + if (renderList.length > 0) { + return ( + + {renderList} + + ); + } - private renderActionBar = ({ packageMeta = {} }) => { - return {this.renderActionBarListItems(packageMeta)}; + return null; }; } diff --git a/src/components/ActionBar/__snapshots__/ActionBar.test.tsx.snap b/src/components/ActionBar/__snapshots__/ActionBar.test.tsx.snap new file mode 100644 index 0000000..fd8ea2f --- /dev/null +++ b/src/components/ActionBar/__snapshots__/ActionBar.test.tsx.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` component should render the component in default state 1`] = `"
"`;