utils.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 (component, options?) => {
  12. const opts = options || {};
  13. if (!opts.global) opts.global = {};
  14. let pinia;
  15. if (opts.usePinia) pinia = opts.usePinia;
  16. else if (
  17. opts.mockSocket ||
  18. (opts.pinia && opts.pinia.stubActions === false)
  19. )
  20. pinia = createTestingPinia({ stubActions: false });
  21. else pinia = createTestingPinia();
  22. if (opts.usePinia) delete opts.usePinia;
  23. if (opts.pinia) delete opts.pinia;
  24. const plugins = [
  25. pinia,
  26. [
  27. VueTippy,
  28. {
  29. directive: "tippy", // => v-tippy
  30. flipDuration: 0,
  31. popperOptions: {
  32. modifiers: {
  33. preventOverflow: {
  34. enabled: true
  35. }
  36. }
  37. },
  38. allowHTML: true,
  39. defaultProps: { animation: "scale", touch: "hold" }
  40. }
  41. ]
  42. ];
  43. if (opts.global.plugins)
  44. opts.global.plugins = [...opts.global.plugins, ...plugins];
  45. else opts.global.plugins = plugins;
  46. const components = { Tippy };
  47. if (opts.global.components)
  48. opts.global.components = {
  49. ...opts.global.components,
  50. ...components
  51. };
  52. else opts.global.components = components;
  53. await getConfig();
  54. if (opts.lofig) {
  55. lofig.config = {
  56. ...config,
  57. ...opts.lofig
  58. };
  59. delete opts.lofig;
  60. } else lofig.config = config;
  61. if (opts.mockSocket) {
  62. const websocketsStore = useWebsocketsStore();
  63. await websocketsStore.createSocket();
  64. await flushPromises();
  65. if (opts.mockSocket.data)
  66. websocketsStore.socket.data = opts.mockSocket.data;
  67. if (typeof opts.mockSocket.executeDispatch !== "undefined")
  68. websocketsStore.socket.executeDispatch =
  69. opts.mockSocket.executeDispatch;
  70. delete opts.mockSocket;
  71. }
  72. if (opts.loginRequired) {
  73. const userAuthStore = useUserAuthStore();
  74. userAuthStore.loggedIn = true;
  75. await flushPromises();
  76. delete opts.loginRequired;
  77. }
  78. if (opts.beforeMount) {
  79. await opts.beforeMount();
  80. delete opts.beforeMount;
  81. }
  82. if (opts.baseTemplate) {
  83. document.body.innerHTML = opts.baseTemplate;
  84. delete opts.baseTemplate;
  85. } else
  86. document.body.innerHTML = `
  87. <div id="root"></div>
  88. <div id="toasts-container" class="position-right position-bottom">
  89. <div id="toasts-content"></div>
  90. </div>
  91. `;
  92. if (!opts.attachTo) opts.attachTo = document.getElementById("root");
  93. const wrapper = mount(component, opts);
  94. if (opts.onMount) {
  95. await opts.onMount();
  96. delete opts.onMount;
  97. }
  98. await flushPromises();
  99. if (opts.afterMount) {
  100. await opts.afterMount();
  101. delete opts.afterMount;
  102. }
  103. return wrapper;
  104. };