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

Add real provider for i18nProvider mock #181025

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { FormattedMessage } from '@kbn/i18n-react';
import { I18nProviderMock } from './i18n_context_mock';
import { shallow } from 'enzyme';

describe('I18nProviderMock', () => {
it('interpolates to default message if present', () => {
expect(
shallow(
<I18nProviderMock>
<FormattedMessage id="id" defaultMessage="default message" />
</I18nProviderMock>
).html()
).toMatchInlineSnapshot(`"default message"`);
});

it('interpolates to id if default message is not present', () => {
expect(
shallow(
<I18nProviderMock>
<FormattedMessage id="id" />
</I18nProviderMock>
).html()
).toMatchInlineSnapshot(`"id"`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
// eslint-disable-next-line @kbn/eslint/module_migration
import { IntlProvider } from 'react-intl';
import { i18n } from '@kbn/i18n';

const emptyMessages = {};

export const I18nProviderMock: React.FC = ({ children }) => {
return (
<IntlProvider
locale={i18n.getLocale()}
messages={emptyMessages}
defaultLocale={i18n.getLocale()}
formats={i18n.getFormats()}
textComponent={React.Fragment}
>
{children}
</IntlProvider>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
* Side Public License, v 1.
*/

import React from 'react';
import type { PublicMethodsOf } from '@kbn/utility-types';
import { I18nService } from '@kbn/core-i18n-browser-internal';
import type { I18nService } from '@kbn/core-i18n-browser-internal';
import type { I18nStart } from '@kbn/core-i18n-browser';

const PassThroughComponent = ({ children }: { children: React.ReactNode }) => children;
import { I18nProviderMock } from './i18n_context_mock';

const createStartContractMock = () => {
const setupContract: jest.Mocked<I18nStart> = {
// By default mock the Context component so it simply renders all children
Context: jest.fn().mockImplementation(PassThroughComponent),
// Stubbed provider returning the default message or id
Context: jest.fn().mockImplementation(I18nProviderMock),
};
return setupContract;
};
Expand Down
7 changes: 5 additions & 2 deletions packages/core/i18n/core-i18n-browser-mocks/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
]
},
"include": [
"**/*.ts"
"**/*.ts",
"**/*.tsx"
],
"kbn_references": [
"@kbn/utility-types",
"@kbn/core-i18n-browser",
"@kbn/core-i18n-browser-internal"
"@kbn/core-i18n-browser-internal",
"@kbn/i18n-react",
"@kbn/i18n"
],
"exclude": [
"target/**/*",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading