diff --git a/projects/testing-library/src/lib/models.ts b/projects/testing-library/src/lib/models.ts index 51fe5f7..0e4b404 100644 --- a/projects/testing-library/src/lib/models.ts +++ b/projects/testing-library/src/lib/models.ts @@ -328,6 +328,21 @@ export interface RenderComponentOptions( routes = [], removeAngularAttributes = false, defaultImports = [], + initialRoute = '', } = { ...globalConfig, ...renderOptions }; dtlConfigure({ @@ -107,6 +108,8 @@ export async function render( const zone = safeInject(NgZone); const router = safeInject(Router); + if (initialRoute) await router.navigate([initialRoute]); + if (typeof router?.initialNavigation === 'function') { if (zone) { zone.run(() => router.initialNavigation()); diff --git a/projects/testing-library/tests/render.spec.ts b/projects/testing-library/tests/render.spec.ts index 12a1f62..28e5996 100644 --- a/projects/testing-library/tests/render.spec.ts +++ b/projects/testing-library/tests/render.spec.ts @@ -12,6 +12,7 @@ import { import { NoopAnimationsModule, BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { TestBed } from '@angular/core/testing'; import { render, fireEvent, screen } from '../src/public_api'; +import { Resolve, RouterModule } from '@angular/router'; @Component({ selector: 'atl-fixture', @@ -296,3 +297,48 @@ describe('DebugElement', () => { expect(view.debugElement.componentInstance).toBeInstanceOf(FixtureComponent); }); }); + +describe('initialRoute', () => { + @Component({ + standalone: true, + selector: 'atl-fixture2', + template: ``, + }) + class SecondaryFixtureComponent {} + + @Component({ + standalone: true, + selector: 'atl-router-fixture', + template: ``, + imports: [RouterModule], + }) + class RouterFixtureComponent {} + + @Injectable() + class FixtureResolver implements Resolve { + public isResolved = false; + + public resolve() { + this.isResolved = true; + } + } + + it('allows initially rendering a specific route to avoid triggering a resolver for the default route', async () => { + const initialRoute = 'initial-route'; + const routes = [ + { path: initialRoute, component: FixtureComponent }, + { path: '**', resolve: { data: FixtureResolver }, component: SecondaryFixtureComponent }, + ]; + + await render(RouterFixtureComponent, { + initialRoute, + routes, + providers: [FixtureResolver], + }); + const resolver = TestBed.inject(FixtureResolver); + + expect(resolver.isResolved).toBe(false); + expect(screen.queryByText('Secondary Component')).not.toBeInTheDocument(); + expect(screen.getByText('button')).toBeInTheDocument(); + }); +});