tests: workaround perfectscroll importing issue in jest

This commit is contained in:
Roman Hotsiy 2018-03-26 21:56:39 +03:00
parent 83af48112c
commit 29a7f299a1
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0

View File

@ -1,9 +1,17 @@
import * as React from 'react'; import * as React from 'react';
import { default as PerfectScrollbarConstructor } from 'perfect-scrollbar'; import PerfectScrollbarType, * as PerfectScrollbarNamespace from 'perfect-scrollbar';
import psStyles from 'perfect-scrollbar/css/perfect-scrollbar.css'; import psStyles from 'perfect-scrollbar/css/perfect-scrollbar.css';
import styled, { injectGlobal } from '../styled-components'; import styled, { injectGlobal } from '../styled-components';
/*
* perfect scrollbar umd bundle uses exports assignment while module uses default export
* so when bundled with webpack default export works but with jest it crashes
* That's why the following ugly fix is required
*/
const PerfectScrollbarConstructor =
PerfectScrollbarNamespace.default || ((PerfectScrollbarNamespace as any) as PerfectScrollbarType);
injectGlobal`${psStyles && psStyles.toString()}`; injectGlobal`${psStyles && psStyles.toString()}`;
const StyledScrollWrapper = styled.div` const StyledScrollWrapper = styled.div`
@ -11,12 +19,12 @@ const StyledScrollWrapper = styled.div`
`; `;
export class PerfectScrollbar extends React.Component<{ export class PerfectScrollbar extends React.Component<{
options?: PerfectScrollbarConstructor.Options; options?: PerfectScrollbarType.Options;
className?: string; className?: string;
updateFn: (fn) => void; updateFn: (fn) => void;
}> { }> {
private _container: HTMLElement; private _container: HTMLElement;
private inst: PerfectScrollbarConstructor; private inst: PerfectScrollbarType;
componentDidMount() { componentDidMount() {
this.inst = new PerfectScrollbarConstructor(this._container, this.props.options || {}); this.inst = new PerfectScrollbarConstructor(this._container, this.props.options || {});