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." + ); + }); +});