Skip to content

Commit ee8c906

Browse files
author
vinkumar2
committed
Fixed Test Coverage for Popover component and other small lint issues
1 parent e98968e commit ee8c906

File tree

8 files changed

+89
-43
lines changed

8 files changed

+89
-43
lines changed

config/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
'**lib/core/**/*.js',
1212
'!**lib/components/**/*.mock.js',
1313
'!**lib/components/**/*.story.js',
14+
'!**lib/components/**/*.styles.js',
1415
'!**lib/styles/**/*.js',
1516
'!**/node_modules/**',
1617
],

lib/components/molecules/Popover/Popover.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
*/
77
import React, { PureComponent, createRef } from 'react';
88
import styled from 'styled-components';
9+
import classnames from 'classnames';
910

1011
import styles from './Popover.style';
1112
import type { Props, State } from './types';
1213

1314
class Popover extends PureComponent<Props, State> {
1415
static defaultProps = {
15-
popOverBody: '',
16-
popOverHeader: '',
1716
isVisible: false,
1817
hidePopoverCloseBtn: false,
19-
trigger: '',
2018
};
2119

2220
/**
@@ -36,23 +34,26 @@ class Popover extends PureComponent<Props, State> {
3634

3735
/**
3836
* @componentDidMount
39-
* Add eventlistner to handle click outside the wrapper to that popover
37+
* Add event listener to handle click outside the wrapper to that popover
4038
* gets closed and setting showPopover state on the basis of prop if passed
4139
*/
4240
componentDidMount() {
43-
document.addEventListener('mousedown', this.handleClickOutside);
41+
document.addEventListener('mousedown', this.handleClickOutside, false);
42+
4443
const { isVisible } = this.props;
45-
this.setState({
46-
showPopover: isVisible,
47-
});
44+
if (isVisible) {
45+
this.setState({
46+
showPopover: isVisible,
47+
});
48+
}
4849
}
4950

5051
/**
5152
* @componentWillUnmount
52-
* Remove all event listener on component unmounting
53+
* Remove all event listener on component un-mounting
5354
*/
5455
componentWillUnmount() {
55-
document.removeEventListener('mousedown', this.handleClickOutside);
56+
document.removeEventListener('mousedown', this.handleClickOutside, false);
5657
}
5758

5859
/**
@@ -109,7 +110,11 @@ class Popover extends PureComponent<Props, State> {
109110
const element = trigger && React.cloneElement(trigger, { onClick: this.handleClick });
110111

111112
return (
112-
<div role="presentation" ref={this.wrapperRef} className={className}>
113+
<div
114+
role="presentation"
115+
ref={this.wrapperRef}
116+
className={classnames('popover-wrap', className)}
117+
>
113118
{element}
114119
{showPopover && (
115120
<div role="dialog" className="popover">
@@ -118,9 +123,7 @@ class Popover extends PureComponent<Props, State> {
118123
X
119124
</button>
120125
)}
121-
<header className="popover__header">
122-
<h3>{popOverHeader}</h3>
123-
</header>
126+
{popOverHeader && <h3 className="popover__header">{popOverHeader}</h3>}
124127
<div className="popover__body">{children}</div>
125128
</div>
126129
)}

lib/components/molecules/Popover/Popover.mock.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
const defaultVisiblePopover = {
2-
className: `example`,
2+
className: 'example',
33
popOverHeader: `This is a popover header text`,
44
trigger: <button>Click to Open</button>,
55
isVisible: true,
66
};
77

88
const hiddenPopover = {
9-
className: `example`,
9+
className: 'example',
1010
popOverHeader: `This is a popover header text`,
1111
trigger: <button>Click to Open</button>,
1212
isVisible: false,
1313
};
1414

1515
const ListPopover = {
16-
className: `example`,
16+
className: 'example',
1717
popOverHeader: `This is a popover header text`,
1818
trigger: <button>Click to Open</button>,
1919
};
2020

2121
const noCloseBtnPopover = {
22-
className: `example`,
22+
className: 'example',
2323
popOverHeader: `This is a popover header text`,
2424
trigger: <button>Click to Open</button>,
2525
isVisible: false,

lib/components/molecules/Popover/Popover.story.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ storiesOf('Molecules', module).addWithChapters('Popover', {
3636
title: 'Popover Variations',
3737
sections: [
3838
{
39-
title: '[isVisible=true] variation',
39+
title: 'By default Visible variation',
4040
sectionFn: () => (
4141
<Popover {...defaultVisiblePopover}>
4242
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
@@ -45,7 +45,7 @@ storiesOf('Molecules', module).addWithChapters('Popover', {
4545
),
4646
},
4747
{
48-
title: '[isVisible=false] variation',
48+
title: 'By default Invisible variation',
4949
sectionFn: () => (
5050
<Popover {...hiddenPopover}>
5151
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
@@ -54,7 +54,7 @@ storiesOf('Molecules', module).addWithChapters('Popover', {
5454
),
5555
},
5656
{
57-
title: 'List(Component) variation',
57+
title: 'List Component as child variation',
5858
sectionFn: () => (
5959
<Popover {...ListPopover}>
6060
<div>
Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,63 @@
1-
import { shallow } from 'enzyme';
2-
// import renderer from 'react-test-renderer';
1+
import { shallow, mount } from 'enzyme';
2+
import { PopoverVanilla } from '../index';
33

4-
import Popover from '../index';
4+
const interactivePopover = {
5+
className: 'example',
6+
popOverHeader: 'This is a popover header text',
7+
trigger: <button id="popover-trigger">Click to Open</button>,
8+
};
59

6-
describe('<Popover />', () => {
7-
let PopoverComponent = '';
10+
describe('<Popover /> Rendering', () => {
11+
let PopoverComponent;
812
beforeEach(() => {
9-
PopoverComponent = shallow(<Popover>Test</Popover>);
13+
PopoverComponent = shallow(<PopoverVanilla>Test</PopoverVanilla>);
1014
});
1115

1216
test('should render correctly', () => {
1317
expect(PopoverComponent).toMatchSnapshot();
1418
});
1519
});
20+
21+
describe('Popover functional behavior', () => {
22+
let PopoverComponent;
23+
24+
test('Should have Close Button By Default and can be closed', () => {
25+
PopoverComponent = mount(
26+
<PopoverVanilla {...interactivePopover} isVisible>
27+
Test
28+
</PopoverVanilla>
29+
);
30+
expect(PopoverComponent.find('.popover__close').length).toEqual(1);
31+
PopoverComponent = mount(
32+
<PopoverVanilla {...interactivePopover} hidePopoverCloseBtn>
33+
Test
34+
</PopoverVanilla>
35+
);
36+
expect(PopoverComponent.find('.popover__close').length).toEqual(0);
37+
});
38+
39+
test('Should open By Default when isVisible set to true', () => {
40+
PopoverComponent = mount(
41+
<PopoverVanilla {...interactivePopover} isVisible>
42+
Test
43+
</PopoverVanilla>
44+
);
45+
expect(PopoverComponent.find('.popover__header').length).toEqual(1);
46+
});
47+
48+
test('Should open and close on Trigger and X button click', () => {
49+
PopoverComponent = mount(
50+
<div>
51+
<p className="test">Test Content</p>
52+
<PopoverVanilla {...interactivePopover}>Test</PopoverVanilla>
53+
</div>
54+
);
55+
// Test Default Open through trigger
56+
PopoverComponent.find('#popover-trigger').simulate('click');
57+
expect(PopoverComponent.find('.popover__header').length).toEqual(1);
58+
// Test Close Popover when clicking outside through trigger the popover
59+
// Test Close through X button
60+
PopoverComponent.find('.popover__close').simulate('click');
61+
expect(PopoverComponent.find('.popover__header').length).toEqual(0);
62+
});
63+
});
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`<Popover /> should render correctly 1`] = `
4-
<Popover
5-
className="Popover-sc-1xo3w5k-0 cPkpcj"
6-
hidePopoverCloseBtn={false}
7-
isVisible={false}
8-
popOverBody=""
9-
popOverHeader=""
10-
trigger=""
11-
>
12-
Test
13-
</Popover>
3+
exports[`<Popover /> Rendering should render correctly 1`] = `
4+
<div
5+
className="popover-wrap"
6+
role="presentation"
7+
/>
148
`;

lib/components/molecules/Popover/types/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import type { Node } from 'react';
33

44
export type Props = {
5-
popOverHeader: Node | string,
5+
popOverHeader?: Node | string,
66
children: Node | string,
7-
isVisible: boolean,
8-
hidePopoverCloseBtn: boolean,
9-
className: string,
7+
isVisible?: boolean,
8+
hidePopoverCloseBtn?: boolean,
9+
className?: string,
1010
trigger: React$Element<*>,
1111
};
1212

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"scripts": {
1414
"test": "cross-env NODE_ENV=test jest --config ./config/jest.config.js",
1515
"test:updateSnapshot": "yarn run test --updateSnapshot",
16-
"test:watch": "yarn run test --watch",
16+
"test:watch": "yarn run test --watch --verbose",
1717
"audit": "yarn run build -- --report",
1818
"clean:dist": "rimraf dist",
1919
"build": "yarn run clean:dist && cross-env BABEL_ENV=production rollup --config rollup.config.js",

0 commit comments

Comments
 (0)