From 3f0bc75fe07bc1f8724156fd70578fa17efcf531 Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Tue, 29 Oct 2019 03:58:41 +0900 Subject: [PATCH] Add description for intl-test-helper using TSDoc --- tests/helpers/intl-test-helper.tsx | 43 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/tests/helpers/intl-test-helper.tsx b/tests/helpers/intl-test-helper.tsx index 1de1789e07eb..c56fdccfc657 100644 --- a/tests/helpers/intl-test-helper.tsx +++ b/tests/helpers/intl-test-helper.tsx @@ -1,7 +1,6 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import PropTypes from 'prop-types'; import React, { ReactElement, ExoticComponent, @@ -30,6 +29,12 @@ export const defaultIntl = createIntl({ textComponent: 'span', }); +/** + * Attempts to unwrap if wrapped in React.forwardRef + * + * @param element - the component element that might be wrapped in React.forwardRef. + * @returns cloned instance of wrapped element if it's forwardRef, otherwise just element. + */ function unwrapForwardRef(element: ReactElement): WrappedComponentElement { const {type, props} = element as ReactElement; if (type.$$typeof && type.$$typeof === Symbol.for('react.forward_ref')) { @@ -57,24 +62,27 @@ interface ShallowWithIntlOptions extends ShallowRendererProps { intl?: IntlShape; } +/** + * Shallow renders a react component which use injectIntl. + * + * @param element - An instance of {@link IntlInjectedElement} + * @param options - Extra options for Enzyme's {@link enzyme#shallow | `shallow()`} + */ export function shallowWithIntl(element: T, options?: ShallowWithIntlOptions) { const {intl = defaultIntl, ...shallowOptions} = options || {}; // eslint-disable-next-line no-param-reassign element = unwrapForwardRef(element); + if (!isIntlInjectedElement(element)) { throw new Error('shallowWithIntl() allows only components wrapped by injectIntl() HOC. Use shallow() instead.'); } return shallow( - - // Unwrap injectIntl , - - // Override options shallowOptions, ); } @@ -82,6 +90,14 @@ export function shallowWithIntl(element: T, optio interface MountWithIntlOptions extends MountRendererProps { intl?: IntlShape; } + +/** + * Mounts and renders a react component + * and provide context for {@link react-intl#useIntl | `useIntl()`}, `` and etc. + * + * @param element - An instance of any ReactElement containing elements use react-intl inside. + * @param options - Extra options for Enzyme's {@link enzyme#mount | `mount()`} + */ export function mountWithIntl(element: T, options?: MountWithIntlOptions) { const {intl = defaultIntl, ...mountOptions} = options || {}; @@ -96,16 +112,9 @@ export function mountWithIntl(elem /> ) : element; - return mount( - newElement, - - // For useIntl, - { - wrappingComponent: IntlProvider, - wrappingComponentProps: {...intl}, - - // Override options - ...mountOptions, - }, - ); + return mount(newElement, { + wrappingComponent: IntlProvider, + wrappingComponentProps: {...intl}, + ...mountOptions, + }); }