From cc044e9a645bfb6b4708fe30b9bc0492dd5bf60a Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Sat, 13 Apr 2019 20:07:31 -0700 Subject: [PATCH] Add i18n test Now the util folder has 100% coverage. Signed-off-by: Mcat12 --- src/util/__tests__/i18n.test.tsx | 29 +++++++++++++++++++++++++++++ src/util/i18n.tsx | 12 +++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/util/__tests__/i18n.test.tsx diff --git a/src/util/__tests__/i18n.test.tsx b/src/util/__tests__/i18n.test.tsx new file mode 100644 index 0000000..64539a1 --- /dev/null +++ b/src/util/__tests__/i18n.test.tsx @@ -0,0 +1,29 @@ +/* Pi-hole: A black hole for Internet advertisements + * (c) 2019 Pi-hole, LLC (https://pi-hole.net) + * Network-wide ad blocking via your own hardware. + * + * Web Interface + * Test internationalization setup + * + * This file is copyright under the latest version of the EUPL. + * Please see LICENSE file for your rights under this license. */ + +import { setupI18n } from "../i18n"; + +// Mock react-i18next, as it does not properly export the i18next module during +// testing. +// https://github.com/i18next/react-i18next/issues/434 +jest.mock("react-i18next", () => ({ + reactI18nextModule: { + type: "3rdParty", + init: () => {} + } +})); + +it("configures i18n successfully", async () => { + // Provide a mock ajax function to the XHR backend + const fakeAjax = (url: any, options: any, callback: any) => callback("", {}); + + // Make sure i18n initializes without error + await setupI18n(fakeAjax); +}); diff --git a/src/util/i18n.tsx b/src/util/i18n.tsx index 16e0ffa..89cf31c 100644 --- a/src/util/i18n.tsx +++ b/src/util/i18n.tsx @@ -15,8 +15,13 @@ import { reactI18nextModule } from "react-i18next"; import config from "../config"; import languages from "../languages.json"; -export function setupI18n() { - i18n +/** + * Set up the internationalization service + * + * @param ajax An optional ajax function to use when fetching translations + */ +export function setupI18n(ajax?: any) { + return i18n .use(XHR) .use(LanguageDetector) .use(reactI18nextModule) @@ -46,7 +51,8 @@ export function setupI18n() { escapeValue: false }, backend: { - loadPath: process.env.PUBLIC_URL + "/i18n/{{lng}}/{{ns}}.json" + loadPath: process.env.PUBLIC_URL + "/i18n/{{lng}}/{{ns}}.json", + ajax }, react: { // Wait until translations are loaded before rendering