Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/translation error throwing #828

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/core/utils/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ const processTexts = (texts: string[], placeholders: Placeholders) =>
);

export const useText = (): UseTextFunction => {
const { data } = useSelector((state: RootState) => state.text);
const { data } = useSelector<{ data: Record<string, string> }>(
(state: RootState) => state.text
);

return (key: string, { placeholders, count } = { count: 0 }) => {
if (!data) {
throw new Error(`The translation store is broken.`);
}
if (!data[key]) {
if (data[key] === undefined) {
throw new Error(`The translation for ${key} is not defined.`);
}
const textDefinition = constructTextDefinitionFromRawTextTextEntry(
Expand Down
15 changes: 14 additions & 1 deletion src/tests/unit/text.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const store = configureStore({
data: {
simpleText: "This is a @simple test",
placeholdersText: `{"type":"simple","text":["This is a text with a @placeholder embedded. Does it work? @result it does!"]}`,
pluralText: `{"type":"plural","text":["You have 1 material on the waiting list","You have @count materials on the waiting list"]}`
pluralText: `{"type":"plural","text":["You have 1 material on the waiting list","You have @count materials on the waiting list"]}`,
emptyStringText: ""
}
}
}
Expand Down Expand Up @@ -88,3 +89,15 @@ test("Should throw an error if the text definition is not found", () => {
);
});
});

test("Shouldn't throw an error if the text is an empty string", () => {
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(() => {
// This test would be failing if an error was thrown for empty strings.
// That's why we don't need to define anything else here to know that it works.
t("emptyStringText");
});
});
Loading