Skip to content

Commit cef1690

Browse files
authored
CLDR-18475 st: fe tests for cldrSurvey.setLang (#4571)
1 parent bfc1ec0 commit cef1690

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

tools/cldr-apps/js/src/esm/cldrSurvey.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,17 @@ function locInfo(loc) {
821821
function setLang(node, loc, overridedir) {
822822
var info = locInfo(loc);
823823

824+
function assertValidDir(d) {
825+
if (d != "rtl" && d != "ltr") {
826+
throw Error(`${d} should be either ltr or rtl`);
827+
}
828+
}
829+
824830
if (overridedir) {
831+
assertValidDir(overridedir);
825832
node.dir = overridedir;
826833
} else if (info && info.dir) {
834+
assertValidDir(info.dir);
827835
node.dir = info.dir;
828836
}
829837

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import { expect, assert } from "chai";
2+
3+
import * as cldrLoad from "../src/esm/cldrLoad.mjs";
4+
import * as cldrLocaleMap from "../src/esm/cldrLocaleMap.mjs";
5+
import * as cldrStatus from "../src/esm/cldrStatus.mjs";
6+
import * as cldrSurvey from "../src/esm/cldrSurvey.mjs";
7+
8+
export const TestCldrSurvey = "ok";
9+
10+
/** local function for resetting things before test calls */
11+
function setCldrLoad() {
12+
const flm = {
13+
locales: {
14+
ar: {
15+
bcp47: "ar",
16+
parent: "root",
17+
sub: [
18+
"ar_DZ",
19+
"ar_BH",
20+
"ar_TD",
21+
"ar_KM",
22+
"ar_DJ",
23+
"ar_EG",
24+
"ar_ER",
25+
"ar_IQ",
26+
"ar_IL",
27+
"ar_JO",
28+
"ar_KW",
29+
"ar_LB",
30+
"ar_LY",
31+
"ar_MR",
32+
"ar_MA",
33+
"ar_OM",
34+
"ar_PS",
35+
"ar_QA",
36+
"ar_SA",
37+
"ar_SO",
38+
"ar_SS",
39+
"ar_SD",
40+
"ar_SY",
41+
"ar_TN",
42+
"ar_AE",
43+
"ar_EH",
44+
"ar_YE",
45+
"ar_001",
46+
],
47+
dcChild: "ar_001",
48+
highestParent: "ar",
49+
name: "Arabic",
50+
dir: "rtl",
51+
type: "common",
52+
extended: false,
53+
tc: true,
54+
},
55+
bn: {
56+
bcp47: "bn",
57+
parent: "root",
58+
sub: ["bn_BD", "bn_IN"],
59+
dcChild: "bn_BD",
60+
highestParent: "bn",
61+
name: "Bangla",
62+
type: "common",
63+
dir: "ltr", // we now explicitly set this, even for ltr
64+
extended: false,
65+
tc: true,
66+
},
67+
},
68+
};
69+
70+
cldrLoad.setTheLocaleMap(new cldrLocaleMap.LocaleMap(flm));
71+
}
72+
73+
function unsetCldrLoad() {
74+
cldrLoad.setTheLocaleMap(new cldrLocaleMap.LocaleMap(null));
75+
cldrStatus.setCurrentLocale(null);
76+
}
77+
78+
describe("cldrSurvey.setLang", function () {
79+
before(() => setCldrLoad());
80+
after(() => unsetCldrLoad());
81+
it("should be able to set a node explicitly", () => {
82+
const n = document.createElement("span");
83+
expect(n).to.be.ok;
84+
cldrSurvey.setLang(n, "bn", "rtl");
85+
expect(n.lang).to.equal("bn");
86+
expect(n.dir).to.equal("rtl");
87+
});
88+
it("should be able to set another node explicitly", () => {
89+
const n = document.createElement("span");
90+
expect(n).to.be.ok;
91+
cldrSurvey.setLang(n, "bn", "ltr");
92+
expect(n.lang).to.equal("bn");
93+
expect(n.dir).to.equal("ltr");
94+
});
95+
it("should be able to a node with ar language", () => {
96+
const n = document.createElement("span");
97+
expect(n).to.be.ok;
98+
cldrSurvey.setLang(n, "ar");
99+
expect(n.lang).to.equal("ar");
100+
expect(n.dir).to.equal("rtl");
101+
});
102+
it("should be able to set a node with bn language", () => {
103+
const n = document.createElement("span");
104+
expect(n).to.be.ok;
105+
cldrSurvey.setLang(n, "bn");
106+
expect(n.lang).to.equal("bn");
107+
expect(n.dir).to.equal("ltr");
108+
});
109+
it("should be able to a node with default ar language", () => {
110+
cldrStatus.setCurrentLocale("ar");
111+
const n = document.createElement("span");
112+
expect(n).to.be.ok;
113+
cldrSurvey.setLang(n);
114+
expect(n.lang).to.equal("ar");
115+
expect(n.dir).to.equal("rtl");
116+
});
117+
it("should be able to a node with default bn language", () => {
118+
cldrStatus.setCurrentLocale("bn");
119+
const n = document.createElement("span");
120+
expect(n).to.be.ok;
121+
cldrSurvey.setLang(n);
122+
expect(n.lang).to.equal("bn");
123+
expect(n.dir).to.equal("ltr");
124+
});
125+
it("should be able to a node with override ar language", () => {
126+
cldrStatus.setCurrentLocale("ar");
127+
const n = document.createElement("span");
128+
expect(n).to.be.ok;
129+
cldrSurvey.setLang(n, undefined, "ltr");
130+
expect(n.lang).to.equal("ar");
131+
expect(n.dir).to.equal("ltr");
132+
});
133+
it("should be able to a node with override bn language", () => {
134+
cldrStatus.setCurrentLocale("bn");
135+
const n = document.createElement("span");
136+
expect(n).to.be.ok;
137+
cldrSurvey.setLang(n, undefined, "rtl");
138+
expect(n.lang).to.equal("bn");
139+
expect(n.dir).to.equal("rtl");
140+
});
141+
it("Should fail on a bad override directionality", () => {
142+
cldrStatus.setCurrentLocale("bn");
143+
const n = document.createElement("span");
144+
expect(n).to.be.ok;
145+
assert.throws(() => cldrSurvey.setLang(n, undefined, "RIGHT_TO_LEFT"));
146+
});
147+
});

tools/cldr-apps/js/test/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { TestCldrGui } from "./TestCldrGui.mjs";
1414
import { TestCldrListUsers } from "./TestCldrListUsers.mjs";
1515
import { TestCldrProgress } from "./TestCldrProgress.mjs";
1616
import { TestCldrStatus } from "./TestCldrStatus.mjs";
17+
import { TestCldrSurvey } from "./TestCldrSurvey.mjs";
1718
import { TestCldrTable } from "./TestCldrTable.mjs";
1819
import { TestCldrText } from "./TestCldrText.mjs";
1920

@@ -36,6 +37,7 @@ export default {
3637
TestCldrListUsers,
3738
TestCldrProgress,
3839
TestCldrStatus,
40+
TestCldrSurvey,
3941
TestCldrTable,
4042
TestCldrTest,
4143
TestCldrText,

0 commit comments

Comments
 (0)