Skip to content

Commit

Permalink
feat(router-store): Add urlAfterRedirects (#2775)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-okrushko authored and brandonroberts committed Nov 25, 2020
1 parent 7591b82 commit 45ee1c2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
42 changes: 33 additions & 9 deletions modules/router-store/spec/router_store_module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing';
import { Router, RouterEvent } from '@angular/router';
import { Router, RouterEvent, NavigationEnd } from '@angular/router';
import {
routerReducer,
RouterReducerState,
Expand All @@ -11,7 +11,7 @@ import {
DefaultRouterStateSerializer,
} from '@ngrx/router-store';
import { select, Store, ActionsSubject } from '@ngrx/store';
import { withLatestFrom, filter } from 'rxjs/operators';
import { withLatestFrom, filter, skip } from 'rxjs/operators';

import { createTestModule } from './utils';

Expand Down Expand Up @@ -155,13 +155,12 @@ describe('Router Store Module', () => {
a.payload && a.payload.event;

describe('Full', () => {
it('should dispatch the full event', async () => {
it('should dispatch the full event', async (done) => {
const { actions, router } = setup(RouterState.Full);
actions
.pipe(filter(onlyRouterActions))
.subscribe(({ payload }) =>
expect(payload.event instanceof RouterEvent).toBe(true)
);
actions.pipe(filter(onlyRouterActions)).subscribe(({ payload }) => {
expect(payload.event instanceof RouterEvent).toBe(true);
done();
});

await router.navigateByUrl('/');
});
Expand Down Expand Up @@ -191,18 +190,43 @@ describe('Router Store Module', () => {
});

describe('Minimal', () => {
it('should dispatch the navigation id with url', async () => {
it('should dispatch the navigation id with url', async (done) => {
const { actions, router } = setup(RouterState.Minimal);
actions
.pipe(filter(onlyRouterActions))
.subscribe(({ payload }: any) => {
expect(payload.event instanceof RouterEvent).toBe(false);
expect(payload.event).toEqual({ id: 1, url: '/' });
done();
});

await router.navigateByUrl('/');
});

it('should dispatch the navigation with urlAfterRedirects', async (done) => {
const { actions, router } = setup(RouterState.Minimal);
actions
.pipe(
filter(onlyRouterActions),
// wait until NavigationEnd router event
filter(
({ payload }) =>
!!(payload.event as NavigationEnd).urlAfterRedirects
)
)
.subscribe(({ payload }: any) => {
expect(payload.event instanceof RouterEvent).toBe(false);
expect(payload.event).toEqual({
id: 1,
url: '/redirect',
urlAfterRedirects: '/next',
});
done();
});

await router.navigateByUrl('/redirect');
});

it('should use the minimal router serializer', () => {
const { serializer } = setup(RouterState.Minimal);
expect(serializer).toEqual(new MinimalRouterStateSerializer());
Expand Down
5 changes: 5 additions & 0 deletions modules/router-store/spec/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export function createTestModule(
loadChildren: 'test',
canLoad: ['CanLoadNext'],
},
{
path: 'redirect',
pathMatch: 'full',
redirectTo: 'next',
},
]),
StoreRouterConnectingModule.forRoot(opts.config),
],
Expand Down
8 changes: 7 additions & 1 deletion modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,13 @@ export class StoreRouterConnectingModule {
event:
this.config.routerState === RouterState.Full
? payload.event
: { id: payload.event.id, url: payload.event.url },
: {
id: payload.event.id,
url: payload.event.url,
// safe, as it will just be `undefined` for non-NavigationEnd router events
urlAfterRedirects: (payload.event as NavigationEnd)
.urlAfterRedirects,
},
},
});
} finally {
Expand Down

0 comments on commit 45ee1c2

Please sign in to comment.