Skip to content
Merged
Changes from 1 commit
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
112 changes: 112 additions & 0 deletions test/nuxt/components/Header/MobileMenu.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { describe, it, expect, vi } from 'vitest'
import { mockNuxtImport, mountSuspended } from '@nuxt/test-utils/runtime'
import { ref, computed, nextTick } from 'vue'

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

View workflow job for this annotation

GitHub Actions / πŸ€– Autofix code

eslint(no-unused-vars)

Identifier 'ref' is imported but never used.

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

View workflow job for this annotation

GitHub Actions / πŸ’ͺ Type check

'ref' is declared but its value is never read.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

// 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 { MobileMenuClient } = await import('#components')

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

View workflow job for this annotation

GitHub Actions / πŸ’ͺ Type check

Property 'MobileMenuClient' does not exist on type '{ default: typeof import("/home/runner/work/npmx.dev/npmx.dev/.nuxt/components"); BlogPostFederatedArticles: DefineComponent<__VLS_Props, {}, {}, {}, {}, ComponentOptionsMixin, ... 13 more ..., any>; ... 381 more ...; componentNames: string[]; }'.
return mountSuspended(MobileMenuClient, {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
props: {
open,
links: [
{
type: 'group' as const,
name: 'main',
label: 'Navigation',
items: [
{ name: 'home', label: 'Home', to: '/', iconClass: 'i-lucide:home' },
],
},
],
},
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