From 87d07a424ded23870d260b681edfede2b2199951 Mon Sep 17 00:00:00 2001 From: Krzysztof Cislo Date: Mon, 22 Jun 2020 14:52:50 +0200 Subject: [PATCH] - Removed navigation param NAVIGATION_CANCELLED_EVENT as it was not used - Removed candidate on CANCEL_EVENT to stop propagation to NAVIGATE_EVENT (to allow to cancel navigation in PRE_NAVIGATE_EVENT) - Added test for the new scenario --- index.spec.ts | 22 ++++++++++++++++++++-- index.ts | 16 ++++++++-------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/index.spec.ts b/index.spec.ts index 080b8fb..be04aa1 100644 --- a/index.spec.ts +++ b/index.spec.ts @@ -4,7 +4,7 @@ import { routingModule, navigate, cancelNavigation, - StateWithRouting, RoutingEvents + StateWithRouting, RoutingEvents, PRE_NAVIGATE_EVENT } from './index'; import * as sinon from 'sinon'; import { expect, use } from 'chai'; @@ -141,6 +141,24 @@ describe(`simple scenarions`, () => { }); + it('Router should allow to cancel navigation in PRE_NAVIGATE_EVENT', async () => { + // eslint-disable-next-line @typescript-eslint/no-empty-function + onNavigate(store, '/a', () => {}); + onNavigate(store, '/b', () => {}); + + store.on(PRE_NAVIGATE_EVENT,(_, {navigation}) => { + if(navigation.url === '/b') { + cancelNavigation(store); + } + }) + + await navigate(store, '/a'); + await navigate(store, '/b'); + + expect(store.get().routing.current.url).eq('/a'); + expect(store.get().routing.current.route).eq('/a'); + }); + it('Router should allows to cancel async navigation', async () => { let continueA: () => void; // eslint-disable-next-line @typescript-eslint/no-empty-function @@ -193,6 +211,6 @@ describe(`simple scenarions`, () => { onNavigate(store, '/a/(?.*)', spy); await navigate(store, '/a/test'); expect(store.get().routing.current.params).eql({page: 'test', 0: 'test'}); - }) + }); }); diff --git a/index.ts b/index.ts index 663e666..a9dab37 100644 --- a/index.ts +++ b/index.ts @@ -136,7 +136,7 @@ export interface RoutingEvents { [NAVIGATE_EVENT]: NavigationEvent; [NAVIGATION_ENDED_EVENT]: {navigation: NavigationState}; [NAVIGATION_FAILED_EVENT]: {navigation: Navigation; error: any }; - [NAVIGATION_CANCELLED_EVENT]: NavigationEvent; + [NAVIGATION_CANCELLED_EVENT]: undefined; [NAVIGATION_IGNORED_EVENT]: NavigationEvent; [POST_NAVIGATE_EVENT]: {navigation: Navigation; error?: any }; [CANCEL_EVENT]: undefined; @@ -157,7 +157,7 @@ const ignoreNavigation = (navigation: Navigation, {current, next}: StateWithRout * const store = createStore([asyncRoutingModule, your_module1 ...]); */ export const routingModule = (store: StoreonStore) => { - + const dispatch = store.dispatch.bind(store); const on = store.on.bind(store); @@ -199,7 +199,7 @@ export const routingModule = (store: StoreonStore { if (routing.next) { - dispatch(NAVIGATION_CANCELLED_EVENT, {navigation: routing.next}) + dispatch(NAVIGATION_CANCELLED_EVENT) } }); @@ -271,10 +271,10 @@ export const routingModule = (store: StoreonStore ({routing : { ...routing, next: undefined }})); - on(NAVIGATION_FAILED_EVENT, ({ routing }) => ({routing : { ...routing, next: undefined }})); + on(NAVIGATION_CANCELLED_EVENT, ({ routing }) => ({routing : { ...routing, candidate: undefined, next: undefined }})); + on(NAVIGATION_FAILED_EVENT, ({ routing }) => ({routing : { ...routing, candidate: undefined, next: undefined }})); on(NAVIGATION_ENDED_EVENT, ({ routing }, {navigation}) => - ({routing : { ...routing, next: undefined, current: navigation }})); + ({routing : { ...routing, candidate: undefined, next: undefined, current: navigation }})); // binding events to close promise on(NAVIGATION_IGNORED_EVENT, (s, e) => dispatch(POST_NAVIGATE_EVENT, e)); @@ -289,8 +289,8 @@ export const routingModule = (store: StoreonStore { /* istanbul ignore else */ - if (routing.next) { - dispatch(NAVIGATION_CANCELLED_EVENT, {navigation: routing.next }) + if (routing.next || routing.candidate) { + dispatch(NAVIGATION_CANCELLED_EVENT) } }); };