Adding root portal and initial test machinery

This commit is contained in:
Jesús Espino 2020-10-14 08:51:37 +02:00
parent c486ed00be
commit 4142f1e07c
6 changed files with 5185 additions and 5 deletions

View file

@ -14,6 +14,9 @@
<body>
<div id="octo-tasks-app">
</div>
<div id="root-portal">
</div>
</body>
</html>
</html>

5085
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,8 @@
"scripts": {
"pack": "NODE_ENV=production webpack --config webpack.js",
"packdev": "NODE_ENV=dev webpack --config webpack.dev.js",
"watchdev": "NODE_ENV=dev webpack --watch --config webpack.dev.js"
"watchdev": "NODE_ENV=dev webpack --watch --config webpack.dev.js",
"test": "jest"
},
"dependencies": {
"marked": "^1.1.1",
@ -14,14 +15,24 @@
"react-dom": "^16.13.1",
"react-simplemde-editor": "^4.1.3"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
}
},
"devDependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.0.4",
"@types/jest": "^26.0.14",
"@types/marked": "^1.1.0",
"@types/react": "^16.9.49",
"@types/react-dom": "^16.9.8",
"copy-webpack-plugin": "^6.0.3",
"file-loader": "^6.1.0",
"html-webpack-plugin": "^4.5.0",
"jest": "^26.5.3",
"terser-webpack-plugin": "^4.1.0",
"ts-jest": "^26.4.1",
"ts-loader": "^8.0.3",
"typescript": "^4.0.2",
"webpack": "^4.44.1",

View file

@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`components/RootPortal should match snapshot 1`] = `
<div
id="root-portal"
>
<div>
<div>
Testing Portal
</div>
</div>
</div>
`;

View file

@ -0,0 +1,30 @@
// Copyright (c) 2020-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {render} from '@testing-library/react';
import '@testing-library/jest-dom';
import RootPortal from './rootPortal';
describe('components/RootPortal', () => {
beforeEach(() => {
// Quick fix to disregard console error when unmounting a component
console.error = jest.fn();
});
test('should match snapshot', () => {
const rootPortalDiv = document.createElement('div');
rootPortalDiv.id = 'root-portal';
const {getByText, container} = render(
<RootPortal>
<div>{'Testing Portal'}</div>
</RootPortal>,
{container: document.body.appendChild(rootPortalDiv)},
);
expect(getByText('Testing Portal')).toBeVisible();
expect(container).toMatchSnapshot();
});
});

View file

@ -0,0 +1,44 @@
// Copyright (c) 2020-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
type Props = {
children: React.ReactNode
}
export default class RootPortal extends React.PureComponent<Props> {
el: HTMLDivElement
static propTypes = {
children: PropTypes.node,
}
constructor(props: Props) {
super(props);
this.el = document.createElement('div');
}
componentDidMount() {
const rootPortal = document.getElementById('root-portal');
if (rootPortal) {
rootPortal.appendChild(this.el);
}
}
componentWillUnmount() {
const rootPortal = document.getElementById('root-portal');
if (rootPortal) {
rootPortal.removeChild(this.el);
}
}
render() {
return ReactDOM.createPortal(
this.props.children,
this.el,
);
}
}