Add real provider for i18nProvider mock (#181025)

## Summary

Fix https://github.com/elastic/kibana/issues/180725
This commit is contained in:
Pierre Gayvallet 2024-04-19 09:47:43 +02:00 committed by GitHub
parent 7623720421
commit 5f0b9f7110
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 2333 additions and 485 deletions

View file

@ -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"`);
});
});

View file

@ -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>
);
};

View file

@ -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;
};

View file

@ -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/**/*",

View file

@ -114,33 +114,118 @@ Array [
"results": Array [
Object {
"type": "return",
"value": <KibanaErrorBoundaryProvider
analytics={
"value": <IntlProvider
defaultLocale="en"
formats={
Object {
"optIn": [MockFunction],
"reportEvent": [MockFunction],
"telemetryCounter$": Subject {
"closed": false,
"currentObservers": null,
"hasError": false,
"isStopped": false,
"observers": Array [],
"thrownError": null,
"date": Object {
"full": Object {
"day": "numeric",
"month": "long",
"weekday": "long",
"year": "numeric",
},
"long": Object {
"day": "numeric",
"month": "long",
"year": "numeric",
},
"medium": Object {
"day": "numeric",
"month": "short",
"year": "numeric",
},
"short": Object {
"day": "numeric",
"month": "numeric",
"year": "2-digit",
},
},
"number": Object {
"currency": Object {
"style": "currency",
},
"percent": Object {
"style": "percent",
},
},
"relative": Object {
"days": Object {
"units": "day",
},
"hours": Object {
"units": "hour",
},
"minutes": Object {
"units": "minute",
},
"months": Object {
"units": "month",
},
"seconds": Object {
"units": "second",
},
"years": Object {
"units": "year",
},
},
"time": Object {
"full": Object {
"hour": "numeric",
"minute": "numeric",
"second": "numeric",
"timeZoneName": "short",
},
"long": Object {
"hour": "numeric",
"minute": "numeric",
"second": "numeric",
"timeZoneName": "short",
},
"medium": Object {
"hour": "numeric",
"minute": "numeric",
"second": "numeric",
},
"short": Object {
"hour": "numeric",
"minute": "numeric",
},
},
}
}
locale="en"
messages={Object {}}
textComponent={Symbol(react.fragment)}
>
<KibanaErrorBoundary>
<EuiFlyout
onClose={[Function]}
>
<MountWrapper
className="kbnOverlayMountWrapper"
mount={[Function]}
/>
</EuiFlyout>
</KibanaErrorBoundary>
</KibanaErrorBoundaryProvider>,
<KibanaErrorBoundaryProvider
analytics={
Object {
"optIn": [MockFunction],
"reportEvent": [MockFunction],
"telemetryCounter$": Subject {
"closed": false,
"currentObservers": null,
"hasError": false,
"isStopped": false,
"observers": Array [],
"thrownError": null,
},
}
}
>
<KibanaErrorBoundary>
<EuiFlyout
onClose={[Function]}
>
<MountWrapper
className="kbnOverlayMountWrapper"
mount={[Function]}
/>
</EuiFlyout>
</KibanaErrorBoundary>
</KibanaErrorBoundaryProvider>
</IntlProvider>,
},
],
},
@ -222,33 +307,118 @@ Array [
"results": Array [
Object {
"type": "return",
"value": <KibanaErrorBoundaryProvider
analytics={
"value": <IntlProvider
defaultLocale="en"
formats={
Object {
"optIn": [MockFunction],
"reportEvent": [MockFunction],
"telemetryCounter$": Subject {
"closed": false,
"currentObservers": null,
"hasError": false,
"isStopped": false,
"observers": Array [],
"thrownError": null,
"date": Object {
"full": Object {
"day": "numeric",
"month": "long",
"weekday": "long",
"year": "numeric",
},
"long": Object {
"day": "numeric",
"month": "long",
"year": "numeric",
},
"medium": Object {
"day": "numeric",
"month": "short",
"year": "numeric",
},
"short": Object {
"day": "numeric",
"month": "numeric",
"year": "2-digit",
},
},
"number": Object {
"currency": Object {
"style": "currency",
},
"percent": Object {
"style": "percent",
},
},
"relative": Object {
"days": Object {
"units": "day",
},
"hours": Object {
"units": "hour",
},
"minutes": Object {
"units": "minute",
},
"months": Object {
"units": "month",
},
"seconds": Object {
"units": "second",
},
"years": Object {
"units": "year",
},
},
"time": Object {
"full": Object {
"hour": "numeric",
"minute": "numeric",
"second": "numeric",
"timeZoneName": "short",
},
"long": Object {
"hour": "numeric",
"minute": "numeric",
"second": "numeric",
"timeZoneName": "short",
},
"medium": Object {
"hour": "numeric",
"minute": "numeric",
"second": "numeric",
},
"short": Object {
"hour": "numeric",
"minute": "numeric",
},
},
}
}
locale="en"
messages={Object {}}
textComponent={Symbol(react.fragment)}
>
<KibanaErrorBoundary>
<EuiFlyout
onClose={[Function]}
>
<MountWrapper
className="kbnOverlayMountWrapper"
mount={[Function]}
/>
</EuiFlyout>
</KibanaErrorBoundary>
</KibanaErrorBoundaryProvider>,
<KibanaErrorBoundaryProvider
analytics={
Object {
"optIn": [MockFunction],
"reportEvent": [MockFunction],
"telemetryCounter$": Subject {
"closed": false,
"currentObservers": null,
"hasError": false,
"isStopped": false,
"observers": Array [],
"thrownError": null,
},
}
}
>
<KibanaErrorBoundary>
<EuiFlyout
onClose={[Function]}
>
<MountWrapper
className="kbnOverlayMountWrapper"
mount={[Function]}
/>
</EuiFlyout>
</KibanaErrorBoundary>
</KibanaErrorBoundaryProvider>
</IntlProvider>,
},
],
},