2018-08-21 18:01:29 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-11-01 14:59:54 +01:00
|
|
|
* 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.
|
2018-08-21 18:01:29 +02:00
|
|
|
*/
|
|
|
|
|
2018-11-06 16:09:13 +01:00
|
|
|
import { I18nProvider, InjectedIntl, intlShape } from '@kbn/i18n/react';
|
|
|
|
import { mount, ReactWrapper, render, shallow } from 'enzyme';
|
|
|
|
import React, { ReactElement, ValidationMap } from 'react';
|
2018-11-01 14:59:54 +01:00
|
|
|
|
|
|
|
// Use fake component to extract `intl` property to use in tests.
|
2018-11-06 16:09:13 +01:00
|
|
|
const { intl } = (mount(
|
|
|
|
<I18nProvider>
|
|
|
|
<br />
|
|
|
|
</I18nProvider>
|
|
|
|
).find('IntlProvider') as ReactWrapper<{}, {}, import('react-intl').IntlProvider>)
|
|
|
|
.instance()
|
|
|
|
.getChildContext();
|
2018-08-21 18:01:29 +02:00
|
|
|
|
2018-11-06 16:09:13 +01:00
|
|
|
function getOptions(context = {}, childContextTypes = {}, props = {}) {
|
2018-11-01 14:59:54 +01:00
|
|
|
return {
|
2018-08-21 18:01:29 +02:00
|
|
|
context: {
|
|
|
|
...context,
|
|
|
|
intl,
|
|
|
|
},
|
|
|
|
childContextTypes: {
|
|
|
|
...childContextTypes,
|
2018-11-01 14:59:54 +01:00
|
|
|
intl: intlShape,
|
2018-08-21 18:01:29 +02:00
|
|
|
},
|
|
|
|
...props,
|
|
|
|
};
|
2018-11-01 14:59:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When using React-Intl `injectIntl` on components, props.intl is required.
|
|
|
|
*/
|
2018-11-06 16:09:13 +01:00
|
|
|
function nodeWithIntlProp<T>(node: ReactElement<T>): ReactElement<T & { intl: InjectedIntl }> {
|
|
|
|
return React.cloneElement<any>(node, { intl });
|
2018-11-01 14:59:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the wrapper instance using shallow with provided intl object into context
|
|
|
|
*
|
|
|
|
* @param node The React element or cheerio wrapper
|
|
|
|
* @param options properties to pass into shallow wrapper
|
|
|
|
* @return The wrapper instance around the rendered output with intl object in context
|
|
|
|
*/
|
2018-11-06 16:09:13 +01:00
|
|
|
export function shallowWithIntl<T>(
|
|
|
|
node: ReactElement<T>,
|
|
|
|
{
|
|
|
|
context,
|
|
|
|
childContextTypes,
|
|
|
|
...props
|
|
|
|
}: {
|
|
|
|
context?: any;
|
|
|
|
childContextTypes?: ValidationMap<any>;
|
|
|
|
} = {}
|
|
|
|
) {
|
2018-11-01 14:59:54 +01:00
|
|
|
const options = getOptions(context, childContextTypes, props);
|
|
|
|
|
2018-11-06 16:09:13 +01:00
|
|
|
return shallow(nodeWithIntlProp(node), options);
|
2018-11-01 14:59:54 +01:00
|
|
|
}
|
2018-08-21 18:01:29 +02:00
|
|
|
|
2018-11-01 14:59:54 +01:00
|
|
|
/**
|
|
|
|
* Creates the wrapper instance using mount with provided intl object into context
|
|
|
|
*
|
|
|
|
* @param node The React element or cheerio wrapper
|
|
|
|
* @param options properties to pass into mount wrapper
|
|
|
|
* @return The wrapper instance around the rendered output with intl object in context
|
|
|
|
*/
|
2018-11-06 16:09:13 +01:00
|
|
|
export function mountWithIntl<T>(
|
|
|
|
node: ReactElement<T>,
|
|
|
|
{
|
|
|
|
context,
|
|
|
|
childContextTypes,
|
|
|
|
...props
|
|
|
|
}: {
|
|
|
|
context?: any;
|
|
|
|
childContextTypes?: ValidationMap<any>;
|
|
|
|
} = {}
|
|
|
|
) {
|
2018-11-01 14:59:54 +01:00
|
|
|
const options = getOptions(context, childContextTypes, props);
|
2018-08-21 18:01:29 +02:00
|
|
|
|
2018-11-06 16:09:13 +01:00
|
|
|
return mount(nodeWithIntlProp(node), options);
|
2018-08-21 18:01:29 +02:00
|
|
|
}
|
|
|
|
|
2018-11-01 14:59:54 +01:00
|
|
|
/**
|
|
|
|
* Creates the wrapper instance using render with provided intl object into context
|
|
|
|
*
|
|
|
|
* @param node The React element or cheerio wrapper
|
|
|
|
* @param options properties to pass into render wrapper
|
|
|
|
* @return The wrapper instance around the rendered output with intl object in context
|
|
|
|
*/
|
2018-11-06 16:09:13 +01:00
|
|
|
export function renderWithIntl<T>(
|
|
|
|
node: ReactElement<T>,
|
|
|
|
{
|
|
|
|
context,
|
|
|
|
childContextTypes,
|
|
|
|
...props
|
|
|
|
}: {
|
|
|
|
context?: any;
|
|
|
|
childContextTypes?: ValidationMap<any>;
|
|
|
|
} = {}
|
|
|
|
) {
|
2018-11-01 14:59:54 +01:00
|
|
|
const options = getOptions(context, childContextTypes, props);
|
|
|
|
|
2018-11-06 16:09:13 +01:00
|
|
|
return render(nodeWithIntlProp(node), options);
|
2018-11-01 14:59:54 +01:00
|
|
|
}
|