[test] add test for i18n currentLocale

pull/595/head
LouisLam 3 years ago
parent 11abc1f1e0
commit 01812cc446

@ -0,0 +1,7 @@
const config = {};
if (process.env.TEST_FRONTEND) {
config.presets = ["@babel/preset-env"];
}
module.exports = config;

@ -0,0 +1,5 @@
module.exports = {
"rootDir": ".",
"testRegex": "./test/frontend.spec.js",
};

@ -4,12 +4,8 @@ module.exports = {
"globals": { "globals": {
"__DEV__": true "__DEV__": true
}, },
"testRegex": "./test/*.spec.js", "testRegex": "./test/e2e.spec.js",
"rootDir": ".", "rootDir": ".",
"testTimeout": 30000, "testTimeout": 30000,
"transform": {
"^.+\\.js$": "babel-jest",
".+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "jest-transform-stub"
}
}; };

17873
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -22,7 +22,8 @@
"build": "vite build", "build": "vite build",
"test": "node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/ --test", "test": "node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/ --test",
"test-with-build": "npm run build && npm test", "test-with-build": "npm run build && npm test",
"jest": "node test/prepare-jest.js && jest", "jest": "node test/prepare-jest.js && npm run jest-frontend && jest ",
"jest-frontend": "cross-env TEST_FRONTEND=1 jest --config=./jest-frontend.config.js",
"tsc": "tsc", "tsc": "tsc",
"vite-preview-dist": "vite preview --host", "vite-preview-dist": "vite preview --host",
"build-docker": "npm run build-docker-debian && npm run build-docker-alpine", "build-docker": "npm run build-docker-debian && npm run build-docker-alpine",
@ -98,6 +99,7 @@
}, },
"devDependencies": { "devDependencies": {
"@babel/eslint-parser": "~7.15.7", "@babel/eslint-parser": "~7.15.7",
"@babel/preset-env": "^7.15.8",
"@types/bootstrap": "~5.1.6", "@types/bootstrap": "~5.1.6",
"@vitejs/plugin-legacy": "~1.6.1", "@vitejs/plugin-legacy": "~1.6.1",
"@vitejs/plugin-vue": "~1.9.2", "@vitejs/plugin-vue": "~1.9.2",

@ -2,7 +2,6 @@
const { Page, Browser } = require("puppeteer"); const { Page, Browser } = require("puppeteer");
const { sleep } = require("../src/util"); const { sleep } = require("../src/util");
const axios = require("axios"); const axios = require("axios");
const { currentLocale } = require("../src/i18n");
/** /**
* Set back the correct data type for page object * Set back the correct data type for page object
@ -37,7 +36,7 @@ describe("Init", () => {
}); });
it(`should be titled "${title}"`, async () => { it(`should be titled "${title}"`, async () => {
await expect(page.title()).resolves.toMatch(title); await expect(page.title()).resolves.toEqual(title);
}); });
// Setup Page // Setup Page
@ -76,11 +75,11 @@ describe("Init", () => {
await page.select("#language", "zh-HK"); await page.select("#language", "zh-HK");
let languageTitle = await page.evaluate(() => document.querySelector("[for=language]").innerText); let languageTitle = await page.evaluate(() => document.querySelector("[for=language]").innerText);
expect(languageTitle).toMatch("語言"); expect(languageTitle).toEqual("語言");
await page.select("#language", "en"); await page.select("#language", "en");
languageTitle = await page.evaluate(() => document.querySelector("[for=language]").innerText); languageTitle = await page.evaluate(() => document.querySelector("[for=language]").innerText);
expect(languageTitle).toMatch("Language"); expect(languageTitle).toEqual("Language");
}); });
it("Change Theme", async () => { it("Change Theme", async () => {
@ -104,21 +103,21 @@ describe("Init", () => {
it("Search Engine Visibility", async () => { it("Search Engine Visibility", async () => {
// Default // Default
let res = await axios.get(baseURL + "/robots.txt"); let res = await axios.get(baseURL + "/robots.txt");
expect(res.data).toMatch("Disallow: /"); expect(res.data).toContain("Disallow: /");
// Yes // Yes
await click(page, "#searchEngineIndexYes"); await click(page, "#searchEngineIndexYes");
await click(page, "form > div > .btn[type=submit]"); await click(page, "form > div > .btn[type=submit]");
await sleep(2000); await sleep(2000);
res = await axios.get(baseURL + "/robots.txt"); res = await axios.get(baseURL + "/robots.txt");
expect(res.data).not.toMatch("Disallow: /"); expect(res.data).not.toContain("Disallow: /");
// No // No
await click(page, "#searchEngineIndexNo"); await click(page, "#searchEngineIndexNo");
await click(page, "form > div > .btn[type=submit]"); await click(page, "form > div > .btn[type=submit]");
await sleep(2000); await sleep(2000);
res = await axios.get(baseURL + "/robots.txt"); res = await axios.get(baseURL + "/robots.txt");
expect(res.data).toMatch("Disallow: /"); expect(res.data).toContain("Disallow: /");
}); });
it("Entry Page", async () => { it("Entry Page", async () => {
@ -219,19 +218,11 @@ describe("Init", () => {
await page.goto(baseURL + "/status"); await page.goto(baseURL + "/status");
}); });
it(`should be titled "${title}"`, async () => { it(`should be titled "${title}"`, async () => {
await expect(page.title()).resolves.toMatch(title); await expect(page.title()).resolves.toEqual(title);
}); });
}); });
}); });
describe("Test i18n.js", () => {
it("currentLocale()", () => {
expect(currentLocale()).toMatch("");
});
});
async function login(username, password) { async function login(username, password) {
await input(page, "#floatingInput", username); await input(page, "#floatingInput", username);
await input(page, "#floatingPassword", password); await input(page, "#floatingPassword", password);

@ -0,0 +1,42 @@
// eslint-disable-next-line no-global-assign
global.localStorage = {};
global.navigator = {
language: "en"
};
const { currentLocale } = require("../src/i18n");
describe("Test i18n.js", () => {
it("currentLocale()", () => {
expect(currentLocale()).toEqual("en");
navigator.language = "zh-HK";
expect(currentLocale()).toEqual("zh-HK");
// Note that in Safari on iOS prior to 10.2, the country code returned is lowercase: "en-us", "fr-fr" etc.
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language
navigator.language = "zh-hk";
expect(currentLocale()).toEqual("en");
navigator.language = "en-US";
expect(currentLocale()).toEqual("en");
navigator.language = "ja-ZZ";
expect(currentLocale()).toEqual("ja");
navigator.language = "zz";
expect(currentLocale()).toEqual("en");
navigator.language = "zz-ZZ";
expect(currentLocale()).toEqual("en");
localStorage.locale = "en";
expect(currentLocale()).toEqual("en");
localStorage.locale = "zh-HK";
expect(currentLocale()).toEqual("zh-HK");
});
});
Loading…
Cancel
Save