Skip to content

Commit 1df0107

Browse files
authored
chore(www) Add unit tests for i18n utils (#21941)
* Add unit tests for i18n utils * get rid of unused import
1 parent 095aae9 commit 1df0107

File tree

3 files changed

+96
-16
lines changed

3 files changed

+96
-16
lines changed

www/src/utils/__tests__/i18n.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { localizedPath, getLanguages, getLocaleAndBasePath } from "../i18n"
2+
3+
describe("getLanguages", () => {
4+
it("returns an empty list when no languages are passed in", () => {
5+
expect(getLanguages("")).toEqual([])
6+
})
7+
8+
it("returns a list of languages", () => {
9+
expect(
10+
getLanguages("es ja id pl pt-BR zh-Hans").map(lang => lang.code)
11+
).toEqual(["es", "ja", "id", "pl", "pt-BR", "zh-Hans"])
12+
})
13+
14+
it("throws an error if the default language is defined", () => {
15+
expect(() => getLanguages("en ja")).toThrow(/default locale/i)
16+
})
17+
18+
it("throws an error if an invalid locale is defined and references the languages file", () => {
19+
expect(() => getLanguages("simlish")).toThrow(/invalid locale/i)
20+
expect(() => getLanguages("simlish")).toThrow("i18n.json")
21+
})
22+
})
23+
24+
describe("localizedPath", () => {
25+
it("returns the path when locale === defaultLang", () => {
26+
expect(localizedPath("en", "/")).toEqual("/")
27+
expect(localizedPath("en", "/docs/")).toEqual("/docs/")
28+
expect(localizedPath("en", "/docs/quick-start/")).toEqual(
29+
"/docs/quick-start/"
30+
)
31+
})
32+
33+
it("returns a path prefixed with a locale for other languages", () => {
34+
expect(localizedPath("es", "/")).toEqual("/es/")
35+
expect(localizedPath("es", "/docs/")).toEqual("/es/docs/")
36+
expect(localizedPath("es", "/docs/quick-start/")).toEqual(
37+
"/es/docs/quick-start/"
38+
)
39+
})
40+
41+
it("returns the path when a locale is already in the path", () => {
42+
expect(localizedPath("es", "/es/docs/")).toEqual("/es/docs/")
43+
})
44+
})
45+
46+
describe("getLocaleAndBasePath", () => {
47+
const codes = ["es", "ja"]
48+
it("returns the locale and base path when passed a path with a locale", () => {
49+
expect(getLocaleAndBasePath("/es/", codes)).toEqual({
50+
locale: "es",
51+
basePath: "/",
52+
})
53+
expect(getLocaleAndBasePath("/es/docs/", codes)).toEqual({
54+
locale: "es",
55+
basePath: "/docs/",
56+
})
57+
expect(getLocaleAndBasePath("/es/docs/quick-start/", codes)).toEqual({
58+
locale: "es",
59+
basePath: "/docs/quick-start/",
60+
})
61+
})
62+
63+
it("returns defaultLang when passed a path with no locale", () => {
64+
expect(getLocaleAndBasePath("/", codes)).toEqual({
65+
locale: "en",
66+
basePath: "/",
67+
})
68+
expect(getLocaleAndBasePath("/docs/", codes)).toEqual({
69+
locale: "en",
70+
basePath: "/docs/",
71+
})
72+
expect(getLocaleAndBasePath("/docs/quick-start/", codes)).toEqual({
73+
locale: "en",
74+
basePath: "/docs/quick-start/",
75+
})
76+
})
77+
})

www/src/utils/i18n.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ const defaultLang = "en"
44
// Only allow languages defined in the LOCALES env variable.
55
// This allows us to compile only languages that are "complete" or test only
66
// a single language
7-
function getLanguages() {
7+
function getLanguages(localeStr) {
88
// If `LOCALES` isn't defined, only have default language (English)
9-
if (!process.env.LOCALES) {
9+
if (!localeStr) {
1010
return []
1111
}
1212

13-
const langCodes = process.env.LOCALES.split(" ")
13+
const langCodes = localeStr.split(" ")
1414
const langs = []
1515
for (let code of langCodes) {
16+
if (code === defaultLang) {
17+
throw new Error(
18+
`${code} is the default locale and should not be put in the locale list.`
19+
)
20+
}
1621
const lang = allLangs.find(lang => lang.code === code)
1722
// Error if one of the locales provided isn't a valid locale
1823
if (!lang) {
@@ -25,7 +30,7 @@ function getLanguages() {
2530
return langs
2631
}
2732

28-
const langs = getLanguages()
33+
const langs = getLanguages(process.env.LOCALES)
2934
const langCodes = langs.map(lang => lang.code)
3035

3136
function isDefaultLang(locale) {
@@ -34,12 +39,10 @@ function isDefaultLang(locale) {
3439

3540
/**
3641
* Get the path prefixed with the locale
37-
* @param {*} locale the locale to prefix with
38-
* @param {*} path the path to prefix
42+
* @param {string} locale the locale to prefix with
43+
* @param {string} path the path to prefix
3944
*/
4045
function localizedPath(locale, path) {
41-
const isIndex = path === `/`
42-
4346
// Our default language isn't prefixed for back-compat
4447
if (isDefaultLang(locale)) {
4548
return path
@@ -50,25 +53,25 @@ function localizedPath(locale, path) {
5053
// If for whatever reason we receive an already localized path
5154
// (e.g. if the path was made with location.pathname)
5255
// just return it as-is.
53-
if (langCodes.includes(base)) {
56+
if (base === locale) {
5457
return path
5558
}
5659

57-
// If it's another language, add the "path"
58-
// However, if the homepage/index page is linked don't add the "to"
59-
// Because otherwise this would add a trailing slash
60-
return `${locale}${isIndex ? `` : `${path}`}`
60+
// If it's another language, prefix with the locale
61+
return `/${locale}${path}`
6162
}
6263

6364
/**
6465
* Split a path into its locale and base path.
6566
*
6667
* e.g. /es/tutorial/ -> { locale: "es", basePath: "/tutorial/"}
6768
* @param {string} path the path to extract locale information from
69+
* @param {Array} langCodes a list of valid language codes, defaulting to
70+
* the ones defined in `process.env.LOCALES`
6871
*/
69-
function getLocaleAndBasePath(path) {
72+
function getLocaleAndBasePath(path, codes = langCodes) {
7073
const [, code, ...rest] = path.split("/")
71-
if (langCodes.includes(code)) {
74+
if (codes.includes(code)) {
7275
return { locale: code, basePath: `/${rest.join("/")}` }
7376
}
7477
return { locale: defaultLang, basePath: path }
@@ -78,6 +81,7 @@ module.exports = {
7881
langCodes,
7982
langs,
8083
defaultLang,
84+
getLanguages,
8185
localizedPath,
8286
getLocaleAndBasePath,
8387
}

www/src/utils/node/docs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const url = require(`url`)
66
const moment = require(`moment`)
77
const { langCodes } = require(`../i18n`)
88
const { getPrevAndNext } = require(`../get-prev-and-next.js`)
9-
const { localizedPath } = require(`../i18n.js`)
109

1110
// convert a string like `/some/long/path/name-of-docs/` to `name-of-docs`
1211
const slugToAnchor = slug =>

0 commit comments

Comments
 (0)