DisplayNames.supportedLocalesOf.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. describe("correct behavior", () => {
  2. test("length is 1", () => {
  3. expect(Intl.DisplayNames.supportedLocalesOf).toHaveLength(1);
  4. });
  5. test("basic functionality", () => {
  6. // prettier-ignore
  7. const values = [
  8. [[], []],
  9. [undefined, []],
  10. ["en", ["en"]],
  11. [new Intl.Locale("en"), ["en"]],
  12. [["en"], ["en"]],
  13. [["en", "en-gb", "en-us"], ["en", "en-GB", "en-US"]],
  14. [["en", "de", "fr"], ["en", "de", "fr"]],
  15. [["en-foobar"], ["en-foobar"]],
  16. [["en-foobar-u-abc"], ["en-foobar-u-abc"]],
  17. [["aa", "zz"], []],
  18. [["en", "aa", "zz"], ["en"]],
  19. ];
  20. for (const [input, expected] of values) {
  21. expect(Intl.DisplayNames.supportedLocalesOf(input)).toEqual(expected);
  22. // "best fit" (implementation defined) just uses the same implementation as "lookup" at the moment
  23. expect(
  24. Intl.DisplayNames.supportedLocalesOf(input, { localeMatcher: "best fit" })
  25. ).toEqual(Intl.DisplayNames.supportedLocalesOf(input, { localeMatcher: "lookup" }));
  26. }
  27. });
  28. });
  29. describe("errors", () => {
  30. test("invalid value for localeMatcher option", () => {
  31. expect(() => {
  32. Intl.DisplayNames.supportedLocalesOf([], { localeMatcher: "foo" });
  33. }).toThrowWithMessage(RangeError, "foo is not a valid value for option localeMatcher");
  34. });
  35. test("invalid language tag", () => {
  36. expect(() => {
  37. Intl.DisplayNames.supportedLocalesOf(["aaaaaaaaa"]);
  38. }).toThrowWithMessage(RangeError, "aaaaaaaaa is not a structurally valid language tag");
  39. });
  40. });