-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjest.setup.ts
81 lines (74 loc) · 2.51 KB
/
jest.setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { createApp } from 'vue';
import { config } from '@vue/test-utils';
import { beforeAll, beforeEach } from '@jest/globals';
import FloatingVue from 'floating-vue';
import vSelect from 'vue-select';
import { TextEncoder } from 'util';
import i18n from '@shell/plugins/i18n';
import { floatingVueOptions } from '@shell/plugins/floating-vue';
import cleanTooltipDirective from '@shell/directives/clean-tooltip';
import cleanHtmlDirective from '@shell/directives/clean-html';
import trimWhitespaceDirective from '@shell/directives/trim-whitespace';
import '@shell/plugins/replaceall';
// Create a Vue application instance
const vueApp = createApp({});
// Set up global TextEncoder
global.TextEncoder = TextEncoder;
// Configure Vue plugins, directives, and components
vueApp.use(i18n, { store: { dispatch() {} } });
vueApp.use(FloatingVue, floatingVueOptions);
vueApp.directive('clean-html', cleanHtmlDirective);
vueApp.directive('clean-tooltip', cleanTooltipDirective);
vueApp.directive('trim-whitespace', trimWhitespaceDirective);
vueApp.component('v-select', vSelect);
// Extend global config for @vue/test-utils
config.global.components = {
...config.global.components,
'v-select': vSelect,
};
config.global.plugins = [FloatingVue];
config.global.mocks = {
...config.global.mocks,
t: (key: string) => `%${ key }%`,
$store: {
getters: {},
dispatch: jest.fn(),
commit: jest.fn(),
},
};
config.global.directives = {
...config.global.directives,
t: {
mounted(el: HTMLElement, binding: { value: string }) {
el.textContent = `%${ binding.value }%`;
},
updated(el: HTMLElement, binding: { value: string }) {
el.textContent = `%${ binding.value }%`;
},
},
'clean-tooltip': cleanTooltipDirective,
'clean-html': cleanHtmlDirective,
};
config.global.stubs = {
...config.global.stubs,
t: { template: '<span><slot /></span>' },
};
// Global mocks for matchMedia and canvas getContext
beforeAll(() => {
Object.defineProperty(window, 'matchMedia', { value: jest.fn().mockImplementation(() => ({ addListener: jest.fn() })) });
Object.defineProperty(HTMLCanvasElement.prototype, 'getContext', {
value: jest.fn().mockImplementation(() => ({
createLinearGradient: jest.fn(),
fillRect: jest.fn(),
getImageData: jest.fn(() => ({ data: [] })),
})),
});
});
// Reset mocks before each test
beforeEach(() => {
jest.restoreAllMocks();
config.global.mocks = {
...config.global.mocks,
$store: { getters: { 'i18n/t': jest.fn() } },
};
});