logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

setup_test.js (3144B)


  1. import { config } from '@vue/test-utils'
  2. import { createRouter, createMemoryHistory } from 'vue-router'
  3. import VueVirtualScroller from 'vue-virtual-scroller'
  4. import routes from 'src/boot/routes'
  5. import makeMockStore from './mock_store'
  6. export const $t = msg => msg
  7. const $i18n = { t: msg => msg }
  8. const applyAfterStore = (store, afterStore) => {
  9. afterStore(store)
  10. return store
  11. }
  12. const getDefaultOpts = ({ afterStore = () => {} } = {}) => ({
  13. global: {
  14. plugins: [
  15. applyAfterStore(makeMockStore(), afterStore),
  16. VueVirtualScroller,
  17. createRouter({
  18. history: createMemoryHistory(),
  19. routes: routes({ state: {
  20. users: {
  21. currentUser: {}
  22. },
  23. instance: {}
  24. }})
  25. }),
  26. (Vue) => { Vue.directive('body-scroll-lock', {}) }
  27. ],
  28. components: {
  29. },
  30. stubs: {
  31. I18nT: true,
  32. teleport: true,
  33. FAIcon: true,
  34. FALayers: true,
  35. },
  36. mocks: {
  37. $t,
  38. $i18n
  39. }
  40. }
  41. })
  42. // https://github.com/vuejs/vue-test-utils/issues/960
  43. const customBehaviors = () => {
  44. const filterByText = keyword => {
  45. const match = keyword instanceof RegExp
  46. ? (target) => target && keyword.test(target)
  47. : (target) => keyword === target
  48. return wrapper => (
  49. match(wrapper.text()) ||
  50. match(wrapper.attributes('aria-label')) ||
  51. match(wrapper.attributes('title'))
  52. )
  53. }
  54. return {
  55. findComponentByText(searchedComponent, text) {
  56. return this.findAllComponents(searchedComponent)
  57. .filter(filterByText(text))
  58. .at(0)
  59. },
  60. findByText(searchedElement, text) {
  61. return this.findAll(searchedElement)
  62. .filter(filterByText(text))
  63. .at(0)
  64. },
  65. };
  66. };
  67. config.plugins.VueWrapper.install(customBehaviors)
  68. export const mountOpts = (allOpts = {}) => {
  69. const { afterStore, ...opts } = allOpts
  70. const defaultOpts = getDefaultOpts({ afterStore })
  71. const mergedOpts = {
  72. ...opts,
  73. global: {
  74. ...defaultOpts.global
  75. }
  76. }
  77. if (opts.global) {
  78. mergedOpts.global.plugins = mergedOpts.global.plugins.concat(opts.global.plugins || [])
  79. Object.entries(opts.global).forEach(([k, v]) => {
  80. if (k === 'plugins') {
  81. return
  82. }
  83. if (defaultOpts.global[k]) {
  84. mergedOpts.global[k] = {
  85. ...defaultOpts.global[k],
  86. ...v,
  87. }
  88. } else {
  89. mergedOpts.global[k] = v
  90. }
  91. })
  92. }
  93. return mergedOpts
  94. }
  95. // https://stackoverflow.com/questions/78033718/how-can-i-wait-for-an-emitted-event-of-a-mounted-component-in-vue-test-utils
  96. export const waitForEvent = (wrapper, event, {
  97. timeout = 1000,
  98. timesEmitted = 1
  99. } = {}) => {
  100. const tick = 10
  101. const totalTries = timeout / tick
  102. return new Promise((resolve, reject) => {
  103. let currentTries = 0
  104. const wait = () => {
  105. const e = wrapper.emitted(event)
  106. if (e && e.length >= timesEmitted) {
  107. resolve()
  108. return
  109. }
  110. if (currentTries >= totalTries) {
  111. reject(new Error('Event did not fire'))
  112. return
  113. }
  114. ++currentTries
  115. setTimeout(wait, tick)
  116. }
  117. wait()
  118. })
  119. }