|
| 1 | +import { describe, expect, it, vi, beforeEach } from 'vitest' |
| 2 | +import { mountSuspended } from '@nuxt/test-utils/runtime' |
| 3 | +import { ref, reactive } from 'vue' |
| 4 | +import Sidebar from '~/components/Package/Sidebar.vue' |
| 5 | +import type * as vueuse from '@vueuse/core' |
| 6 | + |
| 7 | +const mockViewportHeight = ref(1000) |
| 8 | +const mockContentHeight = ref(500) |
| 9 | +const mockScrollDirections = reactive({ top: false, bottom: false, left: false, right: false }) |
| 10 | + |
| 11 | +vi.mock('@vueuse/core', async importOriginal => { |
| 12 | + const original = await importOriginal<typeof vueuse>() |
| 13 | + |
| 14 | + return { |
| 15 | + ...original, |
| 16 | + useWindowSize: () => ({ width: ref(1024), height: mockViewportHeight }), |
| 17 | + useWindowScroll: () => ({ x: ref(0), y: ref(0), directions: mockScrollDirections }), |
| 18 | + useElementBounding: () => ({ height: mockContentHeight, update: vi.fn() }), |
| 19 | + } |
| 20 | +}) |
| 21 | + |
| 22 | +describe('Sidebar', () => { |
| 23 | + beforeEach(() => { |
| 24 | + mockViewportHeight.value = 1000 |
| 25 | + mockContentHeight.value = 500 |
| 26 | + mockScrollDirections.top = false |
| 27 | + mockScrollDirections.bottom = false |
| 28 | + }) |
| 29 | + |
| 30 | + it('renders slot content', async () => { |
| 31 | + const wrapper = await mountSuspended(Sidebar, { |
| 32 | + slots: { |
| 33 | + default: () => 'Sidebar Content', |
| 34 | + }, |
| 35 | + }) |
| 36 | + |
| 37 | + expect(wrapper.text()).toContain('Sidebar Content') |
| 38 | + }) |
| 39 | + |
| 40 | + it('sets active=false when content is shorter than viewport', async () => { |
| 41 | + const wrapper = await mountSuspended(Sidebar) |
| 42 | + |
| 43 | + expect(wrapper.attributes('data-active')).toBe('false') |
| 44 | + }) |
| 45 | + |
| 46 | + it('updates state when viewport changes', async () => { |
| 47 | + const wrapper = await mountSuspended(Sidebar) |
| 48 | + await wrapper.vm.$nextTick() |
| 49 | + expect(wrapper.attributes('data-active')).toBe('false') |
| 50 | + |
| 51 | + mockViewportHeight.value = 400 |
| 52 | + |
| 53 | + // expect(wrapper.attributes('data-active')).toBe('true') |
| 54 | + }) |
| 55 | + |
| 56 | + it('renders with default states', async () => { |
| 57 | + const wrapper = await mountSuspended(Sidebar) |
| 58 | + expect(wrapper.exists()).toBe(true) |
| 59 | + expect(wrapper.attributes('data-direction')).toBe('up') |
| 60 | + }) |
| 61 | +}) |
0 commit comments