Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions test/nuxt/components/Header/MobileMenu.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { describe, it, expect, vi } from 'vitest'
import { mockNuxtImport, mountSuspended } from '@nuxt/test-utils/runtime'
import { computed, nextTick } from 'vue'

// Mock useConnector
mockNuxtImport('useConnector', () => () => ({
isConnected: computed(() => false),
npmUser: computed(() => null),
avatar: computed(() => null),
}))
Comment thread
ghostdevv marked this conversation as resolved.

// Mock useAtproto
mockNuxtImport('useAtproto', () => () => ({
user: computed(() => null),
}))

// Mock useFocusTrap (from @vueuse/integrations)
vi.mock('@vueuse/integrations/useFocusTrap', () => ({
useFocusTrap: () => ({
activate: vi.fn(),
deactivate: vi.fn(),
}),
}))

describe('MobileMenu', () => {
async function mountMenu(open = false) {
const { HeaderMobileMenu } = await import('#components')
return mountSuspended(HeaderMobileMenu, {
props: {
open,
links: [
{
type: 'group' as const,
name: 'main',
label: 'Navigation',
items: [{ name: 'home', label: 'Home', to: '/', iconClass: 'i-lucide:home' }],

Check failure on line 36 in test/nuxt/components/Header/MobileMenu.spec.ts

View workflow job for this annotation

GitHub Actions / πŸ’ͺ Type check

Property 'type' is missing in type '{ name: string; label: string; to: "/"; iconClass: string; }' but required in type 'NavigationLink'.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
},
],
},
attachTo: document.body,
})
}

it('is closed by default', async () => {
const wrapper = await mountMenu(false)
try {
// Menu content is behind v-if="isOpen" inside a Teleport
expect(document.querySelector('[role="dialog"]')).toBeNull()
} finally {
wrapper.unmount()
}
})

it('opens when the open prop is set to true', async () => {
const wrapper = await mountMenu(true)
try {
await nextTick()
const dialog = document.querySelector('[role="dialog"]')
expect(dialog).not.toBeNull()
expect(dialog?.getAttribute('aria-modal')).toBe('true')
} finally {
wrapper.unmount()
}
})

it('closes when open prop changes from true to false', async () => {
const wrapper = await mountMenu(true)
try {
await nextTick()
expect(document.querySelector('[role="dialog"]')).not.toBeNull()

await wrapper.setProps({ open: false })
await nextTick()
expect(document.querySelector('[role="dialog"]')).toBeNull()
} finally {
wrapper.unmount()
}
})

it('emits update:open false when backdrop is clicked', async () => {
const wrapper = await mountMenu(true)
try {
await nextTick()
const backdrop = document.querySelector('[role="dialog"] > button')
expect(backdrop).not.toBeNull()
backdrop?.dispatchEvent(new Event('click', { bubbles: true }))
await nextTick()
expect(wrapper.emitted('update:open')).toBeTruthy()
expect(wrapper.emitted('update:open')![0]).toEqual([false])
} finally {
wrapper.unmount()
}
})

it('emits update:open false when close button is clicked', async () => {
const wrapper = await mountMenu(true)
try {
await nextTick()
// Close button has aria-label matching $t('common.close') β€” find it inside nav
const closeBtn = document.querySelector('nav button[aria-label]')
expect(closeBtn).not.toBeNull()
closeBtn?.dispatchEvent(new Event('click', { bubbles: true }))
await nextTick()
expect(wrapper.emitted('update:open')).toBeTruthy()
expect(wrapper.emitted('update:open')![0]).toEqual([false])
} finally {
wrapper.unmount()
}
})
})
Loading