2025-02-18 17:42:50 -05:00
|
|
|
import { config } from '@vue/test-utils'
|
2026-01-06 16:23:17 +02:00
|
|
|
import { createMemoryHistory, createRouter } from 'vue-router'
|
|
|
|
|
import VueVirtualScroller from 'vue-virtual-scroller'
|
2026-01-08 17:26:52 +02:00
|
|
|
|
|
|
|
|
import routes from 'src/boot/routes'
|
2025-02-18 17:42:50 -05:00
|
|
|
import makeMockStore from './mock_store'
|
|
|
|
|
|
2026-01-06 16:22:52 +02:00
|
|
|
export const $t = (msg) => msg
|
|
|
|
|
const $i18n = { t: (msg) => msg }
|
2025-02-18 17:42:50 -05:00
|
|
|
|
|
|
|
|
const applyAfterStore = (store, afterStore) => {
|
|
|
|
|
afterStore(store)
|
|
|
|
|
return store
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-06 17:32:22 +02:00
|
|
|
const getDefaultOpts = ({
|
|
|
|
|
afterStore = () => {
|
|
|
|
|
/* no-op */
|
|
|
|
|
},
|
|
|
|
|
} = {}) => ({
|
2025-02-18 17:42:50 -05:00
|
|
|
global: {
|
|
|
|
|
plugins: [
|
|
|
|
|
applyAfterStore(makeMockStore(), afterStore),
|
|
|
|
|
VueVirtualScroller,
|
|
|
|
|
createRouter({
|
|
|
|
|
history: createMemoryHistory(),
|
2026-01-06 16:22:52 +02:00
|
|
|
routes: routes({
|
|
|
|
|
state: {
|
|
|
|
|
users: {
|
|
|
|
|
currentUser: {},
|
|
|
|
|
},
|
|
|
|
|
instance: {},
|
2025-02-18 17:42:50 -05:00
|
|
|
},
|
2026-01-06 16:22:52 +02:00
|
|
|
}),
|
2025-02-18 17:42:50 -05:00
|
|
|
}),
|
2026-01-06 16:22:52 +02:00
|
|
|
(Vue) => {
|
|
|
|
|
Vue.directive('body-scroll-lock', {})
|
|
|
|
|
},
|
2025-02-18 17:42:50 -05:00
|
|
|
],
|
2026-01-06 16:22:52 +02:00
|
|
|
components: {},
|
2025-02-18 17:42:50 -05:00
|
|
|
stubs: {
|
|
|
|
|
I18nT: true,
|
|
|
|
|
teleport: true,
|
|
|
|
|
FAIcon: true,
|
|
|
|
|
FALayers: true,
|
|
|
|
|
},
|
|
|
|
|
mocks: {
|
|
|
|
|
$t,
|
2026-01-06 16:22:52 +02:00
|
|
|
$i18n,
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-02-18 17:42:50 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// https://github.com/vuejs/vue-test-utils/issues/960
|
|
|
|
|
const customBehaviors = () => {
|
2026-01-06 16:22:52 +02:00
|
|
|
const filterByText = (keyword) => {
|
|
|
|
|
const match =
|
|
|
|
|
keyword instanceof RegExp
|
|
|
|
|
? (target) => target && keyword.test(target)
|
|
|
|
|
: (target) => keyword === target
|
2025-02-18 17:42:50 -05:00
|
|
|
|
2026-01-06 16:22:52 +02:00
|
|
|
return (wrapper) =>
|
2025-02-18 17:42:50 -05:00
|
|
|
match(wrapper.text()) ||
|
2026-01-06 16:22:52 +02:00
|
|
|
match(wrapper.attributes('aria-label')) ||
|
|
|
|
|
match(wrapper.attributes('title'))
|
2025-02-18 17:42:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
findComponentByText(searchedComponent, text) {
|
|
|
|
|
return this.findAllComponents(searchedComponent)
|
|
|
|
|
.filter(filterByText(text))
|
|
|
|
|
.at(0)
|
|
|
|
|
},
|
|
|
|
|
findByText(searchedElement, text) {
|
2026-01-06 16:22:52 +02:00
|
|
|
return this.findAll(searchedElement).filter(filterByText(text)).at(0)
|
2025-02-18 17:42:50 -05:00
|
|
|
},
|
2026-01-06 16:22:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-02-18 17:42:50 -05:00
|
|
|
|
|
|
|
|
config.plugins.VueWrapper.install(customBehaviors)
|
|
|
|
|
|
|
|
|
|
export const mountOpts = (allOpts = {}) => {
|
|
|
|
|
const { afterStore, ...opts } = allOpts
|
|
|
|
|
const defaultOpts = getDefaultOpts({ afterStore })
|
|
|
|
|
const mergedOpts = {
|
|
|
|
|
...opts,
|
|
|
|
|
global: {
|
2026-01-06 16:22:52 +02:00
|
|
|
...defaultOpts.global,
|
|
|
|
|
},
|
2025-02-18 17:42:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (opts.global) {
|
2026-01-06 16:22:52 +02:00
|
|
|
mergedOpts.global.plugins = mergedOpts.global.plugins.concat(
|
|
|
|
|
opts.global.plugins || [],
|
|
|
|
|
)
|
2025-02-18 17:42:50 -05:00
|
|
|
Object.entries(opts.global).forEach(([k, v]) => {
|
|
|
|
|
if (k === 'plugins') {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (defaultOpts.global[k]) {
|
|
|
|
|
mergedOpts.global[k] = {
|
|
|
|
|
...defaultOpts.global[k],
|
|
|
|
|
...v,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
mergedOpts.global[k] = v
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mergedOpts
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/78033718/how-can-i-wait-for-an-emitted-event-of-a-mounted-component-in-vue-test-utils
|
2026-01-06 16:22:52 +02:00
|
|
|
export const waitForEvent = (
|
|
|
|
|
wrapper,
|
|
|
|
|
event,
|
|
|
|
|
{ timeout = 1000, timesEmitted = 1 } = {},
|
|
|
|
|
) => {
|
2025-02-18 17:42:50 -05:00
|
|
|
const tick = 10
|
|
|
|
|
|
2025-02-27 22:54:23 -05:00
|
|
|
return vi.waitFor(
|
|
|
|
|
() => {
|
2025-02-18 17:42:50 -05:00
|
|
|
const e = wrapper.emitted(event)
|
|
|
|
|
if (e && e.length >= timesEmitted) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-02-27 22:54:23 -05:00
|
|
|
throw new Error('event is not emitted')
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
timeout,
|
2026-01-06 16:22:52 +02:00
|
|
|
interval: tick,
|
|
|
|
|
},
|
2025-02-27 22:54:23 -05:00
|
|
|
)
|
2025-02-18 17:42:50 -05:00
|
|
|
}
|