Skip to content

Commit

Permalink
Throw an error if a text string isn't defined with description of string
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamik10 committed Dec 14, 2023
1 parent 103b4b1 commit 4938295
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/core/utils/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? {}) };
Expand Down
12 changes: 12 additions & 0 deletions src/tests/unit/text.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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."
);
});
});

0 comments on commit 4938295

Please sign in to comment.