Collator.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. describe("errors", () => {
  2. test("structurally invalid tag", () => {
  3. expect(() => {
  4. new Intl.Collator("root");
  5. }).toThrowWithMessage(RangeError, "root is not a structurally valid language tag");
  6. expect(() => {
  7. new Intl.Collator("en-");
  8. }).toThrowWithMessage(RangeError, "en- is not a structurally valid language tag");
  9. expect(() => {
  10. new Intl.Collator("Latn");
  11. }).toThrowWithMessage(RangeError, "Latn is not a structurally valid language tag");
  12. expect(() => {
  13. new Intl.Collator("en-u-aa-U-aa");
  14. }).toThrowWithMessage(RangeError, "en-u-aa-U-aa is not a structurally valid language tag");
  15. });
  16. test("options is an invalid type", () => {
  17. expect(() => {
  18. new Intl.Collator("en", null);
  19. }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
  20. });
  21. test("localeMatcher option is invalid ", () => {
  22. expect(() => {
  23. new Intl.Collator("en", { localeMatcher: "hello!" });
  24. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option localeMatcher");
  25. });
  26. test("usage option is invalid ", () => {
  27. expect(() => {
  28. new Intl.Collator("en", { usage: "hello!" });
  29. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option usage");
  30. });
  31. test("collation option is invalid ", () => {
  32. expect(() => {
  33. new Intl.Collator("en", { collation: "hello!" });
  34. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option collation");
  35. });
  36. test("caseFirst option is invalid ", () => {
  37. expect(() => {
  38. new Intl.Collator("en", { caseFirst: "hello!" });
  39. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option caseFirst");
  40. });
  41. test("sensitivity option is invalid ", () => {
  42. expect(() => {
  43. new Intl.Collator("en", { sensitivity: "hello!" });
  44. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option sensitivity");
  45. });
  46. });
  47. describe("normal behavior", () => {
  48. test("length is 0", () => {
  49. expect(Intl.Collator).toHaveLength(0);
  50. });
  51. test("all valid usage options", () => {
  52. ["sort", "search"].forEach(usage => {
  53. expect(() => {
  54. new Intl.Collator("en", { usage: usage });
  55. }).not.toThrow();
  56. });
  57. });
  58. test("all valid localeMatcher options", () => {
  59. ["lookup", "best fit"].forEach(localeMatcher => {
  60. expect(() => {
  61. new Intl.Collator("en", { localeMatcher: localeMatcher });
  62. }).not.toThrow();
  63. });
  64. });
  65. test("valid collation options", () => {
  66. ["default", "compat"].forEach(collation => {
  67. expect(() => {
  68. new Intl.Collator("en", { collation: collation });
  69. }).not.toThrow();
  70. });
  71. });
  72. test("valid caseFirst options", () => {
  73. ["upper", "lower", "false"].forEach(caseFirst => {
  74. expect(() => {
  75. new Intl.Collator("en", { caseFirst: caseFirst });
  76. }).not.toThrow();
  77. });
  78. });
  79. test("valid sensitivity options", () => {
  80. ["base", "accent", "case", "variant"].forEach(sensitivity => {
  81. expect(() => {
  82. new Intl.Collator("en", { sensitivity: sensitivity });
  83. }).not.toThrow();
  84. });
  85. });
  86. });