1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-07-31 13:44:28 +03:00

Fix instances of double translation and guard translation calls using typescript (#11443)

Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
This commit is contained in:
Michael Telatynski
2023-08-22 16:32:05 +01:00
committed by GitHub
parent d13b6e1b41
commit ac70f7ac9b
157 changed files with 554 additions and 780 deletions

View File

@ -14,7 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { EventType, JoinRule, MatrixClient, MatrixEvent, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import {
EventType,
HistoryVisibility,
JoinRule,
MatrixClient,
MatrixEvent,
Room,
RoomMember,
} from "matrix-js-sdk/src/matrix";
import { render } from "@testing-library/react";
import { ReactElement } from "react";
import { Mocked, mocked } from "jest-mock";
@ -571,4 +579,39 @@ describe("TextForEvent", () => {
).toEqual("@a changed the join rule to a not implemented one");
});
});
describe("textForHistoryVisibilityEvent()", () => {
type TestCase = [string, { result: string }];
const testCases: TestCase[] = [
[
HistoryVisibility.Invited,
{ result: "@a made future room history visible to all room members, from the point they are invited." },
],
[
HistoryVisibility.Joined,
{ result: "@a made future room history visible to all room members, from the point they joined." },
],
[HistoryVisibility.Shared, { result: "@a made future room history visible to all room members." }],
[HistoryVisibility.WorldReadable, { result: "@a made future room history visible to anyone." }],
];
it.each(testCases)(
"returns correct message when room join rule changed to %s",
(historyVisibility, { result }) => {
expect(
textForEvent(
new MatrixEvent({
type: "m.room.history_visibility",
sender: "@a",
content: {
history_visibility: historyVisibility,
},
state_key: "",
}),
mockClient,
),
).toEqual(result);
},
);
});
});

View File

@ -21,6 +21,7 @@ import { render, screen } from "@testing-library/react";
import { shouldShowFeedback } from "../../../../src/utils/Feedback";
import BetaCard from "../../../../src/components/views/beta/BetaCard";
import SettingsStore from "../../../../src/settings/SettingsStore";
import { TranslationKey } from "../../../../src/languageHandler";
jest.mock("../../../../src/utils/Feedback");
jest.mock("../../../../src/settings/SettingsStore");
@ -31,10 +32,10 @@ describe("<BetaCard />", () => {
beforeEach(() => {
mocked(SettingsStore).getBetaInfo.mockReturnValue({
title: "title",
title: "title" as TranslationKey,
caption: () => "caption",
feedbackLabel: "feedbackLabel",
feedbackSubheading: "feedbackSubheading",
feedbackSubheading: "feedbackSubheading" as TranslationKey,
});
mocked(SettingsStore).getValue.mockReturnValue(true);
mocked(shouldShowFeedback).mockReturnValue(true);
@ -53,9 +54,9 @@ describe("<BetaCard />", () => {
it("should not show feedback prompt if label is unset", () => {
mocked(SettingsStore).getBetaInfo.mockReturnValue({
title: "title",
title: "title" as TranslationKey,
caption: () => "caption",
feedbackSubheading: "feedbackSubheading",
feedbackSubheading: "feedbackSubheading" as TranslationKey,
});
render(<BetaCard featureId={featureId} />);
expect(screen.queryByText("Feedback")).toBeFalsy();
@ -63,7 +64,7 @@ describe("<BetaCard />", () => {
it("should not show feedback prompt if subheading is unset", () => {
mocked(SettingsStore).getBetaInfo.mockReturnValue({
title: "title",
title: "title" as TranslationKey,
caption: () => "caption",
feedbackLabel: "feedbackLabel",
});

View File

@ -19,7 +19,7 @@ import { fireEvent, render, screen } from "@testing-library/react";
import { IThreepid, ThreepidMedium } from "matrix-js-sdk/src/@types/threepids";
import { IRequestTokenResponse, MatrixError } from "matrix-js-sdk/src/matrix";
import { UserFriendlyError } from "../../../../../src/languageHandler";
import { TranslationKey, UserFriendlyError } from "../../../../../src/languageHandler";
import EmailAddresses, { EmailAddress } from "../../../../../src/components/views/settings/discovery/EmailAddresses";
import { clearAllModals, getMockClientWithEventEmitter } from "../../../../test-utils";
@ -119,7 +119,7 @@ describe("<EmailAddress/>", () => {
});
it("Shows error dialog when share completion fails (UserFriendlyError)", async () => {
const fakeErrorText = "Fake UserFriendlyError error in test";
const fakeErrorText = "Fake UserFriendlyError error in test" as TranslationKey;
mockClient.bindThreePid.mockRejectedValue(new UserFriendlyError(fakeErrorText));
fireEvent.click(screen.getByText("Complete"));

View File

@ -30,6 +30,7 @@ import {
substitute,
TranslatedString,
UserFriendlyError,
TranslationKey,
} from "../src/languageHandler";
import { stubClient } from "./test-utils";
import { setupLanguageMock } from "./setup/setupLanguage";
@ -61,9 +62,9 @@ describe("languageHandler", () => {
});
it("should support overriding translations", async () => {
const str = "This is a test string that does not exist in the app.";
const enOverride = "This is the English version of a custom string.";
const deOverride = "This is the German version of a custom string.";
const str = "This is a test string that does not exist in the app." as TranslationKey;
const enOverride = "This is the English version of a custom string." as TranslationKey;
const deOverride = "This is the German version of a custom string." as TranslationKey;
// First test that overrides aren't being used
await setLanguage("en");
@ -87,7 +88,7 @@ describe("languageHandler", () => {
});
describe("UserFriendlyError", () => {
const testErrorMessage = "This email address is already in use (%(email)s)";
const testErrorMessage = "This email address is already in use (%(email)s)" as TranslationKey;
beforeEach(async () => {
// Setup some strings with variable substituations that we can use in the tests.
const deOverride = "Diese E-Mail-Adresse wird bereits verwendet (%(email)s)";
@ -128,7 +129,7 @@ describe("languageHandler", () => {
});
it("ok to omit the substitution variables and cause object, there just won't be any cause", async () => {
const friendlyError = new UserFriendlyError("foo error");
const friendlyError = new UserFriendlyError("foo error" as TranslationKey);
expect(friendlyError.cause).toBeUndefined();
});
});
@ -171,12 +172,18 @@ describe("languageHandler", () => {
describe("languageHandler JSX", function () {
// See setupLanguage.ts for how we are stubbing out translations to provide fixture data for these tests
const basicString = "Rooms";
const selfClosingTagSub = "Accept <policyLink /> to continue:";
const textInTagSub = "<a>Upgrade</a> to your own domain";
const selfClosingTagSub = "Accept <policyLink /> to continue:" as TranslationKey;
const textInTagSub = "<a>Upgrade</a> to your own domain" as TranslationKey;
const plurals = "and %(count)s others...";
const variableSub = "You are now ignoring %(userId)s";
type TestCase = [string, string, Record<string, unknown>, Record<string, unknown> | undefined, TranslatedString];
type TestCase = [
string,
TranslationKey,
Record<string, unknown>,
Record<string, unknown> | undefined,
TranslatedString,
];
const testCasesEn: TestCase[] = [
// description of the test case, translationString, variables, tags, expected result
["translates a basic string", basicString, {}, undefined, "Rooms"],
@ -253,7 +260,7 @@ describe("languageHandler JSX", function () {
});
it("replacements in the wrong order", function () {
const text = "%(var1)s %(var2)s";
const text = "%(var1)s %(var2)s" as TranslationKey;
expect(_t(text, { var2: "val2", var1: "val1" })).toBe("val1 val2");
});
@ -354,12 +361,12 @@ describe("languageHandler JSX", function () {
describe("when languages dont load", () => {
it("_t", () => {
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary";
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary" as TranslationKey;
expect(_t(STRING_NOT_IN_THE_DICTIONARY, {})).toEqual(STRING_NOT_IN_THE_DICTIONARY);
});
it("_tDom", () => {
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary";
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary" as TranslationKey;
expect(_tDom(STRING_NOT_IN_THE_DICTIONARY, {})).toEqual(
<span lang="en">{STRING_NOT_IN_THE_DICTIONARY}</span>,
);

View File

@ -23,6 +23,7 @@ import { LabGroup, SETTINGS } from "../../../src/settings/Settings";
import { stubClient } from "../../test-utils";
import { WatchManager } from "../../../src/settings/WatchManager";
import MatrixClientBackedController from "../../../src/settings/controllers/MatrixClientBackedController";
import { TranslationKey } from "../../../src/languageHandler";
describe("ServerSupportUnstableFeatureController", () => {
const watchers = new WatchManager();
@ -35,7 +36,7 @@ describe("ServerSupportUnstableFeatureController", () => {
SETTINGS[setting] = {
isFeature: true,
labsGroup: LabGroup.Messaging,
displayName: "name of some kind",
displayName: "name of some kind" as TranslationKey,
supportedLevels: [SettingLevel.DEVICE, SettingLevel.CONFIG],
default: false,
controller,

View File

@ -87,6 +87,20 @@ describe("AutoDiscoveryUtils", () => {
expect(logger.error).toHaveBeenCalled();
});
it("throws an error when homeserver config has fail error and recognised error string", () => {
const discoveryResult = {
...validIsConfig,
"m.homeserver": {
state: AutoDiscoveryAction.FAIL_ERROR,
error: AutoDiscovery.ERROR_INVALID_HOMESERVER,
},
};
expect(() => AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult)).toThrow(
"Unexpected error resolving homeserver configuration",
);
expect(logger.error).toHaveBeenCalled();
});
it("throws an error with fallback message identity server config has fail error", () => {
const discoveryResult = {
...validHsConfig,
@ -249,4 +263,12 @@ describe("AutoDiscoveryUtils", () => {
);
});
});
describe("authComponentStateForError", () => {
const error = new Error("TEST");
it("should return expected error for the registration page", () => {
expect(AutoDiscoveryUtils.authComponentStateForError(error, "register")).toMatchSnapshot();
});
});
});

View File

@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`AutoDiscoveryUtils authComponentStateForError should return expected error for the registration page 1`] = `
{
"serverDeadError": <div>
<strong>
Your Element is misconfigured
</strong>
<div>
<span>
Ask your Element admin to check
<a
href="https://github.com/vector-im/element-web/blob/master/docs/config.md"
rel="noreferrer noopener"
target="_blank"
>
your config
</a>
for incorrect or duplicate entries.
</span>
</div>
</div>,
"serverErrorIsFatal": true,
"serverIsAlive": false,
}
`;