From 157477a2d71af18508130a97588e602ccd90c4e3 Mon Sep 17 00:00:00 2001 From: Maryia Lapata Date: Thu, 1 Nov 2018 13:54:22 +0300 Subject: [PATCH] Fix/react intl testing helper functions (#24527) update testing helper functions for react-intl now we use real intl object passed by IntlProvider via context instead of mocking intl object. so now data(strings, JSX) passed to FormattedMessage component via values are compiled and are inserted in the output of FormattedMessage component --- .../saved_objects_installer.test.js.snap | 461 ++++++------------ .../step_index_pattern.test.js.snap | 44 +- .../__jest__/step_index_pattern.test.js | 5 +- .../scripted_field_table.test.js.snap | 2 +- .../field/__snapshots__/field.test.js.snap | 20 +- src/test_utils/public/enzyme_helpers.js | 119 +++-- .../label_template_flyout.test.js.snap | 2 +- .../__snapshots__/index_table.test.js.snap | 50 +- x-pack/test_utils/enzyme_helpers.js | 92 +++- x-pack/test_utils/mocks/intl.js | 18 - 10 files changed, 316 insertions(+), 497 deletions(-) delete mode 100644 x-pack/test_utils/mocks/intl.js diff --git a/src/core_plugins/kibana/public/home/components/tutorial/__snapshots__/saved_objects_installer.test.js.snap b/src/core_plugins/kibana/public/home/components/tutorial/__snapshots__/saved_objects_installer.test.js.snap index f5ae01470c77..16c0126e15c1 100644 --- a/src/core_plugins/kibana/public/home/components/tutorial/__snapshots__/saved_objects_installer.test.js.snap +++ b/src/core_plugins/kibana/public/home/components/tutorial/__snapshots__/saved_objects_installer.test.js.snap @@ -5,216 +5,84 @@ exports[`bulkCreate should display error message when bulkCreate request fails 1 bulkCreate={[Function]} intl={ Object { - "formatDate": [MockFunction], - "formatHTMLMessage": [MockFunction], - "formatMessage": [MockFunction] { - "calls": Array [ - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.defaultButtonLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Imports index pattern, visualizations and pre-defined dashboards.", - "id": "kbn.home.tutorial.savedObject.installLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.loadTitle", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.defaultButtonLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Imports index pattern, visualizations and pre-defined dashboards.", - "id": "kbn.home.tutorial.savedObject.installLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.loadTitle", - }, - ], - Array [ - Object { - "defaultMessage": "Imports index pattern, visualizations and pre-defined dashboards.", - "id": "kbn.home.tutorial.savedObject.installLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.loadTitle", - }, - ], - Array [ - Object { - "defaultMessage": "{savedObjectsLength} saved objects successfully added", - "id": "kbn.home.tutorial.savedObject.addedLabel", - }, - Object { - "savedObjectsLength": 1, - }, - ], - Array [ - Object { - "defaultMessage": "Imports index pattern, visualizations and pre-defined dashboards.", - "id": "kbn.home.tutorial.savedObject.installLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.loadTitle", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.defaultButtonLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Imports index pattern, visualizations and pre-defined dashboards.", - "id": "kbn.home.tutorial.savedObject.installLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.loadTitle", - }, - ], - Array [ - Object { - "defaultMessage": "Imports index pattern, visualizations and pre-defined dashboards.", - "id": "kbn.home.tutorial.savedObject.installLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.loadTitle", - }, - ], - Array [ - Object { - "defaultMessage": "Request failed, Error: {message}", - "id": "kbn.home.tutorial.savedObject.requestFailedErrorMessage", - }, - Object { - "message": "simulated bulkRequest error", - }, - ], - Array [ - Object { - "defaultMessage": "Imports index pattern, visualizations and pre-defined dashboards.", - "id": "kbn.home.tutorial.savedObject.installLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.loadTitle", - }, - ], - ], - "results": Array [ - Object { - "isThrow": false, - "value": "Load Kibana objects", + "defaultFormats": Object { + "date": Object { + "full": Object { + "day": "numeric", + "month": "long", + "weekday": "long", + "year": "numeric", }, - Object { - "isThrow": false, - "value": "Imports index pattern, visualizations and pre-defined dashboards.", + "long": Object { + "day": "numeric", + "month": "long", + "year": "numeric", }, - Object { - "isThrow": false, - "value": "Load Kibana objects", + "medium": Object { + "day": "numeric", + "month": "short", + "year": "numeric", }, - Object { - "isThrow": false, - "value": "Load Kibana objects", + "short": Object { + "day": "numeric", + "month": "numeric", + "year": "2-digit", }, - Object { - "isThrow": false, - "value": "Imports index pattern, visualizations and pre-defined dashboards.", + }, + "number": Object { + "currency": Object { + "style": "currency", }, - Object { - "isThrow": false, - "value": "Load Kibana objects", + "percent": Object { + "style": "percent", }, - Object { - "isThrow": false, - "value": "Imports index pattern, visualizations and pre-defined dashboards.", + }, + "time": Object { + "full": Object { + "hour": "numeric", + "minute": "numeric", + "second": "numeric", + "timeZoneName": "short", }, - Object { - "isThrow": false, - "value": "Load Kibana objects", + "long": Object { + "hour": "numeric", + "minute": "numeric", + "second": "numeric", + "timeZoneName": "short", }, - Object { - "isThrow": false, - "value": "{savedObjectsLength} saved objects successfully added", + "medium": Object { + "hour": "numeric", + "minute": "numeric", + "second": "numeric", }, - Object { - "isThrow": false, - "value": "Imports index pattern, visualizations and pre-defined dashboards.", + "short": Object { + "hour": "numeric", + "minute": "numeric", }, - Object { - "isThrow": false, - "value": "Load Kibana objects", - }, - Object { - "isThrow": false, - "value": "Load Kibana objects", - }, - Object { - "isThrow": false, - "value": "Imports index pattern, visualizations and pre-defined dashboards.", - }, - Object { - "isThrow": false, - "value": "Load Kibana objects", - }, - Object { - "isThrow": false, - "value": "Imports index pattern, visualizations and pre-defined dashboards.", - }, - Object { - "isThrow": false, - "value": "Load Kibana objects", - }, - Object { - "isThrow": false, - "value": "Request failed, Error: {message}", - }, - Object { - "isThrow": false, - "value": "Imports index pattern, visualizations and pre-defined dashboards.", - }, - Object { - "isThrow": false, - "value": "Load Kibana objects", - }, - ], + }, }, - "formatNumber": [MockFunction], - "formatPlural": [MockFunction], - "formatRelative": [MockFunction], - "formatTime": [MockFunction], - "now": [MockFunction], - "textComponent": "span", + "defaultLocale": "en", + "formatDate": [Function], + "formatHTMLMessage": [Function], + "formatMessage": [Function], + "formatNumber": [Function], + "formatPlural": [Function], + "formatRelative": [Function], + "formatTime": [Function], + "formats": Object {}, + "formatters": Object { + "getDateTimeFormat": [Function], + "getMessageFormat": [Function], + "getNumberFormat": [Function], + "getPluralFormat": [Function], + "getRelativeFormat": [Function], + }, + "locale": "en", + "messages": Object {}, + "now": [Function], + "onError": [Function], + "textComponent": Symbol(react.fragment), + "timeZone": null, } } savedObjects={ @@ -279,7 +147,7 @@ exports[`bulkCreate should display error message when bulkCreate request fails 1 color="warning" data-test-subj="loadSavedObjects_failed" size="m" - title="Request failed, Error: {message}" + title="Request failed, Error: simulated bulkRequest error" /> , "key": "installStep", @@ -413,7 +281,7 @@ exports[`bulkCreate should display error message when bulkCreate request fails 1 color="warning" data-test-subj="loadSavedObjects_failed" size="m" - title="Request failed, Error: {message}" + title="Request failed, Error: simulated bulkRequest error" >
- Request failed, Error: {message} + Request failed, Error: simulated bulkRequest error
@@ -443,133 +311,84 @@ exports[`bulkCreate should display success message when bulkCreate is successful bulkCreate={[Function]} intl={ Object { - "formatDate": [MockFunction], - "formatHTMLMessage": [MockFunction], - "formatMessage": [MockFunction] { - "calls": Array [ - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.defaultButtonLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Imports index pattern, visualizations and pre-defined dashboards.", - "id": "kbn.home.tutorial.savedObject.installLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.loadTitle", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.defaultButtonLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Imports index pattern, visualizations and pre-defined dashboards.", - "id": "kbn.home.tutorial.savedObject.installLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.loadTitle", - }, - ], - Array [ - Object { - "defaultMessage": "Imports index pattern, visualizations and pre-defined dashboards.", - "id": "kbn.home.tutorial.savedObject.installLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.loadTitle", - }, - ], - Array [ - Object { - "defaultMessage": "{savedObjectsLength} saved objects successfully added", - "id": "kbn.home.tutorial.savedObject.addedLabel", - }, - Object { - "savedObjectsLength": 1, - }, - ], - Array [ - Object { - "defaultMessage": "Imports index pattern, visualizations and pre-defined dashboards.", - "id": "kbn.home.tutorial.savedObject.installLabel", - }, - ], - Array [ - Object { - "defaultMessage": "Load Kibana objects", - "id": "kbn.home.tutorial.savedObject.loadTitle", - }, - ], - ], - "results": Array [ - Object { - "isThrow": false, - "value": "Load Kibana objects", + "defaultFormats": Object { + "date": Object { + "full": Object { + "day": "numeric", + "month": "long", + "weekday": "long", + "year": "numeric", }, - Object { - "isThrow": false, - "value": "Imports index pattern, visualizations and pre-defined dashboards.", + "long": Object { + "day": "numeric", + "month": "long", + "year": "numeric", }, - Object { - "isThrow": false, - "value": "Load Kibana objects", + "medium": Object { + "day": "numeric", + "month": "short", + "year": "numeric", }, - Object { - "isThrow": false, - "value": "Load Kibana objects", + "short": Object { + "day": "numeric", + "month": "numeric", + "year": "2-digit", }, - Object { - "isThrow": false, - "value": "Imports index pattern, visualizations and pre-defined dashboards.", + }, + "number": Object { + "currency": Object { + "style": "currency", }, - Object { - "isThrow": false, - "value": "Load Kibana objects", + "percent": Object { + "style": "percent", }, - Object { - "isThrow": false, - "value": "Imports index pattern, visualizations and pre-defined dashboards.", + }, + "time": Object { + "full": Object { + "hour": "numeric", + "minute": "numeric", + "second": "numeric", + "timeZoneName": "short", }, - Object { - "isThrow": false, - "value": "Load Kibana objects", + "long": Object { + "hour": "numeric", + "minute": "numeric", + "second": "numeric", + "timeZoneName": "short", }, - Object { - "isThrow": false, - "value": "{savedObjectsLength} saved objects successfully added", + "medium": Object { + "hour": "numeric", + "minute": "numeric", + "second": "numeric", }, - Object { - "isThrow": false, - "value": "Imports index pattern, visualizations and pre-defined dashboards.", + "short": Object { + "hour": "numeric", + "minute": "numeric", }, - Object { - "isThrow": false, - "value": "Load Kibana objects", - }, - ], + }, }, - "formatNumber": [MockFunction], - "formatPlural": [MockFunction], - "formatRelative": [MockFunction], - "formatTime": [MockFunction], - "now": [MockFunction], - "textComponent": "span", + "defaultLocale": "en", + "formatDate": [Function], + "formatHTMLMessage": [Function], + "formatMessage": [Function], + "formatNumber": [Function], + "formatPlural": [Function], + "formatRelative": [Function], + "formatTime": [Function], + "formats": Object {}, + "formatters": Object { + "getDateTimeFormat": [Function], + "getMessageFormat": [Function], + "getNumberFormat": [Function], + "getPluralFormat": [Function], + "getRelativeFormat": [Function], + }, + "locale": "en", + "messages": Object {}, + "now": [Function], + "onError": [Function], + "textComponent": Symbol(react.fragment), + "timeZone": null, } } savedObjects={ @@ -634,7 +453,7 @@ exports[`bulkCreate should display success message when bulkCreate is successful color="success" data-test-subj="loadSavedObjects_success" size="m" - title="{savedObjectsLength} saved objects successfully added" + title="1 saved objects successfully added" /> , "key": "installStep", @@ -808,7 +627,7 @@ exports[`bulkCreate should display success message when bulkCreate is successful color="success" data-test-subj="loadSavedObjects_success" size="m" - title="{savedObjectsLength} saved objects successfully added" + title="1 saved objects successfully added" >
- {savedObjectsLength} saved objects successfully added + 1 saved objects successfully added
diff --git a/src/core_plugins/kibana/public/management/sections/indices/create_index_pattern_wizard/components/step_index_pattern/__jest__/__snapshots__/step_index_pattern.test.js.snap b/src/core_plugins/kibana/public/management/sections/indices/create_index_pattern_wizard/components/step_index_pattern/__jest__/__snapshots__/step_index_pattern.test.js.snap index 0e6091fccbb3..c83d0939dd23 100644 --- a/src/core_plugins/kibana/public/management/sections/indices/create_index_pattern_wizard/components/step_index_pattern/__jest__/__snapshots__/step_index_pattern.test.js.snap +++ b/src/core_plugins/kibana/public/management/sections/indices/create_index_pattern_wizard/components/step_index_pattern/__jest__/__snapshots__/step_index_pattern.test.js.snap @@ -7,7 +7,7 @@ Object { data-test-subj="createIndexPatternStep1Header" errors={ Array [ - "A {indexPatternName} cannot contain spaces or the characters: {characterList}", + "A name cannot contain spaces or the characters: \\\\, /, ?, \\", <, >, |", ] } goToNextStep={[Function]} @@ -16,48 +16,6 @@ Object { onQueryChanged={[Function]} query="?" />, - "i18n": Array [ - Array [ - Object { - "defaultMessage": "A {indexPatternName} cannot contain spaces or the characters: {characterList}", - "id": "kbn.management.createIndexPattern.step.invalidCharactersErrorMessage", - }, - Object { - "characterList": "\\\\, /, ?, \\", <, >, |", - "indexPatternName": "name", - }, - ], - Array [ - Object { - "defaultMessage": "A {indexPatternName} cannot contain spaces or the characters: {characterList}", - "id": "kbn.management.createIndexPattern.step.invalidCharactersErrorMessage", - }, - Object { - "characterList": "\\\\, /, ?, \\", <, >, |", - "indexPatternName": "name", - }, - ], - Array [ - Object { - "defaultMessage": "A {indexPatternName} cannot contain spaces or the characters: {characterList}", - "id": "kbn.management.createIndexPattern.step.invalidCharactersErrorMessage", - }, - Object { - "characterList": "\\\\, /, ?, \\", <, >, |", - "indexPatternName": "name", - }, - ], - Array [ - Object { - "defaultMessage": "A {indexPatternName} cannot contain spaces or the characters: {characterList}", - "id": "kbn.management.createIndexPattern.step.invalidCharactersErrorMessage", - }, - Object { - "characterList": "\\\\, /, ?, \\", <, >, |", - "indexPatternName": "name", - }, - ], - ], } `; diff --git a/src/core_plugins/kibana/public/management/sections/indices/create_index_pattern_wizard/components/step_index_pattern/__jest__/step_index_pattern.test.js b/src/core_plugins/kibana/public/management/sections/indices/create_index_pattern_wizard/components/step_index_pattern/__jest__/step_index_pattern.test.js index fb2b41e85975..89abe1121e70 100644 --- a/src/core_plugins/kibana/public/management/sections/indices/create_index_pattern_wizard/components/step_index_pattern/__jest__/step_index_pattern.test.js +++ b/src/core_plugins/kibana/public/management/sections/indices/create_index_pattern_wizard/components/step_index_pattern/__jest__/step_index_pattern.test.js @@ -19,7 +19,7 @@ import React from 'react'; import { StepIndexPatternComponent } from '../step_index_pattern'; -import { shallowWithIntl, intl } from 'test_utils/enzyme_helpers'; +import { shallowWithIntl } from 'test_utils/enzyme_helpers'; import { Header } from '../components/header'; jest.mock('../../../lib/ensure_minimum_time', () => ({ @@ -79,8 +79,6 @@ const createComponent = props => { }; describe('StepIndexPattern', () => { - afterEach(() => intl.formatMessage.mockClear()); - it('renders the loading state', () => { const component = createComponent(); component.setState({ isLoadingIndices: true }); @@ -109,7 +107,6 @@ describe('StepIndexPattern', () => { component.update(); expect({ component: component.find('[data-test-subj="createIndexPatternStep1Header"]'), - i18n: intl.formatMessage.mock.calls, }).toMatchSnapshot(); }); diff --git a/src/core_plugins/kibana/public/management/sections/indices/edit_index_pattern/scripted_fields_table/__jest__/__snapshots__/scripted_field_table.test.js.snap b/src/core_plugins/kibana/public/management/sections/indices/edit_index_pattern/scripted_fields_table/__jest__/__snapshots__/scripted_field_table.test.js.snap index 4ff6802cbc63..c5466b22eb07 100644 --- a/src/core_plugins/kibana/public/management/sections/indices/edit_index_pattern/scripted_fields_table/__jest__/__snapshots__/scripted_field_table.test.js.snap +++ b/src/core_plugins/kibana/public/management/sections/indices/edit_index_pattern/scripted_fields_table/__jest__/__snapshots__/scripted_field_table.test.js.snap @@ -188,7 +188,7 @@ exports[`ScriptedFieldsTable should show a delete modal 1`] = ` confirmButtonText="Delete" onCancel={[Function]} onConfirm={[Function]} - title="Delete scripted field '{fieldName}'?" + title="Delete scripted field 'ScriptedField'?" /> diff --git a/src/core_plugins/kibana/public/management/sections/settings/components/field/__snapshots__/field.test.js.snap b/src/core_plugins/kibana/public/management/sections/settings/components/field/__snapshots__/field.test.js.snap index e18a3f156dc1..8d8a93aad3a7 100644 --- a/src/core_plugins/kibana/public/management/sections/settings/components/field/__snapshots__/field.test.js.snap +++ b/src/core_plugins/kibana/public/management/sections/settings/components/field/__snapshots__/field.test.js.snap @@ -339,7 +339,7 @@ exports[`Field for array setting should render user value if there is user value
) + .find('IntlProvider').instance().getChildContext(); - if (React.isValidElement(node)) { - return shallow(clonedNode, options); - } - - return clonedNode.shallow(options); -} - -/** - * 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 - */ -export function mountWithIntl(node, { context = {}, childContextTypes = {}, ...props } = {}) { - const clonedNode = cloneNode(node); - const options = getOptions(context, childContextTypes, props); - - if (React.isValidElement(node)) { - return mount(clonedNode, options); - } - - return clonedNode.mount(options); -} - -export { intl }; - -function cloneNode(node) { - if (!node) { - throw new Error(`First argument should be cheerio object or React element, not ${node}`); - } - - return React.cloneElement(node, { intl }); -} - -function getOptions(context, childContextTypes, props) { +function getOptions(context = {}, childContextTypes = {}, props = []) { return { context: { ...context, @@ -76,8 +40,63 @@ function getOptions(context, childContextTypes, props) { }, childContextTypes: { ...childContextTypes, - intl: PropTypes.any, + intl: intlShape, }, ...props, }; } + +/** + * When using React-Intl `injectIntl` on components, props.intl is required. + */ +function nodeWithIntlProp(node) { + return React.cloneElement(node, { intl }); +} + +/** + * 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 + */ +export function shallowWithIntl(node, { context, childContextTypes, ...props } = {}) { + const options = getOptions(context, childContextTypes, props); + + return shallow( + nodeWithIntlProp(node), + options, + ); +} + +/** + * 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 + */ +export function mountWithIntl(node, { context, childContextTypes, ...props } = {}) { + const options = getOptions(context, childContextTypes, props); + + return mount( + nodeWithIntlProp(node), + options, + ); +} + +/** + * 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 + */ +export function renderWithIntl(node, { context, childContextTypes, ...props } = {}) { + const options = getOptions(context, childContextTypes, props); + + return render( + nodeWithIntlProp(node), + options, + ); +} diff --git a/src/ui/public/field_editor/components/field_format_editor/editors/url/__snapshots__/label_template_flyout.test.js.snap b/src/ui/public/field_editor/components/field_format_editor/editors/url/__snapshots__/label_template_flyout.test.js.snap index 2a4c0b1eac04..b044baf51001 100644 --- a/src/ui/public/field_editor/components/field_format_editor/editors/url/__snapshots__/label_template_flyout.test.js.snap +++ b/src/ui/public/field_editor/components/field_format_editor/editors/url/__snapshots__/label_template_flyout.test.js.snap @@ -93,7 +93,7 @@ exports[`LabelTemplateFlyout should render normally 1`] = ` Array [ Object { "input": 1234, - "labelTemplate": "User #{value}", + "labelTemplate": "User #{{value}}", "output": "User #1234", "urlTemplate": "http://company.net/profiles?user_id={{value}}", }, diff --git a/x-pack/plugins/index_management/__jest__/components/__snapshots__/index_table.test.js.snap b/x-pack/plugins/index_management/__jest__/components/__snapshots__/index_table.test.js.snap index f81a3d1d7c6a..46a5b473be73 100644 --- a/x-pack/plugins/index_management/__jest__/components/__snapshots__/index_table.test.js.snap +++ b/x-pack/plugins/index_management/__jest__/components/__snapshots__/index_table.test.js.snap @@ -90,51 +90,51 @@ Array [ exports[`index table should show the right context menu options when more than one closed index is selected 1`] = ` Array [ - "Open {entity}", - "Delete {entity}", + "Open indices", + "Delete indices", ] `; exports[`index table should show the right context menu options when more than one open index is selected 1`] = ` Array [ - "Close {entity}", - "Force merge {entity}", - "Refresh {entity}", - "Clear {entity} cache", - "Flush {entity}", - "Delete {entity}", + "Close indices", + "Force merge indices", + "Refresh indices", + "Clear indices cache", + "Flush indices", + "Delete indices", ] `; exports[`index table should show the right context menu options when one index is selected and closed 1`] = ` Array [ - "Show {entity} settings", - "Show {entity} mapping", - "Edit {entity} settings", - "Open {entity}", - "Delete {entity}", + "Show index settings", + "Show index mapping", + "Edit index settings", + "Open index", + "Delete index", ] `; exports[`index table should show the right context menu options when one index is selected and open 1`] = ` Array [ - "Show {entity} settings", - "Show {entity} mapping", - "Show {entity} stats", - "Edit {entity} settings", - "Close {entity}", - "Force merge {entity}", - "Refresh {entity}", - "Clear {entity} cache", - "Flush {entity}", - "Delete {entity}", + "Show index settings", + "Show index mapping", + "Show index stats", + "Edit index settings", + "Close index", + "Force merge index", + "Refresh index", + "Clear index cache", + "Flush index", + "Delete index", ] `; exports[`index table should show the right context menu options when one open and one closed index is selected 1`] = ` Array [ - "Open {entity}", - "Delete {entity}", + "Open indices", + "Delete indices", ] `; diff --git a/x-pack/test_utils/enzyme_helpers.js b/x-pack/test_utils/enzyme_helpers.js index 22912292c597..b175a319690b 100644 --- a/x-pack/test_utils/enzyme_helpers.js +++ b/x-pack/test_utils/enzyme_helpers.js @@ -4,42 +4,86 @@ * you may not use this file except in compliance with the Elastic License. */ -import PropTypes from 'prop-types'; -import React from 'react'; -import { mount } from 'enzyme'; // eslint-disable-line import/no-extraneous-dependencies -import { intl } from './mocks/intl'; - /** - * Creates the wrapper instance 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 + * 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. */ -export function mountWithIntl(node, { context = {}, childContextTypes = {}, ...props } = {}) { - if (!node) { - throw new Error(`First argument should be cheerio object or React element, not ${node}`); - } - const clonedNode = React.cloneElement(node, { intl }); +import React from 'react'; +import { I18nProvider, intlShape } from '@kbn/i18n/react'; +import { mount, shallow, render } from 'enzyme'; // eslint-disable-line import/no-extraneous-dependencies - const options = { +// Use fake component to extract `intl` property to use in tests. +const { intl } = mount(
) + .find('IntlProvider').instance().getChildContext(); + +function getOptions(context = {}, childContextTypes = {}, props = []) { + return { context: { ...context, intl, }, childContextTypes: { ...childContextTypes, - intl: PropTypes.any, + intl: intlShape, }, ...props, }; - - if (React.isValidElement(node)) { - return mount(clonedNode, options); - } - - return clonedNode.mount(options); } -export { intl }; +/** + * When using React-Intl `injectIntl` on components, props.intl is required. + */ +function nodeWithIntlProp(node) { + return React.cloneElement(node, { intl }); +} + +/** + * 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 + */ +export function shallowWithIntl(node, { context, childContextTypes, ...props } = {}) { + const options = getOptions(context, childContextTypes, props); + + return shallow( + nodeWithIntlProp(node), + options, + ); +} + +/** + * 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 + */ +export function mountWithIntl(node, { context, childContextTypes, ...props } = {}) { + const options = getOptions(context, childContextTypes, props); + + return mount( + nodeWithIntlProp(node), + options, + ); +} + +/** + * 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 + */ +export function renderWithIntl(node, { context, childContextTypes, ...props } = {}) { + const options = getOptions(context, childContextTypes, props); + + return render( + nodeWithIntlProp(node), + options, + ); +} diff --git a/x-pack/test_utils/mocks/intl.js b/x-pack/test_utils/mocks/intl.js deleted file mode 100644 index 6c1fcab3aa74..000000000000 --- a/x-pack/test_utils/mocks/intl.js +++ /dev/null @@ -1,18 +0,0 @@ -/* - * 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. - */ -/* global jest */ - -export const intl = { - formatMessage: jest.fn().mockImplementation(({ defaultMessage }) => defaultMessage), - formatDate: jest.fn().mockImplementation(value => value), - formatTime: jest.fn().mockImplementation(value => value), - formatRelative: jest.fn().mockImplementation(value => value), - formatNumber: jest.fn().mockImplementation(value => value), - formatPlural: jest.fn().mockImplementation(value => value), - formatHTMLMessage: jest.fn().mockImplementation(({ defaultMessage }) => defaultMessage), - now: jest.fn().mockImplementation(() => new Date(1531834573179)), - textComponent: 'span' -};