test-utils: add built-in TranslationApi mock

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-09-14 21:11:50 +02:00
parent c4af7014da
commit a7c01da530
6 changed files with 112 additions and 1 deletions
+1
View File
@@ -61,6 +61,7 @@
"@testing-library/user-event": "^14.0.0",
"@types/react": "^16.13.1 || ^17.0.0",
"cross-fetch": "^3.1.5",
"i18next": "^22.4.15",
"zen-observable": "^0.10.0"
},
"peerDependencies": {
@@ -0,0 +1,84 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
TranslationApi,
TranslationRef,
TranslationSnapshot,
} from '@backstage/core-plugin-api/alpha';
import { createInstance as createI18n, type i18n as I18n } from 'i18next';
import ObservableImpl from 'zen-observable';
import { Observable } from '@backstage/types';
// Internal import to avoid code duplication, this will lead to duplication in build output
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import { toInternalTranslationRef } from '../../../../../core-plugin-api/src/translation/TranslationRef';
const DEFAULT_LANGUAGE = 'en';
/** @alpha */
export class MockTranslationApi implements TranslationApi {
static create() {
const i18n = createI18n({
fallbackLng: DEFAULT_LANGUAGE,
supportedLngs: [DEFAULT_LANGUAGE],
interpolation: {
escapeValue: false,
},
ns: [],
defaultNS: false,
fallbackNS: false,
});
i18n.init();
return new MockTranslationApi(i18n);
}
#i18n: I18n;
private constructor(i18n: I18n) {
this.#i18n = i18n;
}
getTranslation<TMessages extends { [key in string]: string }>(
translationRef: TranslationRef<string, TMessages>,
): TranslationSnapshot<TMessages> {
const internalRef = toInternalTranslationRef(translationRef);
const t = this.#i18n.getFixedT(null, internalRef.id);
const defaultMessages = internalRef.getDefaultMessages() as TMessages;
return {
ready: true,
t: (key, options) => {
return t(key as string, {
...options,
defaultValue: defaultMessages[key],
});
},
};
}
translation$<TMessages extends { [key in string]: string }>(): Observable<
TranslationSnapshot<TMessages>
> {
// No need to implement, getTranslation will always return a ready snapshot
return new ObservableImpl<TranslationSnapshot<TMessages>>(_subscriber => {
return () => {};
});
}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { MockTranslationApi } from './MockTranslationApi';
@@ -20,3 +20,4 @@ export * from './ErrorApi';
export * from './FetchApi';
export * from './PermissionApi';
export * from './StorageApi';
export * from './TranslationApi';
@@ -20,10 +20,17 @@ import {
fetchApiRef,
storageApiRef,
} from '@backstage/core-plugin-api';
import { MockErrorApi, MockFetchApi, MockStorageApi } from './apis';
import { translationApiRef } from '@backstage/core-plugin-api/alpha';
import {
MockErrorApi,
MockFetchApi,
MockStorageApi,
MockTranslationApi,
} from './apis';
export const mockApis = [
createApiFactory(errorApiRef, new MockErrorApi()),
createApiFactory(fetchApiRef, new MockFetchApi()),
createApiFactory(storageApiRef, MockStorageApi.create()),
createApiFactory(translationApiRef, MockTranslationApi.create()),
];
+1
View File
@@ -10063,6 +10063,7 @@ __metadata:
"@testing-library/user-event": ^14.0.0
"@types/react": ^16.13.1 || ^17.0.0
cross-fetch: ^3.1.5
i18next: ^22.4.15
msw: ^1.0.0
zen-observable: ^0.10.0
peerDependencies: