Skip to content

Commit

Permalink
test: add i18n test
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Sep 4, 2024
1 parent 7abffa0 commit b0a2154
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
12 changes: 12 additions & 0 deletions jest/setupFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import "react-native-gesture-handler/jestSetup";

import mockClipboard from "@react-native-clipboard/clipboard/jest/clipboard-mock.js";
import mockBackHandler from "react-native/Libraries/Utilities/__mocks__/BackHandler.js";

// include this section and the NativeAnimatedHelper section for mocking react-native-reanimated
jest.mock("react-native-reanimated", () => {
Expand Down Expand Up @@ -50,6 +51,17 @@ jest.mock("react-i18next", () => ({
},
}));

jest.mock("i18next", () => ({
changeLanguage: jest.fn(),
use: jest.fn().mockReturnThis(), // Add this line to mock the use method
init: jest.fn().mockReturnThis(), // Add this line to mock the init method
}));

jest.mock(
"react-native/Libraries/Utilities/BackHandler",
() => mockBackHandler
);

jest.mock("react-native-responsive-fontsize", () => ({
RFValue: jest.fn().mockImplementation(() => 10),
RFPercentage: jest.fn().mockImplementation(() => 10),
Expand Down
124 changes: 124 additions & 0 deletions src/utils/__tests__/translations/i18n.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import AsyncStorage from "@react-native-async-storage/async-storage";

import i18n from "../../translations/i18n";

describe("i18n", () => {
beforeEach(() => {
jest.clearAllMocks();
AsyncStorage.clear();
});

afterEach(() => {
jest.resetAllMocks();
});
it("should throw an error if no locales are found", () => {
jest.resetModules();

jest.mock("react-native-localize", () => ({
getLocales: jest.fn(),
}));

expect(() => {
require("../../translations/i18n");
}).toThrow("No locales found");
});

const resources = {
en: {},
ru: {},
ptBR: {},
sk: {},
sl: {},
srLatn: {},
sr: {},
sv: {},
sw: {},
th: {},
tr: {},
uk: {},
ur: {},
vi: {},
zh: {},
zhHans: {},
zhHant: {},
klingon: {},
};

it("should return languageWithoutHyphen if it exists in resources", () => {
const languageWithoutHyphen = "ptBR";

const languageCode = "pt";

const result = Object.keys(resources).includes(languageWithoutHyphen)
? languageWithoutHyphen
: languageCode !== null && Object.keys(resources).includes(languageCode)
? languageCode
: "en";

expect(result).toBe(languageWithoutHyphen);
});

it('should return "en" if languageWithoutHyphen does not exist in resources and languageCode is null', () => {
const languageWithoutHyphen = "nonexistent";
const languageCode = null;

const result = Object.keys(resources).includes(languageWithoutHyphen)
? languageWithoutHyphen
: languageCode !== null && Object.keys(resources).includes(languageCode)
? languageCode
: "en";

expect(result).toBe("en");
});

it("should return languageCode if languageWithoutHyphen does not exist in resources but languageCode does", () => {
const languageWithoutHyphen = "nonexistent";

const languageCode = "ru";

const result = Object.keys(resources).includes(languageWithoutHyphen)
? languageWithoutHyphen
: languageCode !== null && Object.keys(resources).includes(languageCode)
? languageCode
: "en";

expect(result).toBe(languageCode);
});

it("should change language to stored language", async () => {
jest.mock("react-native-localize", () => ({
getLocales: () => [
{ languageCode: "en", languageTag: "en-US", countryCode: "US" },
],
}));

const storedLanguageMock = "es"; // idioma armazenado simulado
(AsyncStorage.getItem as jest.Mock).mockResolvedValue(storedLanguageMock);

await (async () => {
const storedLanguage = await AsyncStorage.getItem("selectedLanguage");
storedLanguage && i18n.changeLanguage(storedLanguage);
})();

expect(AsyncStorage.getItem).toHaveBeenCalledWith("selectedLanguage");
expect(i18n.changeLanguage).toHaveBeenCalledWith(storedLanguageMock);
});

it("should not change language if no stored language", async () => {
jest.mock("react-native-localize", () => ({
getLocales: () => [
{ languageCode: "en", languageTag: "en-US", countryCode: "US" },
],
}));

(AsyncStorage.getItem as jest.Mock).mockResolvedValue(null);

await (async () => {
const storedLanguage = await AsyncStorage.getItem("selectedLanguage");
storedLanguage && i18n.changeLanguage(storedLanguage);
})();

expect(AsyncStorage.getItem).toHaveBeenCalledWith("selectedLanguage");
expect(i18n.changeLanguage).not.toHaveBeenCalled();
});
});
2 changes: 1 addition & 1 deletion src/utils/translations/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const resources = {

const lngChecked = Object.keys(resources).includes(languageWithoutHyphen)
? languageWithoutHyphen
: Object.keys(resources).includes(languageCode)
: languageCode !== null && Object.keys(resources).includes(languageCode)
? languageCode
: "en";

Expand Down

0 comments on commit b0a2154

Please sign in to comment.