misc.spec.ts 4.1 KB

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