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

fix(router-store): ignore trailing slash when comparing routes #2834

Merged
merged 1 commit into from
Dec 22, 2020
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
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;
}