|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import { mount, flushPromises } from '@vue/test-utils'; |
| 3 | +import { createPinia, setActivePinia } from 'pinia'; |
| 4 | +import { createVuetify } from 'vuetify'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Hoisted store action mocks — must be defined before any imports that use them. |
| 8 | + */ |
| 9 | +const initStatisticsMock = vi.hoisted(() => vi.fn()); |
| 10 | +const getStatisticsMock = vi.hoisted(() => vi.fn()); |
| 11 | +const getNewsMock = vi.hoisted(() => vi.fn()); |
| 12 | + |
| 13 | +vi.mock('../stores/home.store', () => ({ |
| 14 | + useHomeStore: () => ({ |
| 15 | + news: [], |
| 16 | + statistics: null, |
| 17 | + initStatistics: initStatisticsMock, |
| 18 | + getStatistics: getStatisticsMock, |
| 19 | + getNews: getNewsMock, |
| 20 | + }), |
| 21 | +})); |
| 22 | + |
| 23 | +// Stub all child components to avoid deep render |
| 24 | +vi.mock('../components/home.hero.component.vue', () => ({ default: { template: '<div />' } })); |
| 25 | +vi.mock('../components/home.presentation.component.vue', () => ({ default: { template: '<div />' } })); |
| 26 | +vi.mock('../components/home.about.component.vue', () => ({ default: { template: '<div />' } })); |
| 27 | +vi.mock('../components/home.capabilities.component.vue', () => ({ default: { template: '<div />' } })); |
| 28 | +vi.mock('../components/home.features.component.vue', () => ({ default: { template: '<div />' } })); |
| 29 | +vi.mock('../components/home.services.component.vue', () => ({ default: { template: '<div />' } })); |
| 30 | +vi.mock('../components/home.steps.component.vue', () => ({ default: { template: '<div />' } })); |
| 31 | +vi.mock('../components/home.gallery.component.vue', () => ({ default: { template: '<div />' } })); |
| 32 | +vi.mock('../components/home.social.component.vue', () => ({ default: { template: '<div />' } })); |
| 33 | +vi.mock('../components/home.articles.component.vue', () => ({ default: { template: '<div />' } })); |
| 34 | +vi.mock('../components/home.statistics.component.vue', () => ({ default: { template: '<div />' } })); |
| 35 | +vi.mock('../components/home.faq.component.vue', () => ({ default: { template: '<div />' } })); |
| 36 | +vi.mock('../components/home.cta.component.vue', () => ({ default: { template: '<div />' } })); |
| 37 | +vi.mock('../components/home.contact.component.vue', () => ({ default: { template: '<div />' } })); |
| 38 | + |
| 39 | +import HomeView from '../views/home.view.vue'; |
| 40 | + |
| 41 | +/** |
| 42 | + * Mount home.view with a given config. |
| 43 | + * @param {Object} homeConfig - Value of config.home |
| 44 | + * @returns {import('@vue/test-utils').VueWrapper} |
| 45 | + */ |
| 46 | +const mountView = (homeConfig = {}) => |
| 47 | + mount(HomeView, { |
| 48 | + global: { |
| 49 | + plugins: [createVuetify(), createPinia()], |
| 50 | + config: { |
| 51 | + globalProperties: { |
| 52 | + config: { home: homeConfig }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | +describe('home.view — created() statistics.dynamic guard (T4)', () => { |
| 59 | + beforeEach(() => { |
| 60 | + setActivePinia(createPinia()); |
| 61 | + vi.clearAllMocks(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('calls initStatistics and getStatistics when statistics is truthy and dynamic is not set', async () => { |
| 65 | + mountView({ statistics: { content: [] } }); |
| 66 | + await flushPromises(); |
| 67 | + |
| 68 | + expect(initStatisticsMock).toHaveBeenCalledTimes(1); |
| 69 | + expect(getStatisticsMock).toHaveBeenCalledTimes(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it('calls initStatistics but skips getStatistics when statistics.dynamic is false', async () => { |
| 73 | + mountView({ statistics: { content: [], dynamic: false } }); |
| 74 | + await flushPromises(); |
| 75 | + |
| 76 | + expect(initStatisticsMock).toHaveBeenCalledTimes(1); |
| 77 | + expect(getStatisticsMock).not.toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('calls initStatistics and getStatistics when statistics.dynamic is true', async () => { |
| 81 | + mountView({ statistics: { content: [], dynamic: true } }); |
| 82 | + await flushPromises(); |
| 83 | + |
| 84 | + expect(initStatisticsMock).toHaveBeenCalledTimes(1); |
| 85 | + expect(getStatisticsMock).toHaveBeenCalledTimes(1); |
| 86 | + }); |
| 87 | + |
| 88 | + it('skips initStatistics and getStatistics when config.home.statistics is falsy', async () => { |
| 89 | + mountView({}); |
| 90 | + await flushPromises(); |
| 91 | + |
| 92 | + expect(initStatisticsMock).not.toHaveBeenCalled(); |
| 93 | + expect(getStatisticsMock).not.toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('calls getNews when config.home.articles is set', async () => { |
| 97 | + mountView({ articles: { enabled: true } }); |
| 98 | + await flushPromises(); |
| 99 | + |
| 100 | + expect(getNewsMock).toHaveBeenCalledTimes(1); |
| 101 | + }); |
| 102 | + |
| 103 | + it('does not call getNews when config.home.articles is falsy', async () => { |
| 104 | + mountView({}); |
| 105 | + await flushPromises(); |
| 106 | + |
| 107 | + expect(getNewsMock).not.toHaveBeenCalled(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments