Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[I18n] Migrate enzyme helpers to TypeScript #25108

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@
* under the License.
*/

/**
* Components using the react-intl module require access to the intl context.
* This is not available when mounting single components in Enzyme.
* These helper functions aim to address that and wrap a valid,
* intl context around them.
*/

import React from 'react';
import { I18nProvider, intlShape } from '@kbn/i18n/react';
import { mount, shallow, render } from 'enzyme';
import { I18nProvider, InjectedIntl, intlShape } from '@kbn/i18n/react';
import { mount, ReactWrapper, render, shallow } from 'enzyme';
import React, { ReactElement } from 'react';
import { IntlProvider } from 'react-intl';

// Use fake component to extract `intl` property to use in tests.
const { intl } = mount(<I18nProvider><br /></I18nProvider>)
.find('IntlProvider').instance().getChildContext();
const { intl } = (mount(
<I18nProvider>
<br />
</I18nProvider>
).find('IntlProvider') as ReactWrapper<{}, {}, IntlProvider>)
.instance()
.getChildContext();

function getOptions(context = {}, childContextTypes = {}, props = []) {
function getOptions(context = {}, childContextTypes = {}, props = {}) {
return {
context: {
...context,
Expand All @@ -49,7 +48,7 @@ function getOptions(context = {}, childContextTypes = {}, props = []) {
/**
* When using React-Intl `injectIntl` on components, props.intl is required.
*/
function nodeWithIntlProp(node) {
function nodeWithIntlProp(node: ReactElement<any>): ReactElement<{ intl: InjectedIntl }> {
return React.cloneElement(node, { intl });
}

Expand All @@ -60,13 +59,20 @@ function nodeWithIntlProp(node) {
* @param options properties to pass into shallow wrapper
* @return The wrapper instance around the rendered output with intl object in context
*/
export function shallowWithIntl(node, { context, childContextTypes, ...props } = {}) {
export function shallowWithIntl(
node: ReactElement<any>,
{
context,
childContextTypes,
...props
}: {
context?: any;
childContextTypes?: any;
} = {}
) {
const options = getOptions(context, childContextTypes, props);

return shallow(
nodeWithIntlProp(node),
options,
);
return shallow(nodeWithIntlProp(node), options);
}

/**
Expand All @@ -76,13 +82,20 @@ export function shallowWithIntl(node, { context, childContextTypes, ...props } =
* @param options properties to pass into mount wrapper
* @return The wrapper instance around the rendered output with intl object in context
*/
export function mountWithIntl(node, { context, childContextTypes, ...props } = {}) {
export function mountWithIntl(
node: ReactElement<any>,
{
context,
childContextTypes,
...props
}: {
context?: any;
childContextTypes?: any;
} = {}
) {
const options = getOptions(context, childContextTypes, props);

return mount(
nodeWithIntlProp(node),
options,
);
return mount(nodeWithIntlProp(node), options);
}

/**
Expand All @@ -92,11 +105,18 @@ export function mountWithIntl(node, { context, childContextTypes, ...props } = {
* @param options properties to pass into render wrapper
* @return The wrapper instance around the rendered output with intl object in context
*/
export function renderWithIntl(node, { context, childContextTypes, ...props } = {}) {
export function renderWithIntl(
node: ReactElement<any>,
{
context,
childContextTypes,
...props
}: {
context?: any;
childContextTypes?: any;
} = {}
) {
const options = getOptions(context, childContextTypes, props);

return render(
nodeWithIntlProp(node),
options,
);
return render(nodeWithIntlProp(node), options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@
* intl context around them.
*/

import React from 'react';
import { I18nProvider, intlShape } from '@kbn/i18n/react';
import { mount, shallow, render } from 'enzyme'; // eslint-disable-line import/no-extraneous-dependencies
import { I18nProvider, InjectedIntl, intlShape } from '@kbn/i18n/react';
import { mount, ReactWrapper, render, shallow } from 'enzyme';
import React, { ReactElement } from 'react';
import { IntlProvider } from 'react-intl';

// Use fake component to extract `intl` property to use in tests.
const { intl } = mount(<I18nProvider><br /></I18nProvider>)
.find('IntlProvider').instance().getChildContext();
const { intl } = (mount(
<I18nProvider>
<br />
</I18nProvider>
).find('IntlProvider') as ReactWrapper<{}, {}, IntlProvider>)
.instance()
.getChildContext();

function getOptions(context = {}, childContextTypes = {}, props = []) {
function getOptions(context = {}, childContextTypes = {}, props = {}) {
return {
context: {
...context,
Expand All @@ -36,7 +42,7 @@ function getOptions(context = {}, childContextTypes = {}, props = []) {
/**
* When using React-Intl `injectIntl` on components, props.intl is required.
*/
function nodeWithIntlProp(node) {
function nodeWithIntlProp(node: ReactElement<any>): ReactElement<{ intl: InjectedIntl }> {
return React.cloneElement(node, { intl });
}

Expand All @@ -47,13 +53,20 @@ function nodeWithIntlProp(node) {
* @param options properties to pass into shallow wrapper
* @return The wrapper instance around the rendered output with intl object in context
*/
export function shallowWithIntl(node, { context, childContextTypes, ...props } = {}) {
export function shallowWithIntl(
node: ReactElement<any>,
{
context,
childContextTypes,
...props
}: {
context?: any;
childContextTypes?: any;
} = {}
) {
const options = getOptions(context, childContextTypes, props);

return shallow(
nodeWithIntlProp(node),
options,
);
return shallow(nodeWithIntlProp(node), options);
}

/**
Expand All @@ -63,13 +76,20 @@ export function shallowWithIntl(node, { context, childContextTypes, ...props } =
* @param options properties to pass into mount wrapper
* @return The wrapper instance around the rendered output with intl object in context
*/
export function mountWithIntl(node, { context, childContextTypes, ...props } = {}) {
export function mountWithIntl(
node: ReactElement<any>,
{
context,
childContextTypes,
...props
}: {
context?: any;
childContextTypes?: any;
} = {}
) {
const options = getOptions(context, childContextTypes, props);

return mount(
nodeWithIntlProp(node),
options,
);
return mount(nodeWithIntlProp(node), options);
}

/**
Expand All @@ -79,11 +99,18 @@ export function mountWithIntl(node, { context, childContextTypes, ...props } = {
* @param options properties to pass into render wrapper
* @return The wrapper instance around the rendered output with intl object in context
*/
export function renderWithIntl(node, { context, childContextTypes, ...props } = {}) {
export function renderWithIntl(
node: ReactElement<any>,
{
context,
childContextTypes,
...props
}: {
context?: any;
childContextTypes?: any;
} = {}
) {
const options = getOptions(context, childContextTypes, props);

return render(
nodeWithIntlProp(node),
options,
);
return render(nodeWithIntlProp(node), options);
}
1 change: 1 addition & 0 deletions x-pack/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"common/**/*",
"server/**/*",
"plugins/**/*",
"test_utils/enzyme_helpers.tsx"
],
"exclude": [
"test/**/*"
Expand Down