Skip to content

Commit 14553f6

Browse files
feat(router-store): Add urlAfterRedirects (#2775)
1 parent d1873c9 commit 14553f6

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

modules/router-store/spec/router_store_module.spec.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TestBed } from '@angular/core/testing';
2-
import { Router, RouterEvent } from '@angular/router';
2+
import { Router, RouterEvent, NavigationEnd } from '@angular/router';
33
import {
44
routerReducer,
55
RouterReducerState,
@@ -11,7 +11,7 @@ import {
1111
DefaultRouterStateSerializer,
1212
} from '@ngrx/router-store';
1313
import { select, Store, ActionsSubject } from '@ngrx/store';
14-
import { withLatestFrom, filter } from 'rxjs/operators';
14+
import { withLatestFrom, filter, skip } from 'rxjs/operators';
1515

1616
import { createTestModule } from './utils';
1717

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

157157
describe('Full', () => {
158-
it('should dispatch the full event', async () => {
158+
it('should dispatch the full event', async (done) => {
159159
const { actions, router } = setup(RouterState.Full);
160-
actions
161-
.pipe(filter(onlyRouterActions))
162-
.subscribe(({ payload }) =>
163-
expect(payload.event instanceof RouterEvent).toBe(true)
164-
);
160+
actions.pipe(filter(onlyRouterActions)).subscribe(({ payload }) => {
161+
expect(payload.event instanceof RouterEvent).toBe(true);
162+
done();
163+
});
165164

166165
await router.navigateByUrl('/');
167166
});
@@ -191,18 +190,43 @@ describe('Router Store Module', () => {
191190
});
192191

193192
describe('Minimal', () => {
194-
it('should dispatch the navigation id with url', async () => {
193+
it('should dispatch the navigation id with url', async (done) => {
195194
const { actions, router } = setup(RouterState.Minimal);
196195
actions
197196
.pipe(filter(onlyRouterActions))
198197
.subscribe(({ payload }: any) => {
199198
expect(payload.event instanceof RouterEvent).toBe(false);
200199
expect(payload.event).toEqual({ id: 1, url: '/' });
200+
done();
201201
});
202202

203203
await router.navigateByUrl('/');
204204
});
205205

206+
it('should dispatch the navigation with urlAfterRedirects', async (done) => {
207+
const { actions, router } = setup(RouterState.Minimal);
208+
actions
209+
.pipe(
210+
filter(onlyRouterActions),
211+
// wait until NavigationEnd router event
212+
filter(
213+
({ payload }) =>
214+
!!(payload.event as NavigationEnd).urlAfterRedirects
215+
)
216+
)
217+
.subscribe(({ payload }: any) => {
218+
expect(payload.event instanceof RouterEvent).toBe(false);
219+
expect(payload.event).toEqual({
220+
id: 1,
221+
url: '/redirect',
222+
urlAfterRedirects: '/next',
223+
});
224+
done();
225+
});
226+
227+
await router.navigateByUrl('/redirect');
228+
});
229+
206230
it('should use the minimal router serializer', () => {
207231
const { serializer } = setup(RouterState.Minimal);
208232
expect(serializer).toEqual(new MinimalRouterStateSerializer());

modules/router-store/spec/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export function createTestModule(
4242
loadChildren: 'test',
4343
canLoad: ['CanLoadNext'],
4444
},
45+
{
46+
path: 'redirect',
47+
pathMatch: 'full',
48+
redirectTo: 'next',
49+
},
4550
]),
4651
StoreRouterConnectingModule.forRoot(opts.config),
4752
],

modules/router-store/src/router_store_module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,13 @@ export class StoreRouterConnectingModule {
327327
event:
328328
this.config.routerState === RouterState.Full
329329
? payload.event
330-
: { id: payload.event.id, url: payload.event.url },
330+
: {
331+
id: payload.event.id,
332+
url: payload.event.url,
333+
// safe, as it will just be `undefined` for non-NavigationEnd router events
334+
urlAfterRedirects: (payload.event as NavigationEnd)
335+
.urlAfterRedirects,
336+
},
331337
},
332338
});
333339
} finally {

0 commit comments

Comments
 (0)