From 4938295e758004d86584fe8d048032cc2aad3af7 Mon Sep 17 00:00:00 2001 From: Adam Antal Date: Thu, 14 Dec 2023 15:26:13 +0100 Subject: [PATCH] Throw an error if a text string isn't defined with description of string --- src/core/utils/text.tsx | 8 +++++++- src/tests/unit/text.test.tsx | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/core/utils/text.tsx b/src/core/utils/text.tsx index 569e8c84b6..fb7e8bc57d 100644 --- a/src/core/utils/text.tsx +++ b/src/core/utils/text.tsx @@ -108,8 +108,14 @@ export const useText = (): UseTextFunction => { const { data } = useSelector((state: RootState) => state.text); return (key: string, { placeholders, count } = { count: 0 }) => { + if (!data) { + throw new Error(`The translation store is broken.`); + } + if (!data[key]) { + throw new Error(`The translation for ${key} is not defined.`); + } const textDefinition = constructTextDefinitionFromRawTextTextEntry( - data?.[key] ?? key + data[key] ); const textPlaceholders = { ...(placeholders ?? {}) }; diff --git a/src/tests/unit/text.test.tsx b/src/tests/unit/text.test.tsx index 445dd40b5b..af0768d4e9 100644 --- a/src/tests/unit/text.test.tsx +++ b/src/tests/unit/text.test.tsx @@ -76,3 +76,15 @@ test("Should handle plural text definitions", () => { ); }); }); + +test("Should throw an error if the text definition is not found", () => { + const { result } = renderHook(() => useText(), { wrapper: Wrapper }); + // We name it t, because that is how we normally use it in the code. + const t = result.current; + + act(() => { + expect(() => t("nonExistingText")).toThrowError( + "The translation for nonExistingText is not defined." + ); + }); +});