functions.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import "../setup/setup";
  2. import "../setup/server.setup";
  3. import { strict as assert } from "assert";
  4. import { HMPLRequestInfo } from "../../src/types";
  5. import { createScope, clearScope } from "../server/server";
  6. import { compile, stringify } from "../../src/main";
  7. import type { ScopeOptions } from "./functions.types";
  8. const e = (text: string, block: () => unknown, message: string) => {
  9. it(text, () => {
  10. assert.throws(block, {
  11. message
  12. });
  13. });
  14. };
  15. const eq = (text: string, block: unknown, equality: any) => {
  16. it(text, () => {
  17. assert.strictEqual(block, equality);
  18. });
  19. };
  20. const aeq = (
  21. template: string,
  22. get: (...args: any[]) => void,
  23. options: any = {},
  24. scopeOptions: ScopeOptions = {}
  25. ) => {
  26. it("", async () => {
  27. const scope = createScope({ ...scopeOptions });
  28. const req = await new Promise((res) => {
  29. compile(template)({
  30. get: (...args) => get(res, ...args),
  31. ...options
  32. });
  33. });
  34. assert.deepEqual(req, true);
  35. clearScope(scope);
  36. });
  37. };
  38. const aeqError = (
  39. template: string,
  40. get: (...args: any[]) => void,
  41. options: any = {},
  42. scopeOptions: ScopeOptions = {}
  43. ) => {
  44. it("should handle template errors correctly", async () => {
  45. const scope = createScope({ ...scopeOptions });
  46. try {
  47. const req = await new Promise<string>((resolve) => {
  48. compile(template)({
  49. get: (...args) => get(resolve, ...args),
  50. ...options
  51. });
  52. });
  53. assert.deepEqual(req, "true");
  54. } catch (e) {
  55. console.error("Error occurred during test:", e);
  56. throw e;
  57. } finally {
  58. clearScope(scope);
  59. }
  60. });
  61. };
  62. const defaultGetEl: (el: Element | null | undefined) => Element | undefined = (
  63. el
  64. ) => el?.getElementsByTagName("button")?.[0];
  65. // async equal event
  66. const aeqe = (
  67. template: string,
  68. get: (...args: any[]) => void,
  69. compileOptions: any = {},
  70. options: any = {},
  71. scopeOptions: ScopeOptions = {},
  72. quantity = 1,
  73. getEl = defaultGetEl,
  74. event: string = "click",
  75. eventOptions: EventInit = {
  76. bubbles: true,
  77. cancelable: true
  78. }
  79. ) => {
  80. it("", async () => {
  81. const scope = createScope({ ...scopeOptions });
  82. const req = await new Promise((res) => {
  83. const instance = compile(
  84. template,
  85. compileOptions
  86. )({
  87. get: (...args) => get(res, ...args),
  88. ...options
  89. });
  90. const el = instance.response;
  91. const currentEl = getEl(el);
  92. if (currentEl) {
  93. for (let i = 0; i < quantity; i++) {
  94. if (currentEl) {
  95. const clickEvent = new window.Event(event, eventOptions);
  96. currentEl.dispatchEvent(clickEvent);
  97. }
  98. }
  99. }
  100. });
  101. assert.deepEqual(req, true);
  102. clearScope(scope);
  103. });
  104. };
  105. const ae = (
  106. template: string,
  107. message: string,
  108. get: (...args: any[]) => void,
  109. options: any = {},
  110. scopeOptions: ScopeOptions = {}
  111. ) => {
  112. it("", async () => {
  113. const scope = createScope({ ...scopeOptions });
  114. assert.throws(
  115. async () => {
  116. await new Promise((res) => {
  117. compile(template)({
  118. get: (...args) => get(res, ...args),
  119. ...options
  120. });
  121. });
  122. },
  123. {
  124. message
  125. }
  126. );
  127. clearScope(scope);
  128. });
  129. };
  130. const createTestObj1 = (obj: Record<string, any>) => {
  131. return `<div>{${stringify(obj as HMPLRequestInfo)}}</div>`;
  132. };
  133. const createTestObj2 = (text: string) => {
  134. return `<div>${text}</div>`;
  135. };
  136. const createTestObj3 = (text: string) => {
  137. return `<div><button id="click">click</button>${text}</div>`;
  138. };
  139. const createTestObj4 = (text: string) => {
  140. return `<div><form onsubmit="function prevent(e){e.preventDefault();};return prevent(event);" id="form"></form>${text}</div>`;
  141. };
  142. export {
  143. e,
  144. eq,
  145. ae,
  146. aeq,
  147. aeqe,
  148. aeqError,
  149. createTestObj1,
  150. createTestObj2,
  151. createTestObj3,
  152. createTestObj4
  153. };