Bird
0
0

Given this test snippet, what will location.path() return after navigation?

medium📝 component behavior Q13 of 15
Angular - Testing
Given this test snippet, what will location.path() return after navigation?
const fixture = TestBed.createComponent(AppComponent);
const router = TestBed.inject(Router);
const location = TestBed.inject(Location);
router.navigate(['/dashboard']);
fixture.detectChanges();
await fixture.whenStable();
console.log(location.path());
A"/"
B"/dashboard"
C"/home"
D"undefined"
Step-by-Step Solution
Solution:
  1. Step 1: Understand navigation and location.path()

    Calling router.navigate(['/dashboard']) changes the URL path to '/dashboard'. The Location service reflects this path.
  2. Step 2: Confirm location.path() after navigation

    After navigation and stabilization, location.path() returns the current URL path, which is '/dashboard'.
  3. Final Answer:

    "/dashboard" -> Option B
  4. Quick Check:

    location.path() shows current URL path [OK]
Quick Trick: location.path() returns the current URL after navigation [OK]
Common Mistakes:
  • Expecting location.path() to be '/' by default
  • Confusing location.path() with component state
  • Not awaiting navigation completion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes