Skip to content
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

feat: Added initialRoute parameter to avoid resolver issues with using a default route #367

Merged
merged 2 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions projects/testing-library/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,21 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
*/
routes?: Routes;

/**
* @description
* Specifies which route should be initially navigated to
*
* @example
* const component = await render(AppComponent, {
* initialRoute: 'myroute',
* routes: [
* { path: '', component: HomeComponent },
* { path: 'myroute', component: SecondaryComponent }
* ]
* })
*/
initialRoute?: string;

/**
* @description
* Removes the Angular attributes (ng-version, and root-id) from the fixture.
Expand Down
3 changes: 3 additions & 0 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export async function render<SutType, WrapperType = SutType>(
routes = [],
removeAngularAttributes = false,
defaultImports = [],
initialRoute = '',
} = { ...globalConfig, ...renderOptions };

dtlConfigure({
Expand Down Expand Up @@ -107,6 +108,8 @@ export async function render<SutType, WrapperType = SutType>(
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());
Expand Down
47 changes: 47 additions & 0 deletions projects/testing-library/tests/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -296,3 +297,49 @@ describe('DebugElement', () => {
expect(view.debugElement.componentInstance).toBeInstanceOf(FixtureComponent);
});
});

describe('initialRoute', () => {
@Component({
standalone: true,
selector: 'atl-fixture2',
template: `<button>Secondary Component</button>`,
})
class SecondaryFixtureComponent {}

@Component({
standalone: true,
selector: 'atl-router-fixture',
template: `<router-outlet></router-outlet>`,
imports: [RouterModule],
})
class RouterFixtureComponent {}

@Injectable()
class FixtureResolver implements Resolve<void> {
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 expectedRoute = 'correct-route';
Copy link
Member

Choose a reason for hiding this comment

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

Could you rename this to the following please.
I do think it makes it intention clearer.

Suggested change
const expectedRoute = 'correct-route';
const initialRoute = 'initial-route';

const routes = [
{ path: expectedRoute, component: FixtureComponent },
{ path: '**', resolve: { data: FixtureResolver }, component: SecondaryFixtureComponent },
];

await render(RouterFixtureComponent, {
initialRoute: expectedRoute,
routes,
detectChangesOnRender: false,
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why detect changes is set to false?

Suggested change
detectChangesOnRender: false,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mainly just to isolate the test from any extra logic, but I can remove it since it doesn't add any value here

providers: [FixtureResolver],
});
const resolver = TestBed.inject(FixtureResolver);

expect(resolver.isResolved).toBe(false);
expect(screen.queryByText('Secondary Component')).not.toBeInTheDocument();
expect(await screen.findByText('button')).toBeInTheDocument();
Copy link
Member

Choose a reason for hiding this comment

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

There's no need to await here.

Suggested change
expect(await screen.findByText('button')).toBeInTheDocument();
expect(screen.getByText('button')).toBeInTheDocument();

});
});