Mock ActivatedRoute breaks Router createUrlTree when using relativeTo parent

Discussão em 'Angular' iniciado por Rolando Isidoro, Setembro 26, 2024.

  1. I'm having trouble mocking ActivatedRoute with a valid parent property. I've tried a myriad of combinations on the providers metadata property of TestBed.configureTestingModule() similar to:

    await TestBed.configureTestingModule({
    providers: [
    {
    provide: ActivatedRoute,
    useValue: {
    parent: {snapshot: {}},
    snapshot: {},
    params: {}
    },
    }
    ]
    }


    Given a value of 1 on the someId variable, the following code snippet:

    const reconciliationUrl = this.router.createUrlTree(someId, 'some-action']).toString();


    produces the following expected result 1/some-action.

    After changing to:

    const reconciliationUrl = this.router.createUrlTree(
    [someId, 'some-action'],
    { relativeTo: this.activatedRoute.parent }
    ).toString();


    the result is always /.

    How can I effectively mock ActivatedRoute so that the call to createUrlTree generates a url string considering [someId, 'some-action'] commands without reset it to /?

    Continue reading...

Compartilhe esta Página