Skip to content

Commit e5e9638

Browse files
authored
Post-quiz cleaning (#99)
Post-quiz cleaning
2 parents 3ad4097 + 198eb20 commit e5e9638

File tree

12 files changed

+207
-47
lines changed

12 files changed

+207
-47
lines changed

config/jest.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module.exports = {
99
snapshotSerializers: [
1010
'enzyme-to-json/serializer',
1111
],
12-
collectCoverage: true,
1312
collectCoverageFrom: [
1413
'source/components/**/*.js',
1514
'source/hocs/**/*.js',

config/styleguide.config.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
[
2-
{
1+
[{
32
"name": "Documentation",
4-
"sections": [
5-
{
3+
"sections": [{
64
"name": "Introduction",
75
"content": "../docs/PREAMBULE.md"
86
},
@@ -68,6 +66,7 @@
6866
"ImagePreloader",
6967
"Input",
7068
"List",
69+
"Portal",
7170
"Select",
7271
"SimpleLocalNavigation",
7372
"Spinner",
@@ -94,13 +93,13 @@
9493
"directory": "hooks",
9594
"items": [
9695
"useInterval",
97-
"useLazyLoad"
96+
"useLazyLoad",
97+
"usePortal"
9898
]
9999
},
100100
{
101101
"name": "Utils",
102-
"sections": [
103-
{
102+
"sections": [{
104103
"name": "Environment",
105104
"content": "../source/utils/ENVIRONMENT.md"
106105
},

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"build:lib": "yarn clean && yarn lib:build",
1212
"clean": "node ./scripts/clean.js",
1313
"coverage": "yarn test --coverage",
14-
"coveralls": "yarn test --runInBand && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
14+
"coveralls": "yarn coverage --runInBand && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
1515
"dev": "concurrently --names \"SERVER,TESTS\" --prefix \"{time}|{name}\" \"yarn styleguide:watch\" \"yarn test:watch\"",
1616
"git:reset": "git checkout master && git pull",
1717
"lib:build": "rollup -c config/rollup.config.js",
@@ -29,7 +29,7 @@
2929
"release:patch": "yarn git:reset && yarn bump:version patch && yarn build && git add . && yarn create:tag && git push --follow-tags && yarn publish",
3030
"styleguide:build": "styleguidist build --config ./config/styleguide.config.js",
3131
"styleguide:watch": "styleguidist server --config ./config/styleguide.config.js",
32-
"ci": "yarn lint && yarn test --runInBand",
32+
"ci": "yarn lint && yarn coverage --runInBand",
3333
"test": "jest --config ./config/jest.config.js",
3434
"test:update": "jest -u --config ./config/jest.config.js",
3535
"test:watch": "yarn test --watchAll --notify"

source/components/HideComponent/__snapshots__/index.spec.js.snap

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,57 @@
1+
import React from 'react';
12
import PropTypes from 'prop-types';
23

3-
const HideComponent = ({ hide, children }) => {
4-
if (hide === true) {
4+
// use class component so `react-docgen` can parse this file as a component (doesn't work with functional components)
5+
/**
6+
* Component that's used to either show or hide own children.
7+
*
8+
* Since both `show` and `hide` shouldn't be used at the same time, `hide` has a priority
9+
*
10+
* | `hide` | `show` | Component visible? |
11+
* | ----------- | ----------- | ----------- |
12+
* | `undefined` | `undefined` | yes |
13+
* | `undefined` | `true` | yes |
14+
* | `undefined` | `false` | no |
15+
* | `true` | (any value) | no |
16+
* | `false` | (any value) | yes |
17+
*/
18+
class HideComponent extends React.PureComponent {
19+
shouldShow() {
20+
const { show, hide } = this.props;
21+
22+
if (typeof hide !== 'undefined') {
23+
if (hide === true) {
24+
return false;
25+
}
26+
27+
return true;
28+
}
29+
30+
if (typeof show === 'undefined' || show === true) {
31+
return true;
32+
}
33+
534
return false;
635
}
736

8-
return children;
9-
};
37+
render() {
38+
return this.shouldShow() ? this.props.children : null;
39+
}
40+
}
1041

1142
HideComponent.propTypes = {
43+
/** @ignore */
44+
children: PropTypes.node,
45+
/** if `true` the component will be hidden; if `false` the component will be shown */
1246
hide: PropTypes.bool,
47+
/** if `true` the component will be shown; if `false` the component will be hidden */
48+
show: PropTypes.bool,
1349
};
1450

1551
HideComponent.defaultProps = {
16-
hide: false,
52+
children: null,
53+
hide: undefined,
54+
show: undefined,
1755
};
1856

1957
export default HideComponent;

source/components/HideComponent/index.spec.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@ import { mount } from 'enzyme';
33

44
import HideComponent from './index';
55

6-
test('Hide component tree', () => {
7-
const component = mount(
8-
<HideComponent hide>
9-
<div> test </div>
10-
</HideComponent>,
11-
);
12-
expect(component).toMatchSnapshot();
13-
});
6+
describe('Check if show/hide logic works', () => {
7+
const testData = [
8+
{ hide: undefined, show: undefined, result: true },
9+
{ hide: undefined, show: true, result: true },
10+
{ hide: undefined, show: false, result: false },
11+
{ hide: true, show: undefined, result: false },
12+
{ hide: true, show: true, result: false },
13+
{ hide: true, show: false, result: false },
14+
{ hide: false, show: undefined, result: true },
15+
{ hide: false, show: true, result: true },
16+
{ hide: false, show: false, result: true },
17+
];
18+
19+
testData.forEach((testSet) => {
20+
test(`Data set (hide: ${String(testSet.hide)}, show: ${String(testSet.show)}) => ${String(testSet.result)}`, () => {
21+
const component = mount(
22+
<HideComponent hide={testSet.hide} show={testSet.show}>test</HideComponent>
23+
);
1424

15-
test('Show component tree', () => {
16-
const component = mount(
17-
<HideComponent>
18-
<div> test </div>
19-
</HideComponent>,
20-
);
21-
expect(component).toMatchSnapshot();
25+
expect(component.text()).toBe(testSet.result ? 'test' : null);
26+
});
27+
});
2228
});

source/components/Portal/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Open the source and search for "Portal demo".
2+
3+
```js static
4+
<Portal id="asd">
5+
<p>This Portal demo will be rendered in Body</p>
6+
</Portal>
7+
```

source/components/Portal/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// disable jest here
2+
import PropTypes from 'prop-types';
3+
import { createPortal } from 'react-dom';
4+
5+
import usePortal from '../../hooks/usePortal';
6+
7+
/**
8+
* A `Portal` Component that simplifies the usage of React portals.
9+
*/
10+
const Portal = /* istanbul ignore next */ ({ id, children }) => {
11+
const target = usePortal(id);
12+
13+
return createPortal(
14+
children,
15+
target,
16+
);
17+
};
18+
19+
Portal.propTypes = {
20+
/** @ignore */
21+
children: PropTypes.node.isRequired,
22+
/** `ID` of the DOM element */
23+
id: PropTypes.string.isRequired,
24+
};
25+
26+
export default Portal;

source/components/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export { default as Spinner } from './Spinner';
3737
export { default as Switch } from './Switch';
3838
export { default as Timeago } from './Timeago';
3939
export { default as VideoPlayIcon } from './VideoPlayIcon';
40+
export { default as Portal } from './Portal';
4041

4142
export { default as StyledErrorDisplayingContent } from './StyledErrorDisplayingContent';
4243

source/hooks/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
* importable.
55
*/
66

7+
export { default as usePortal } from './usePortal';
78
export { default as useLazyLoad } from './useLazyLoad';
89
export { default as useInterval } from './useInterval';

0 commit comments

Comments
 (0)