-
-
Notifications
You must be signed in to change notification settings - Fork 360
test(ui): add tests for mobile menu default closed state #2195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
howwohmm
wants to merge
5
commits into
npmx-dev:main
Choose a base branch
from
howwohmm:test/mobile-menu-default-state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
400ad78
test(ui): add tests for mobile menu default state and open/close behaβ¦
howwohmm a92bbc0
fix(test): correct component name and remove unused import
howwohmm 87dad03
[autofix.ci] apply automated fixes
autofix-ci[bot] 63493f8
fix(test): use static import and add missing type field
howwohmm dd44bf8
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { describe, it, expect, vi } from 'vitest' | ||
| import { mockNuxtImport, mountSuspended } from '@nuxt/test-utils/runtime' | ||
| import { computed, nextTick } from 'vue' | ||
| import { HeaderMobileMenu } from '#components' | ||
|
|
||
| // Mock useConnector | ||
| mockNuxtImport('useConnector', () => () => ({ | ||
| isConnected: computed(() => false), | ||
| npmUser: computed(() => null), | ||
| avatar: computed(() => null), | ||
| })) | ||
|
|
||
| // 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) { | ||
| return mountSuspended(HeaderMobileMenu, { | ||
| props: { | ||
| open, | ||
| links: [ | ||
| { | ||
| type: 'group' as const, | ||
| name: 'main', | ||
| label: 'Navigation', | ||
| items: [ | ||
| { | ||
| type: 'link' as const, | ||
| 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() | ||
| } | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Align the
useConnectormock shape with the real composable contract.Line 7 mocks
isConnected, but the productionuseConnectorcontract appears to useconnected. This mock can hide a real integration mismatch and give false confidence.Suggested adjustment
mockNuxtImport('useConnector', () => () => ({ - isConnected: computed(() => false), + connected: computed(() => false), npmUser: computed(() => null), avatar: computed(() => null), }))If
MobileMenu.client.vueexpectsisConnected, consider aliasing at destructure time there (e.g.connected: isConnected) so both runtime and tests reflect the same contract.π Committable suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't seem to be the case, where did you get your info?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π§© Analysis chain
Let me check the actual
useConnectorcomposable in the codebase to verify.π Script executed:
Length of output: 2504
π Script executed:
Length of output: 23403
@ghostdevv, you're right β I apologise for the incorrect comment. Looking at the actual source:In
app/composables/useConnector.ts, the composable's return object explicitly exposesisConnected: computed(() => state.value.connected). While the internalConnectorStateinterface usesconnectedas the raw state field, the public API surface returns it under the nameisConnected. The component itself confirms this withconst { isConnected, npmUser, avatar: npmAvatar } = useConnector(), and the existingcreateMockUseConnectorinHeaderConnectorModal.spec.tsalso usesisConnected. The test mock is correct as written β no change needed.βοΈ Learnings added
π§ Learnings used