|
| 1 | +// Import the mocked modules to set their behavior |
| 2 | +import { useData } from 'vitepress' |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +import { useSecondarySidebar } from '../useSecondarySidebar' |
| 6 | + |
| 7 | +// Mock the necessary dependencies |
| 8 | +vi.mock('vitepress', () => ({ |
| 9 | + useData: vi.fn(), |
| 10 | + useRoute: vi.fn(() => ({})), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('@vueuse/core', () => ({ |
| 14 | + createSharedComposable: (fn: any) => fn, |
| 15 | + useScroll: vi.fn(() => ({ y: { value: 0 } })), |
| 16 | + useThrottleFn: vi.fn(fn => fn), |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('vue', () => ({ |
| 20 | + computed: vi.fn(fn => ({ value: fn() })), |
| 21 | + nextTick: vi.fn(), |
| 22 | + onMounted: vi.fn(fn => fn()), |
| 23 | + ref: vi.fn(val => ({ value: val })), |
| 24 | + watch: vi.fn(), |
| 25 | +})) |
| 26 | + |
| 27 | +describe('useSecondarySidebar', () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.clearAllMocks() |
| 30 | + }) |
| 31 | + |
| 32 | + describe('layout behavior', () => { |
| 33 | + it('should hide sidebar for home layout', () => { |
| 34 | + // Mock the frontmatter to have home layout |
| 35 | + vi.mocked(useData).mockReturnValue({ |
| 36 | + frontmatter: { value: { layout: 'home' } }, |
| 37 | + } as any) |
| 38 | + |
| 39 | + const { showSecondarySidebar, showOutline, showWidget } = useSecondarySidebar() |
| 40 | + |
| 41 | + expect(showSecondarySidebar.value).toBe(false) |
| 42 | + expect(showOutline.value).toBe(false) |
| 43 | + expect(showWidget.value).toBe(false) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should show sidebar for docs layout', () => { |
| 47 | + // Mock the frontmatter to have docs layout with headings |
| 48 | + vi.mocked(useData).mockReturnValue({ |
| 49 | + frontmatter: { value: { layout: 'docs' } }, |
| 50 | + } as any) |
| 51 | + |
| 52 | + const { showSecondarySidebar, showOutline, showWidget } = useSecondarySidebar() |
| 53 | + |
| 54 | + // For docs layout, secondarySidebar should always be true by default |
| 55 | + expect(showSecondarySidebar.value).toBe(true) |
| 56 | + |
| 57 | + // However, outline depends on headings existing |
| 58 | + expect(showOutline.value).toBe(false) |
| 59 | + |
| 60 | + // Widget is always shown by default in docs layout |
| 61 | + expect(showWidget.value).toBe(true) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should respect explicit frontmatter settings regardless of layout', () => { |
| 65 | + // Mock the frontmatter to have home layout but with explicit secondary sidebar settings |
| 66 | + vi.mocked(useData).mockReturnValue({ |
| 67 | + frontmatter: { |
| 68 | + value: { |
| 69 | + layout: 'home', |
| 70 | + secondarySidebar: true, |
| 71 | + outline: true, |
| 72 | + widget: true, |
| 73 | + }, |
| 74 | + }, |
| 75 | + } as any) |
| 76 | + |
| 77 | + const { showSecondarySidebar, showOutline, showWidget } = useSecondarySidebar() |
| 78 | + |
| 79 | + expect(showSecondarySidebar.value).toBe(true) |
| 80 | + expect(showOutline.value).toBe(true) |
| 81 | + expect(showWidget.value).toBe(true) |
| 82 | + }) |
| 83 | + }) |
| 84 | +}) |
0 commit comments