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(routing): add support for ActivatedRoute url property #326

Merged
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,11 @@ interface SpectatorRouting<C> extends Spectator<C> {
* Updates the route fragment and triggers a route navigation.
*/
setRouteFragment(fragment: string | null): void;

/**
* Updates the route url and triggers a route navigation.
*/
setRouteUrl(url: UrlSegment[]): void;
}
```

Expand Down Expand Up @@ -616,6 +621,7 @@ The `createRoutesFactory` function can take the following options, on top of the
* `queryParams`: initial query params to use in `ActivatedRoute` stub
* `data`: initial data to use in `ActivatedRoute` stub
* `fragment`: initial fragment to use in `ActivatedRoute` stub
* `url`: initial URL segments to use in `ActivatedRoute` stub
* `stubsEnabled` (default: `true`): enables the `ActivatedRoute` stub, if set to `false` it uses `RouterTestingModule` instead
* `routes`: if `stubsEnabled` is set to false, you can pass a `Routes` configuration for `RouterTestingModule`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertToParamMap, ActivatedRoute, ActivatedRouteSnapshot, Data, Params, ParamMap } from '@angular/router';
import { convertToParamMap, ActivatedRoute, ActivatedRouteSnapshot, Data, Params, ParamMap, UrlSegment } from '@angular/router';
import { Observable, ReplaySubject } from 'rxjs';
import { map } from 'rxjs/operators';

Expand All @@ -14,11 +14,13 @@ export class ActivatedRouteStub extends ActivatedRoute {
private testQueryParams: Params = {};
private testData: Data = {};
private testFragment: string | null = null;
private testUrl: UrlSegment[] = [];

private readonly paramsSubject = new ReplaySubject<Params>(1);
private readonly queryParamsSubject = new ReplaySubject<Params>(1);
private readonly dataSubject = new ReplaySubject<Data>(1);
private readonly fragmentSubject = new ReplaySubject<string | null>(1);
private readonly urlSubject = new ReplaySubject<UrlSegment[]>(1);

constructor(options?: RouteOptions) {
super();
Expand All @@ -28,12 +30,14 @@ export class ActivatedRouteStub extends ActivatedRoute {
this.testQueryParams = options.queryParams || {};
this.testData = options.data || {};
this.testFragment = options.fragment || null;
this.testUrl = options.url || [];
}

this.params = this.paramsSubject.asObservable();
this.queryParams = this.queryParamsSubject.asObservable();
this.data = this.dataSubject.asObservable();
this.fragment = this.fragmentSubject.asObservable() as Observable<string>;
this.url = this.urlSubject.asObservable() as Observable<UrlSegment[]>;

this.triggerNavigation();
}
Expand All @@ -49,6 +53,7 @@ export class ActivatedRouteStub extends ActivatedRoute {
snapshot.queryParams = this.testQueryParams;
snapshot.data = this.testData;
snapshot.fragment = this.testFragment!;
snapshot.url = this.testUrl;

return snapshot;
}
Expand Down Expand Up @@ -81,6 +86,10 @@ export class ActivatedRouteStub extends ActivatedRoute {
this.testFragment = fragment;
}

public setUrl(url: UrlSegment[]): void {
this.testUrl = url;
}

public get children(): ActivatedRouteStub[] {
return [this];
}
Expand All @@ -97,6 +106,7 @@ export class ActivatedRouteStub extends ActivatedRoute {
this.queryParamsSubject.next(this.testQueryParams);
this.dataSubject.next(this.testData);
this.fragmentSubject.next(this.testFragment);
this.urlSubject.next(this.testUrl);
}

public toString(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export function createRoutingFactory<C>(typeOrOptions: Type<C> | SpectatorRoutin
});
}

const { params, queryParams, data, fragment } = { ...options, ...overrides };
const { params, queryParams, data, fragment, url } = { ...options, ...overrides };

TestBed.overrideProvider(ActivatedRoute, {
useValue: new ActivatedRouteStub({ params, queryParams, data, fragment })
useValue: new ActivatedRouteStub({ params, queryParams, data, fragment, url })
});

const spectator = createSpectatorRouting(options, props);
Expand Down
3 changes: 2 additions & 1 deletion projects/spectator/src/lib/spectator-routing/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const defaultRoutingOptions: OptionalsRequired<SpectatorRoutingOptions<any>> = {
fragment: null,
mockRouterLinks: true,
stubsEnabled: true,
routes: []
routes: [],
url: []
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Data, Params } from '@angular/router';
import { Data, Params, UrlSegment } from '@angular/router';

export interface RouteOptions {
params?: Params;
queryParams?: Params;
data?: Data;
fragment?: string | null;
url?: UrlSegment[];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DebugElement } from '@angular/core';
import { ComponentFixture } from '@angular/core/testing';
import { Event, Router } from '@angular/router';
import { Event, Router, UrlSegment } from '@angular/router';

import { Spectator } from '../spectator/spectator';

Expand Down Expand Up @@ -89,6 +89,16 @@ export class SpectatorRouting<C> extends Spectator<C> {
}
}

/**
* Updates the route url and triggers a route navigation.
*/
public setRouteUrl(url: UrlSegment[]): void {
if (this.checkStubPresent()) {
this.activatedRouteStub.setUrl(url);
this.triggerNavigationAndUpdate();
}
}

/**
* Emits a router event
*/
Expand Down
13 changes: 11 additions & 2 deletions projects/spectator/test/with-routing/my-page.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NavigationStart, Router, RouterLink } from '@angular/router';
import { NavigationStart, Router, RouterLink, UrlSegment } from '@angular/router';
import { createRoutingFactory } from '@ngneat/spectator';
import { Component } from '@angular/core';
import { Location } from '@angular/common';
Expand All @@ -17,11 +17,13 @@ describe('MyPageComponent', () => {
});

describe('route options', () => {
const url = [new UrlSegment('/url-path', {})];
const createComponent = createRoutingFactory({
component: MyPageComponent,
data: { title: 'lorem', dynamicTitle: 'ipsum' },
params: { foo: '1', bar: '2' },
queryParams: { baz: '3' }
queryParams: { baz: '3' },
url: url
});

it('should create with default options', () => {
Expand All @@ -33,6 +35,8 @@ describe('MyPageComponent', () => {
expect(spectator.query('.foo')).toHaveText('1');
expect(spectator.query('.bar')).toHaveText('2');
expect(spectator.query('.baz')).toHaveText('3');

expect(spectator.component.url).toEqual(url);
});

it('should create with overridden options', () => {
Expand Down Expand Up @@ -68,6 +72,11 @@ describe('MyPageComponent', () => {
expect(spectator.query('.bar')).toHaveText('X');
expect(spectator.query('.baz')).toHaveText('Y');
expect(spectator.component.fragment).toBe('lorem');

const url = [new UrlSegment('/url-path', {})];
spectator.setRouteUrl(url);

expect(spectator.component.url).toEqual(url);
});

it('should support snapshot data', () => {
Expand Down
4 changes: 3 additions & 1 deletion projects/spectator/test/with-routing/my-page.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ActivatedRoute, Router, UrlSegment } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

Expand All @@ -22,6 +22,7 @@ export class MyPageComponent implements OnInit {
public bar?: string;
public baz$!: Observable<string>;
public fragment!: string | null;
public url?: UrlSegment[];

constructor(private readonly route: ActivatedRoute, private readonly router: Router) {}

Expand All @@ -33,6 +34,7 @@ export class MyPageComponent implements OnInit {
this.route.paramMap.subscribe(params => (this.bar = params.get('bar')!));
this.baz$ = this.route.queryParams.pipe(map(params => params.baz));
this.route.fragment.subscribe(fragment => (this.fragment = fragment));
this.route.url.subscribe(url => (this.url = url));
}

public navigate(): void {
Expand Down