fix(runtime-utils): provide NuxtLink isActive slot props by Archetipo95 · Pull Request #1640 · nuxt/test-utils · GitHub
Skip to content

fix(runtime-utils): provide NuxtLink isActive slot props#1640

Merged
danielroe merged 7 commits into
nuxt:mainfrom
Archetipo95:fix/router-link-is-active-slot-props
Apr 27, 2026
Merged

fix(runtime-utils): provide NuxtLink isActive slot props#1640
danielroe merged 7 commits into
nuxt:mainfrom
Archetipo95:fix/router-link-is-active-slot-props

Conversation

@Archetipo95

Copy link
Copy Markdown
Contributor

Linked issue

Closes #1042

Description

This fixes NuxtLink custom slot behavior in mountSuspended by providing isActive and isExactActive slot props from the RouterLink test stub.

  • update RouterLink stub to expose isActive and isExactActive in custom-slot mode
  • add mountSuspended tests for active and inactive route states
  • add a fixture component reproducing v-slot="{ isActive }" usage

Validation

  • VITEST_DOM_ENV=happy-dom pnpm test:unit --run
  • VITEST_DOM_ENV=jsdom pnpm test:unit --run

@Archetipo95 Archetipo95 requested a review from danielroe as a code owner March 30, 2026 07:08
@pkg-pr-new

pkg-pr-new Bot commented Mar 30, 2026

Copy link
Copy Markdown

@coderabbitai

coderabbitai Bot commented Mar 30, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@danielroe has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 3 minutes and 15 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 62aa6cd8-e7a6-497f-a20e-342cf602725d

📥 Commits

Reviewing files that changed from the base of the PR and between f8631eb and f6de9c9.

📒 Files selected for processing (2)
  • examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts
  • src/runtime-utils/components/RouterLink.ts
📝 Walkthrough

Walkthrough

The changes add a new example Vue component (NuxtLinkWithIsActive.vue) and two example pages (/about and /about/team). Tests were added to assert NuxtLink custom-slot active-state behavior using mountSuspended. The RouterLink runtime was modified to use useLink(props) (instead of useRouter()), derive route, href, isActive, and isExactActive from the returned link, and to pass isActive and isExactActive into the custom slot; the non-custom anchor now uses link.href and calls link.navigate on click.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: fixing NuxtLink to provide isActive slot props in the RouterLink test stub.
Description check ✅ Passed The description is related to the changeset and explains what was fixed, how it was fixed, and how to validate the changes.
Linked Issues check ✅ Passed The PR fully addresses issue #1042 by updating RouterLink stub to expose isActive/isExactActive slot props, adding tests for active/inactive states, and including a fixture component demonstrating v-slot usage.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing NuxtLink custom slot behavior and testing it; new pages (about.vue, about/team.vue) are only fixture files supporting the tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts (1)

551-552: Prefer deterministic link selection in assertions.

Using text-search over all anchors is a bit brittle. Selecting the About anchor by href makes these tests more stable.

Suggested refinement
-  const aboutLink = component.findAll('a').find(el => el.text().includes('About'))
-  expect(aboutLink?.classes()).toContain('active')
+  const aboutLink = component.get('a[href="/about"]')
+  expect(aboutLink.classes()).toContain('active')
...
-  const aboutLink = component.findAll('a').find(el => el.text().includes('About'))
-  expect(aboutLink?.classes()).not.toContain('active')
+  const aboutLink = component.get('a[href="/about"]')
+  expect(aboutLink.classes()).not.toContain('active')

Also applies to: 560-561

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts` around lines 551
- 552, Replace the brittle text-based anchor lookup (aboutLink =
component.findAll('a').find(el => el.text().includes('About'))) with a
deterministic selector that targets the About link by its href attribute (use
component.find or component.findAll with 'a[href="/about"]'), then assert its
classes() contains 'active'; update the same pattern at the other occurrence
that currently uses text-search to ensure stable tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/runtime-utils/components/RouterLink.ts`:
- Around line 25-26: The two flags are incorrectly identical; change the
assignment so isActive checks whether the link's route record appears anywhere
in the currentRoute.matched hierarchy (e.g., use
currentRoute.matched.some(record => record === route || record.name ===
route.name)), while isExactActive remains the exact route match (e.g., compare
route.path or route.name to currentRoute.path/name). Update the variables
isActive and isExactActive in the RouterLink component to use these distinct
checks referencing route and currentRoute.

---

Nitpick comments:
In `@examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts`:
- Around line 551-552: Replace the brittle text-based anchor lookup (aboutLink =
component.findAll('a').find(el => el.text().includes('About'))) with a
deterministic selector that targets the About link by its href attribute (use
component.find or component.findAll with 'a[href="/about"]'), then assert its
classes() contains 'active'; update the same pattern at the other occurrence
that currently uses text-search to ensure stable tests.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f0ca9886-c1bf-4aa5-85da-124731e6fd2c

📥 Commits

Reviewing files that changed from the base of the PR and between 3218b87 and a0e458a.

📒 Files selected for processing (3)
  • examples/app-vitest-full/components/NuxtLinkWithIsActive.vue
  • examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts
  • src/runtime-utils/components/RouterLink.ts

Comment thread src/runtime-utils/components/RouterLink.ts Outdated
Archetipo95 and others added 2 commits March 30, 2026 11:48
use vue-router useLink in RouterLink mock

track nested-route NuxtLink-in-suspended limitation via expected-fail regression test
@Archetipo95

Copy link
Copy Markdown
Contributor Author

Im not sure if I can tag @coderabbitai, but the latest commit has the fix for isActive/isExactActive issue by changing the RouterLink stub to use useLink from vue-router instead of manual path matching.

One known limitation came up in testing. When NuxtLink is used with a custom slot inside mountSuspended, nested routes do not keep the parent link active. For example, /about is not marked active when the current route is /about/team.

This happens because NuxtLink looks for a .useLink property on the resolved RouterLink component to compute active state. Custom static properties are removed from components registered through global.components, so NuxtLink falls back to exact path comparison. So the exact match case still works, but the nested route case does not.I left that as an expected failing test for now: it.fails('keeps parent link active without exact match on nested routes...)

A proper fix would likely need to happen in VTU or in mountSuspended @danielroe do you agree or it's too overengineered?

@coderabbitai

coderabbitai Bot commented Mar 30, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: Cannot destructure property 'isActive' of 'undefined' as it is undefined - NuxtLink

2 participants