Skip to content

Accept query params in initialRoute property #409

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

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
77 changes: 40 additions & 37 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,45 @@ export async function render<SutType, WrapperType = SutType>(

const zone = safeInject(NgZone);
const router = safeInject(Router);
const _navigate = async (elementOrPath: Element | string, basePath = ''): Promise<boolean> => {
const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath.getAttribute('href');
const [path, params] = (basePath + href).split('?');
const queryParams = params
? params.split('&').reduce((qp, q) => {
const [key, value] = q.split('=');
const currentValue = qp[key];
if (typeof currentValue === 'undefined') {
qp[key] = value;
} else if (Array.isArray(currentValue)) {
qp[key] = [...currentValue, value];
} else {
qp[key] = [currentValue, value];
}
return qp;
}, {} as Record<string, string | string[]>)
: undefined;

if (initialRoute) await router.navigate([initialRoute]);
const navigateOptions: NavigationExtras | undefined = queryParams
? {
queryParams,
}
: undefined;

const doNavigate = () => {
return navigateOptions ? router?.navigate([path], navigateOptions) : router?.navigate([path]);
};

let result;

if (zone) {
await zone.run(() => (result = doNavigate()));
} else {
result = doNavigate();
}
return result ?? false;
};

if (initialRoute) await _navigate(initialRoute);

if (typeof router?.initialNavigation === 'function') {
if (zone) {
Expand Down Expand Up @@ -167,43 +204,9 @@ export async function render<SutType, WrapperType = SutType>(
};

const navigate = async (elementOrPath: Element | string, basePath = ''): Promise<boolean> => {
const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath.getAttribute('href');
const [path, params] = (basePath + href).split('?');
const queryParams = params
? params.split('&').reduce((qp, q) => {
const [key, value] = q.split('=');
const currentValue = qp[key];
if (typeof currentValue === 'undefined') {
qp[key] = value;
} else if (Array.isArray(currentValue)) {
qp[key] = [...currentValue, value];
} else {
qp[key] = [currentValue, value];
}
return qp;
}, {} as Record<string, string | string[]>)
: undefined;

const navigateOptions: NavigationExtras | undefined = queryParams
? {
queryParams,
}
: undefined;

const doNavigate = () => {
return navigateOptions ? router?.navigate([path], navigateOptions) : router?.navigate([path]);
};

let result;

if (zone) {
await zone.run(() => (result = doNavigate()));
} else {
result = doNavigate();
}

const result = await _navigate(elementOrPath, basePath);
detectChanges();
return result ?? false;
return result;
};

return {
Expand Down
2 changes: 2 additions & 0 deletions projects/testing-library/tests/integrations/ng-mocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { By } from '@angular/platform-browser';

import { MockComponent } from 'ng-mocks';
import { render } from '../../src/public_api';
import { NgIf } from '@angular/common';

test('sends the correct value to the child input', async () => {
const utils = await render(TargetComponent, {
Expand Down Expand Up @@ -34,6 +35,7 @@ test('sends the correct value to the child input 2', async () => {
selector: 'atl-child',
template: 'child',
standalone: true,
imports: [NgIf],
})
class ChildComponent {
@ContentChild('something')
Expand Down
28 changes: 27 additions & 1 deletion projects/testing-library/tests/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ 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';
import { ActivatedRoute, Resolve, RouterModule } from '@angular/router';
import { map } from 'rxjs';
import { AsyncPipe, NgIf } from '@angular/common';

@Component({
selector: 'atl-fixture',
Expand Down Expand Up @@ -365,6 +367,30 @@ describe('initialRoute', () => {
expect(screen.queryByText('Secondary Component')).not.toBeInTheDocument();
expect(screen.getByText('button')).toBeInTheDocument();
});

it('allows initially rendering a specific route with query parameters', async () => {
@Component({
standalone: true,
selector: 'atl-query-param-fixture',
template: `<p>paramPresent$: {{ paramPresent$ | async }}</p>`,
imports: [NgIf, AsyncPipe],
})
class QueryParamFixtureComponent {
constructor(public route: ActivatedRoute) {}

paramPresent$ = this.route.queryParams.pipe(map((queryParams) => (queryParams?.param ? 'present' : 'missing')));
}

const initialRoute = 'initial-route?param=query';
const routes = [{ path: 'initial-route', component: QueryParamFixtureComponent }];

await render(RouterFixtureComponent, {
initialRoute,
routes,
});

expect(screen.getByText(/present/i)).toBeVisible();
});
});

describe('configureTestBed', () => {
Expand Down