|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { TranslationStringsObject } from "@matrix-org/react-sdk-module-api/lib/types/translations"; |
| 18 | + |
| 19 | +import { ProxiedModuleApi } from "../../src/modules/ProxiedModuleApi"; |
| 20 | +import { stubClient } from "../test-utils"; |
| 21 | +import { setLanguage } from "../../src/languageHandler"; |
| 22 | +import { ModuleRunner } from "../../src/modules/ModuleRunner"; |
| 23 | +import { registerMockModule } from "./MockModule"; |
| 24 | + |
| 25 | +describe("ProxiedApiModule", () => { |
| 26 | + afterEach(() => { |
| 27 | + ModuleRunner.instance.reset(); |
| 28 | + }); |
| 29 | + |
| 30 | + // Note: Remainder is implicitly tested from end-to-end tests of modules. |
| 31 | + |
| 32 | + describe("translations", () => { |
| 33 | + it("should cache translations", () => { |
| 34 | + const api = new ProxiedModuleApi(); |
| 35 | + expect(api.translations).toBeFalsy(); |
| 36 | + |
| 37 | + const translations: TranslationStringsObject = { |
| 38 | + ["custom string"]: { |
| 39 | + "en": "custom string", |
| 40 | + "fr": "custom french string", |
| 41 | + }, |
| 42 | + }; |
| 43 | + api.registerTranslations(translations); |
| 44 | + expect(api.translations).toBe(translations); |
| 45 | + }); |
| 46 | + |
| 47 | + describe("integration", () => { |
| 48 | + it("should translate strings using translation system", async () => { |
| 49 | + // Test setup |
| 50 | + stubClient(); |
| 51 | + |
| 52 | + // Set up a module to pull translations through |
| 53 | + const module = registerMockModule(); |
| 54 | + const en = "custom string"; |
| 55 | + const de = "custom german string"; |
| 56 | + const enVars = "custom variable %(var)s"; |
| 57 | + const varVal = "string"; |
| 58 | + const deVars = "custom german variable %(var)s"; |
| 59 | + const deFull = `custom german variable ${varVal}`; |
| 60 | + expect(module.apiInstance).toBeInstanceOf(ProxiedModuleApi); |
| 61 | + module.apiInstance.registerTranslations({ |
| 62 | + [en]: { |
| 63 | + "en": en, |
| 64 | + "de": de, |
| 65 | + }, |
| 66 | + [enVars]: { |
| 67 | + "en": enVars, |
| 68 | + "de": deVars, |
| 69 | + }, |
| 70 | + }); |
| 71 | + await setLanguage("de"); // calls `registerCustomTranslations()` for us |
| 72 | + |
| 73 | + // See if we can pull the German string out |
| 74 | + expect(module.apiInstance.translateString(en)).toEqual(de); |
| 75 | + expect(module.apiInstance.translateString(enVars, { var: varVal })).toEqual(deFull); |
| 76 | + }); |
| 77 | + }); |
| 78 | + }); |
| 79 | +}); |
0 commit comments