Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Fix lint and typings
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Oct 6, 2019
1 parent 39c0443 commit ee90f34
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
1 change: 0 additions & 1 deletion components/password_reset_form/password_reset_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import PropTypes from 'prop-types';
import React from 'react';
import ReactDOM from 'react-dom';
import {FormattedMessage} from 'react-intl';

import {browserHistory} from 'utils/browser_history';
Expand Down
30 changes: 22 additions & 8 deletions tests/helpers/intl-test-helper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See LICENSE.txt for license information.

import PropTypes from 'prop-types';
import React, {ReactElement} from 'react';
import React, {ReactElement, ExoticComponent, ForwardRefExoticComponent} from 'react';
import {createIntl, IntlProvider, IntlShape, injectIntl} from 'react-intl';
import {mount, shallow, ShallowRendererProps, MountRendererProps} from 'enzyme';

Expand All @@ -17,14 +17,20 @@ const defaultIntl = createIntl({
});

function unwrapForwardRef<WrappedComponentElement extends ReactElement>(element: ReactElement): WrappedComponentElement {
const {type, props} = element;
if (typeof type === 'object' && type['$$typeof'] && type['$$typeof'] === Symbol.for('react.forward_ref')) {
return React.cloneElement<{}>((type as any).render(), props) as WrappedComponentElement;
const {type, props} = element as ReactElement<any, ExoticComponent>;
if (type.$$typeof && type.$$typeof === Symbol.for('react.forward_ref')) {
type ForwardRefComponent = ForwardRefExoticComponent<any> & {
render: () => ReactElement;
};
return React.cloneElement(
(type as ForwardRefComponent).render(),
props,
) as WrappedComponentElement;
}
return element as WrappedComponentElement;
}

export type IntlInjectedElement = ReactElement<any, ReturnType<typeof injectIntl>>;
type IntlInjectedElement = ReactElement<any, ReturnType<typeof injectIntl>>;
export function isIntlInjectedElement(element: ReactElement): element is IntlInjectedElement {
const {type} = element;
if (typeof type === 'function' && type.name === 'WithIntl') {
Expand All @@ -40,6 +46,7 @@ interface ShallowWithIntlOptions extends ShallowRendererProps {
export function shallowWithIntl<T extends IntlInjectedElement>(element: T, options?: ShallowWithIntlOptions) {
const {intl = defaultIntl, ...shallowOptions} = options || {};

// eslint-disable-next-line no-param-reassign
element = unwrapForwardRef<T>(element);
if (!isIntlInjectedElement(element)) {
throw new Error('shallowWithIntl() allows only components wrapped by injectIntl() HOC. Use shallow() instead.');
Expand All @@ -48,7 +55,10 @@ export function shallowWithIntl<T extends IntlInjectedElement>(element: T, optio
return shallow(

// Unwrap injectIntl
<element.type.WrappedComponent intl={intl} {...element.props} />,
<element.type.WrappedComponent
intl={intl}
{...element.props}
/>,

// Override options
shallowOptions,
Expand All @@ -61,11 +71,15 @@ interface MountWithIntlOptions extends MountRendererProps {
export function mountWithIntl<T extends ReactElement | IntlInjectedElement>(element: T, options?: MountWithIntlOptions) {
const {intl = defaultIntl, ...mountOptions} = options || {};

// eslint-disable-next-line no-param-reassign
element = unwrapForwardRef<T>(element);

// Unwrap injectIntl
const newElement = isIntlInjectedElement(element) ? (
<element.type.WrappedComponent intl={intl} {...element.props} />
<element.type.WrappedComponent
intl={intl}
{...element.props}
/>
) : element;

return mount(
Expand All @@ -74,7 +88,7 @@ export function mountWithIntl<T extends ReactElement | IntlInjectedElement>(elem
// For useIntl, <Formatted.../>
{
wrappingComponent: IntlProvider,
wrappingComponentProps: { ...intl },
wrappingComponentProps: {...intl},

// For legacy
context: {
Expand Down

0 comments on commit ee90f34

Please sign in to comment.