Skip to content

Commit

Permalink
Add real provider for i18nProvider mock (#181025)
Browse files Browse the repository at this point in the history
## Summary

Fix #180725
  • Loading branch information
pgayvallet authored Apr 19, 2024
1 parent 7623720 commit 5f0b9f7
Show file tree
Hide file tree
Showing 6 changed files with 2,488 additions and 640 deletions.
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

0 comments on commit 5f0b9f7

Please sign in to comment.