Skip to content

Commit

Permalink
feat: add routeConfig to the stub snapshot and route
Browse files Browse the repository at this point in the history
  • Loading branch information
jnizet committed Mar 3, 2022
1 parent cc184ba commit 768c113
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 8 additions & 2 deletions projects/ngx-speculoos/src/lib/route.spec.ts
@@ -1,4 +1,4 @@
import { ActivatedRoute, ActivatedRouteSnapshot, Data, ParamMap, Params, UrlSegment } from '@angular/router';
import { ActivatedRoute, ActivatedRouteSnapshot, Data, ParamMap, Params, Route, UrlSegment } from '@angular/router';
import { fakeRoute, fakeSnapshot, stubRoute } from './route';
import { of } from 'rxjs';

Expand Down Expand Up @@ -171,6 +171,7 @@ describe('routes', () => {
expect(route.snapshot.firstChild).toBeNull();
expect(route.snapshot.pathFromRoot).toEqual([route.snapshot]);
expect(route.snapshot.root).toBe(route.snapshot);
expect(route.snapshot.routeConfig).toBeNull();

let params: Params;
route.params.subscribe(p => (params = p));
Expand Down Expand Up @@ -205,6 +206,7 @@ describe('routes', () => {
expect(route.firstChild).toBeNull();
expect(route.pathFromRoot).toEqual([route]);
expect(route.root).toBe(route);
expect(route.routeConfig).toBeNull();
});

it('should fill the snapshot and the route with values if options are provided', () => {
Expand All @@ -213,6 +215,7 @@ describe('routes', () => {
const providedData = { jing: 'zoom' };
const providedFragment = 'hello';
const providedUrl = [new UrlSegment('/path', {})];
const providedRouteConfig: Route = { path: 'foo' };

const parent = stubRoute();
const firstChild = stubRoute();
Expand All @@ -226,7 +229,8 @@ describe('routes', () => {
url: providedUrl,
parent,
firstChild,
children
children,
routeConfig: providedRouteConfig
});

expect(route.snapshot.params).toBe(providedParams);
Expand All @@ -236,6 +240,7 @@ describe('routes', () => {
expect(route.snapshot.data).toEqual(providedData);
expect(route.snapshot.fragment).toBe(providedFragment);
expect(route.snapshot.url).toBe(providedUrl);
expect(route.snapshot.routeConfig).toBe(providedRouteConfig);
expect(route.snapshot.parent).toBe(parent.snapshot);
expect(route.snapshot.children).toEqual(children.map(c => c.snapshot));
expect(route.snapshot.firstChild).toBe(firstChild.snapshot);
Expand Down Expand Up @@ -275,6 +280,7 @@ describe('routes', () => {
expect(route.firstChild).toBe(firstChild);
expect(route.pathFromRoot).toEqual([parent, route]);
expect(route.root).toBe(parent);
expect(route.routeConfig).toBe(providedRouteConfig);
});

it('should set a param', () => {
Expand Down
20 changes: 20 additions & 0 deletions projects/ngx-speculoos/src/lib/route.ts
Expand Up @@ -179,6 +179,10 @@ export interface ActivatedRouteStubOptions {
* The children of the route
*/
children?: ActivatedRouteStub[] | null;
/**
* The configuration of the route
*/
routeConfig?: Route | null;
}

class ActivatedRouteSnapshotStub extends ActivatedRouteSnapshot {
Expand All @@ -187,6 +191,7 @@ class ActivatedRouteSnapshotStub extends ActivatedRouteSnapshot {
private _firstChild: ActivatedRouteSnapshot | null = null;
private _children: Array<ActivatedRouteSnapshot> = [];
private _pathFromRoot: Array<ActivatedRouteSnapshot> = [];
private _routeConfig: Route | null = null;

get parent(): ActivatedRouteSnapshot | null {
return this._parent;
Expand Down Expand Up @@ -228,6 +233,16 @@ class ActivatedRouteSnapshotStub extends ActivatedRouteSnapshot {
this._pathFromRoot = value;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
get routeConfig(): Route | null {
return this._routeConfig;
}

set routeConfig(route: Route | null) {
this._routeConfig = route;
}

constructor() {
super();
this._root = this;
Expand Down Expand Up @@ -286,6 +301,7 @@ export class ActivatedRouteStub extends ActivatedRoute {
snapshot.data = options?.data ?? {};
snapshot.fragment = options?.fragment ?? null;
snapshot.url = options?.url ?? [];
snapshot.routeConfig = options?.routeConfig ?? null;

snapshot.firstChild = this.firstChild?.snapshot ?? null;
snapshot.children = this.children?.map(route => route.snapshot) ?? [];
Expand Down Expand Up @@ -326,6 +342,10 @@ export class ActivatedRouteStub extends ActivatedRoute {
return this._children;
}

get routeConfig(): Route | null {
return this.snapshot.routeConfig;
}

/**
* Triggers a navigation with the given new parameters. All the other parts (query params etc.) stay as the are.
* This is a shortcut to `triggerNavigation` that can be used to only change the parameters.
Expand Down

0 comments on commit 768c113

Please sign in to comment.