| jason westover | 313d15c | 2025-08-18 17:16:01 -0500 | [diff] [blame] | 1 | import { config } from '@vue/test-utils'; |
| Jason Westover | e5b9cca | 2026-01-08 10:57:41 -0600 | [diff] [blame] | 2 | import { vi } from 'vitest'; |
| jason westover | 313d15c | 2025-08-18 17:16:01 -0500 | [diff] [blame] | 3 | |
| 4 | // Make Math.random deterministic for stable snapshots (e.g., IDs in components) |
| Jason Westover | e5b9cca | 2026-01-08 10:57:41 -0600 | [diff] [blame] | 5 | vi.spyOn(Math, 'random').mockReturnValue(0.123456789); |
| jason westover | 313d15c | 2025-08-18 17:16:01 -0500 | [diff] [blame] | 6 | |
| Jason Westover | e5b9cca | 2026-01-08 10:57:41 -0600 | [diff] [blame] | 7 | // Stub SVG component imports to avoid verbose path data in snapshots |
| 8 | const SvgStub = { |
| 9 | template: '<svg data-testid="svg-stub"><title>SVG Stub</title></svg>', |
| 10 | }; |
| 11 | vi.mock('@/assets/images/logo-header.svg?component', () => ({ |
| 12 | default: SvgStub, |
| 13 | })); |
| 14 | vi.mock('@/assets/images/login-company-logo.svg?component', () => ({ |
| 15 | default: SvgStub, |
| 16 | })); |
| 17 | vi.mock('@/assets/images/built-on-openbmc-logo.svg?component', () => ({ |
| 18 | default: SvgStub, |
| jason westover | 313d15c | 2025-08-18 17:16:01 -0500 | [diff] [blame] | 19 | })); |
| 20 | |
| Jason Westover | e5b9cca | 2026-01-08 10:57:41 -0600 | [diff] [blame] | 21 | // Mock vue-router - provide a minimal API for tests that import it |
| 22 | vi.mock('vue-router', () => ({ |
| 23 | createRouter: () => ({}), |
| 24 | createMemoryHistory: () => ({}), |
| 25 | useRouter: () => ({ |
| 26 | push: vi.fn(), |
| 27 | replace: vi.fn(), |
| 28 | }), |
| 29 | useRoute: () => ({ |
| 30 | params: {}, |
| 31 | query: {}, |
| 32 | meta: { title: '' }, |
| 33 | }), |
| 34 | })); |
| jason westover | 313d15c | 2025-08-18 17:16:01 -0500 | [diff] [blame] | 35 | |
| Jason Westover | e5b9cca | 2026-01-08 10:57:41 -0600 | [diff] [blame] | 36 | // Use the real i18n instance - Vite's import.meta.glob works natively |
| 37 | import i18n from '@/i18n'; |
| jason westover | 313d15c | 2025-08-18 17:16:01 -0500 | [diff] [blame] | 38 | |
| 39 | // Provide default global mocks/stubs |
| 40 | config.global.mocks = { |
| 41 | $t: (k) => k, |
| 42 | $route: { meta: { title: '' } }, |
| 43 | $eventBus: { |
| 44 | on: () => {}, |
| 45 | off: () => {}, |
| 46 | emit: () => {}, |
| 47 | $on: () => {}, |
| 48 | $off: () => {}, |
| 49 | $emit: () => {}, |
| 50 | }, |
| 51 | }; |
| 52 | |
| 53 | // Stubs with single root elements to properly inherit attributes like data-test-id |
| 54 | config.global.stubs = { |
| 55 | 'router-link': { template: '<a><slot/></a>' }, |
| 56 | 'b-navbar': { template: '<nav><slot/></nav>' }, |
| 57 | 'b-navbar-brand': { template: '<div><slot/></div>' }, |
| 58 | 'b-navbar-nav': { template: '<div><slot/></div>' }, |
| 59 | 'b-dropdown': { template: '<div><slot/></div>' }, |
| 60 | 'b-dropdown-item': { template: '<div><slot/></div>' }, |
| 61 | 'b-nav': { template: '<ul class="nav mb-4 flex-column"><slot/></ul>' }, |
| 62 | 'b-nav-item': { template: '<li><slot/></li>' }, |
| 63 | 'b-collapse': { template: '<ul><slot/></ul>' }, |
| 64 | 'b-button': { template: '<button><slot/></button>' }, |
| 65 | 'b-input-group': { template: '<div class="input-group"><slot/></div>' }, |
| 66 | 'b-input-group-prepend': { |
| 67 | template: '<div class="input-group-prepend"><slot/></div>', |
| 68 | }, |
| 69 | 'b-input-group-text': { |
| 70 | template: '<span class="input-group-text"><slot/></span>', |
| 71 | }, |
| 72 | 'b-form-group': { template: '<div class="form-group mb-2"><slot/></div>' }, |
| 73 | 'b-form-input': { template: '<input class="form-control search-input" />' }, |
| 74 | 'b-form-checkbox': { template: '<div class="form-check"><slot/></div>' }, |
| 75 | 'b-form-radio': { template: '<div class="form-check"><slot/></div>' }, |
| 76 | 'b-form-select': { template: '<select><slot/></select>' }, |
| 77 | 'b-progress': { template: '<div class="progress"><slot/></div>' }, |
| 78 | 'b-progress-bar': { template: '<div class="progress-bar"></div>' }, |
| 79 | 'b-modal': { template: '<div class="modal"><slot/></div>' }, |
| 80 | 'b-tooltip': { template: '<div><slot/></div>' }, |
| 81 | }; |
| 82 | |
| 83 | // Provide plugins - i18n for useI18n() support, and $root helpers |
| 84 | config.global.plugins = [ |
| Jason Westover | e5b9cca | 2026-01-08 10:57:41 -0600 | [diff] [blame] | 85 | i18n, |
| jason westover | 313d15c | 2025-08-18 17:16:01 -0500 | [diff] [blame] | 86 | { |
| 87 | install(app) { |
| jason westover | 313d15c | 2025-08-18 17:16:01 -0500 | [diff] [blame] | 88 | app.config.globalProperties.$root = |
| 89 | app.config.globalProperties.$root || {}; |
| 90 | if (!app.config.globalProperties.$root.$on) { |
| 91 | app.config.globalProperties.$root.$on = () => {}; |
| 92 | } |
| 93 | if (!app.config.globalProperties.$root.$emit) { |
| 94 | app.config.globalProperties.$root.$emit = () => {}; |
| 95 | } |
| 96 | app.mixin({ |
| 97 | beforeCreate() { |
| 98 | const r = this.$root; |
| 99 | if (r && !r.$on) r.$on = () => {}; |
| 100 | if (r && !r.$emit) r.$emit = () => {}; |
| 101 | }, |
| 102 | }); |
| 103 | }, |
| 104 | }, |
| 105 | ]; |
| 106 | |
| 107 | // Stub bootstrap-vue directives |
| 108 | config.global.directives = { |
| 109 | 'b-tooltip': () => {}, |
| 110 | 'b-toggle': () => {}, |
| 111 | }; |