misc.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { describe, expect, it } from "vitest";
  2. import { autoPlural, consumeGen, consumeStringGen, fetchAdvanced, getListLength, insertValues, pauseFor, purifyObj } from "./misc.js";
  3. import { randomId } from "./crypto.js";
  4. //#region autoPlural
  5. describe("misc/autoPlural", () => {
  6. it("Tests if autoPlural uses the correct forms", () => {
  7. expect(autoPlural("apple", -1)).toBe("apples");
  8. expect(autoPlural("apple", 0)).toBe("apples");
  9. expect(autoPlural("apple", 1)).toBe("apple");
  10. expect(autoPlural("apple", 2)).toBe("apples");
  11. expect(autoPlural("cherry", -1)).toBe("cherries");
  12. expect(autoPlural("cherry", 0)).toBe("cherries");
  13. expect(autoPlural("cherry", 1)).toBe("cherry");
  14. expect(autoPlural("cherry", 2)).toBe("cherries");
  15. const cont = document.createElement("div");
  16. for(let i = 0; i < 3; i++) {
  17. const span = document.createElement("span");
  18. cont.append(span);
  19. }
  20. expect(autoPlural("cherry", [1])).toBe("cherry");
  21. expect(autoPlural("cherry", cont.querySelectorAll("span"))).toBe("cherries");
  22. expect(autoPlural("cherry", { count: 3 })).toBe("cherries");
  23. });
  24. it("Handles edge cases", () => {
  25. expect(autoPlural("apple", 2, "-ies")).toBe("applies");
  26. expect(autoPlural("cherry", 2, "-s")).toBe("cherrys");
  27. });
  28. });
  29. //#region insertValues
  30. describe("misc/insertValues", () => {
  31. it("Stringifies and inserts values correctly", () => {
  32. expect(insertValues("a:%1,b:%2,c:%3", "A", "B", "C")).toBe("a:A,b:B,c:C");
  33. expect(insertValues("a:%1,b:%2,c:%3", "A", 2, true)).toBe("a:A,b:2,c:true");
  34. expect(insertValues("a:%1,b:%2,c:%3", { toString: () => "[A]" }, {})).toBe("a:[A],b:[object Object],c:%3");
  35. });
  36. });
  37. //#region pauseFor
  38. describe("misc/pauseFor", () => {
  39. it("Pauses for the correct time and can be aborted", async () => {
  40. const startTs = Date.now();
  41. await pauseFor(100);
  42. expect(Date.now() - startTs).toBeGreaterThanOrEqual(80);
  43. const ac = new AbortController();
  44. const startTs2 = Date.now();
  45. setTimeout(() => ac.abort(), 20);
  46. await pauseFor(100, ac.signal);
  47. expect(Date.now() - startTs2).toBeLessThan(100);
  48. });
  49. });
  50. //#region fetchAdvanced
  51. describe("misc/fetchAdvanced", () => {
  52. it("Fetches a resource correctly", async () => {
  53. try {
  54. const res = await fetchAdvanced("https://jsonplaceholder.typicode.com/todos/1");
  55. const json = await res.json();
  56. expect(json?.id).toBe(1);
  57. }
  58. catch(e) {
  59. expect(e).toBeUndefined();
  60. }
  61. });
  62. it("Throws error on invalid resource", async () => {
  63. try {
  64. const res = await fetchAdvanced("invalid-url");
  65. }
  66. catch(e) {
  67. expect(e).toBeInstanceOf(Error);
  68. }
  69. });
  70. });
  71. //#region consumeGen
  72. describe("misc/consumeGen", () => {
  73. it("Consumes a ValueGen properly", async () => {
  74. expect(await consumeGen(() => 1)).toBe(1);
  75. expect(await consumeGen(() => Promise.resolve(1))).toBe(1);
  76. expect(await consumeGen(1)).toBe(1);
  77. expect(await consumeGen(() => true)).toBe(true);
  78. expect(await consumeGen(async () => false)).toBe(false);
  79. // @ts-expect-error
  80. expect(await consumeGen()).toThrow(TypeError);
  81. });
  82. });
  83. //#region consumeStringGen
  84. describe("misc/consumeStringGen", () => {
  85. it("Consumes a StringGen properly", async () => {
  86. expect(await consumeStringGen("a")).toBe("a");
  87. expect(await consumeStringGen(() => "b")).toBe("b");
  88. expect(await consumeStringGen(() => Promise.resolve("c"))).toBe("c");
  89. expect(await consumeStringGen({ toString: () => "d" })).toBe("d");
  90. });
  91. });
  92. //#region getListLength
  93. describe("misc/getListLength", () => {
  94. it("Resolves all types of ListWithLength", () => {
  95. const cont = document.createElement("div");
  96. for(let i = 0; i < 3; i++) {
  97. const span = document.createElement("span");
  98. cont.append(span);
  99. }
  100. expect(getListLength(cont.querySelectorAll("span"))).toBe(3);
  101. expect(getListLength([1, 2, 3])).toBe(3);
  102. expect(getListLength({ length: 3 })).toBe(3);
  103. expect(getListLength({ size: 3 })).toBe(3);
  104. expect(getListLength({ count: 3 })).toBe(3);
  105. // @ts-expect-error
  106. expect(getListLength({}, true)).toBe(0);
  107. // @ts-expect-error
  108. expect(getListLength({}, false)).toBe(NaN);
  109. });
  110. });
  111. //#region purifyObj
  112. describe("misc/purifyObj", () => {
  113. it("Removes the prototype chain of a passed object", () => {
  114. const obj = { a: 1, b: 2 };
  115. const pure = purifyObj(obj);
  116. // @ts-expect-error
  117. expect(obj.__proto__).toBeDefined();
  118. // @ts-expect-error
  119. expect(pure.__proto__).toBeUndefined();
  120. expect(pure.a).toBe(1);
  121. expect(pure.b).toBe(2);
  122. });
  123. });