Skip to content

Commit

Permalink
fix(router-store): ignore trailing slash when comparing routes (#2834)
Browse files Browse the repository at this point in the history
Closes #2829, #1781
  • Loading branch information
alex-okrushko authored and brandonroberts committed Dec 22, 2020
1 parent 471f74b commit 1086de4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
29 changes: 28 additions & 1 deletion modules/router-store/spec/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ROUTER_NAVIGATED,
ROUTER_NAVIGATION,
ROUTER_REQUEST,
routerNavigationAction,
RouterAction,
routerReducer,
RouterReducerState,
Expand Down Expand Up @@ -169,6 +170,32 @@ describe('integration spec', () => {
});
});

it('should ignore routing actions for the URL that is currently open', async () => {
createTestModule({
reducers: { router: routerReducer },
});

const router = TestBed.inject(Router);
const store = TestBed.inject(Store);
const navigateByUrlSpy = jest.spyOn(router, 'navigateByUrl');

await router.navigateByUrl('/');

const SAME_URL_WITHOUT_SLASH = '';

store.dispatch(
routerNavigationAction({
payload: {
routerState: { url: SAME_URL_WITHOUT_SLASH, root: {} as any },
event: { id: 123 } as any,
},
})
);

// Navigates only ONCE 👇
expect(navigateByUrlSpy.mock.calls.length).toBe(1);
});

it('should support rolling back if navigation gets canceled (navigation initialized through router)', (done: any) => {
const reducer = (state: string = '', action: RouterAction<any>): any => {
if (action.type === ROUTER_NAVIGATION) {
Expand Down Expand Up @@ -573,7 +600,7 @@ describe('integration spec', () => {

expect(log).toEqual([
{ type: 'store', state: null }, // initial state
{ type: 'store', state: null }, // ROUTER_REQEST event in the store
{ type: 'store', state: null }, // ROUTER_REQUEST event in the store
{ type: 'action', action: ROUTER_REQUEST },
{ type: 'router', event: 'NavigationStart', url: '/load' },
{ type: 'store', state: { url: '', navigationId: 1 } },
Expand Down
16 changes: 15 additions & 1 deletion modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export class StoreRouterConnectingModule {
}

const url = routerStoreState.state.url;
if (this.router.url !== url) {
if (!isSameUrl(this.router.url, url)) {
this.storeState = storeState;
this.trigger = RouterTrigger.STORE;
this.router.navigateByUrl(url).catch((error) => {
Expand Down Expand Up @@ -347,3 +347,17 @@ export class StoreRouterConnectingModule {
this.routerState = null;
}
}

/**
* Check if the URLs are matching. Accounts for the possibility of trailing "/" in url.
*/
function isSameUrl(first: string, second: string): boolean {
return stripTrailingSlash(first) === stripTrailingSlash(second);
}

function stripTrailingSlash(text: string): string {
if (text.length > 0 && text[text.length - 1] === '/') {
return text.substring(0, text.length - 1);
}
return text;
}

0 comments on commit 1086de4

Please sign in to comment.