utils.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { createTestingPinia } from "@pinia/testing";
  2. import VueTippy, { Tippy } from "vue-tippy";
  3. import { flushPromises, mount } from "@vue/test-utils";
  4. import { useWebsocketsStore } from "@/stores/websockets";
  5. import { useUserAuthStore } from "@/stores/userAuth";
  6. let config;
  7. const getConfig = async () => {
  8. if (!config) config = await import("../../../dist/config/template.json");
  9. return config;
  10. };
  11. export const getWrapper = async (
  12. component: any,
  13. options?: {
  14. global?: {
  15. plugins?: any[];
  16. components?: { [key: string]: any };
  17. };
  18. usePinia?: boolean;
  19. pinia?: {
  20. stubActions?: boolean;
  21. };
  22. mockSocket?: boolean | { data?: any; executeDispatch?: boolean };
  23. lofig?: any;
  24. loginRequired?: boolean;
  25. baseTemplate?: string;
  26. attachTo?: HTMLElement | null;
  27. beforeMount?: () => void;
  28. onMount?: () => void;
  29. afterMount?: () => void;
  30. }
  31. ) => {
  32. const opts = options || {};
  33. if (!opts.global) opts.global = {};
  34. let pinia;
  35. if (opts.usePinia) pinia = opts.usePinia;
  36. else if (
  37. opts.mockSocket ||
  38. (opts.pinia && opts.pinia.stubActions === false)
  39. )
  40. pinia = createTestingPinia({ stubActions: false });
  41. else pinia = createTestingPinia();
  42. if (opts.usePinia) delete opts.usePinia;
  43. if (opts.pinia) delete opts.pinia;
  44. const plugins = [
  45. pinia,
  46. [
  47. VueTippy,
  48. {
  49. directive: "tippy", // => v-tippy
  50. flipDuration: 0,
  51. popperOptions: {
  52. modifiers: {
  53. preventOverflow: {
  54. enabled: true
  55. }
  56. }
  57. },
  58. allowHTML: true,
  59. defaultProps: { animation: "scale", touch: "hold" }
  60. }
  61. ]
  62. ];
  63. if (opts.global.plugins)
  64. opts.global.plugins = [...opts.global.plugins, ...plugins];
  65. else opts.global.plugins = plugins;
  66. const components = { Tippy };
  67. if (opts.global.components)
  68. opts.global.components = {
  69. ...opts.global.components,
  70. ...components
  71. };
  72. else opts.global.components = components;
  73. await getConfig();
  74. if (opts.lofig) {
  75. lofig.config = {
  76. ...config,
  77. ...opts.lofig
  78. };
  79. delete opts.lofig;
  80. } else lofig.config = config;
  81. if (opts.mockSocket) {
  82. const websocketsStore = useWebsocketsStore();
  83. await websocketsStore.createSocket();
  84. await flushPromises();
  85. if (typeof opts.mockSocket === "object") {
  86. if (opts.mockSocket.data)
  87. websocketsStore.socket.data = opts.mockSocket.data;
  88. if (typeof opts.mockSocket.executeDispatch !== "undefined")
  89. websocketsStore.socket.executeDispatch =
  90. opts.mockSocket.executeDispatch;
  91. }
  92. delete opts.mockSocket;
  93. }
  94. if (opts.loginRequired) {
  95. const userAuthStore = useUserAuthStore();
  96. userAuthStore.loggedIn = true;
  97. await flushPromises();
  98. delete opts.loginRequired;
  99. }
  100. if (opts.beforeMount) {
  101. await opts.beforeMount();
  102. delete opts.beforeMount;
  103. }
  104. if (opts.baseTemplate) {
  105. document.body.innerHTML = opts.baseTemplate;
  106. delete opts.baseTemplate;
  107. } else
  108. document.body.innerHTML = `
  109. <div id="root"></div>
  110. <div id="toasts-container" class="position-right position-bottom">
  111. <div id="toasts-content"></div>
  112. </div>
  113. `;
  114. if (!opts.attachTo) opts.attachTo = document.getElementById("root");
  115. const wrapper = mount(component, opts);
  116. if (opts.onMount) {
  117. await opts.onMount();
  118. delete opts.onMount;
  119. }
  120. await flushPromises();
  121. if (opts.afterMount) {
  122. await opts.afterMount();
  123. delete opts.afterMount;
  124. }
  125. return wrapper;
  126. };