From 2e9bd6dc009a074fe283d0cddf0ef104454d5693 Mon Sep 17 00:00:00 2001 From: Vladlen Fedosov Date: Wed, 17 Jun 2020 18:51:47 +0300 Subject: [PATCH] fix: fixed link handling which lead to "/" --- ilc/client/ClientRouter.js | 2 +- ilc/client/ClientRouter.spec.js | 42 +++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/ilc/client/ClientRouter.js b/ilc/client/ClientRouter.js index 4b706fd4..d6abf58a 100644 --- a/ilc/client/ClientRouter.js +++ b/ilc/client/ClientRouter.js @@ -123,7 +123,7 @@ export default class ClientRouter { const {specialRole} = this.#router.match(pathname); if (specialRole === null) { - this.#navigateToUrl(pathname); + this.#navigateToUrl(href); event.preventDefault(); } }; diff --git a/ilc/client/ClientRouter.spec.js b/ilc/client/ClientRouter.spec.js index 7fcb31d7..a7d1be4e 100644 --- a/ilc/client/ClientRouter.spec.js +++ b/ilc/client/ClientRouter.spec.js @@ -112,6 +112,20 @@ describe('client router', () => { appName: apps['@portal/hero'].name, }, }, + }, + { + routeId: 'rootRoute', + route: '/', + next: false, + template: 'baseTemplate', + slots: { + opponent: { + appName: apps['@portal/opponent'].name, + }, + hero: { + appName: apps['@portal/hero'].name, + }, + }, } ]; @@ -381,7 +395,7 @@ describe('client router', () => { singleSpa.navigateToUrl.resetHistory(); }); - it('should prevent click events on anchors and navigate to a registered micro front-end page', () => { + it('should handle click events on anchors and navigate to a registered micro front-end page', () => { const anchor = { id: 'click-me', href: registryConfig.routes[2].route, @@ -401,7 +415,27 @@ describe('client router', () => { chai.expect(clickEvent.defaultPrevented).to.be.true; }); - it('should not prevent click events on anchors when anchors do not have href attribute', () => { + it('should handle click events on "http(s)://domain" anchors if we have "/" route', () => { + const anchor = { + id: 'click-me', + href: location.origin, + }; + + anchor.ref = html` + + Hi there! I am anchor tag and I have href attribute. + So I should forward you to registered micro front-end page. + + `; + + document.body.appendChild(anchor.ref); + document.getElementById(anchor.id).dispatchEvent(clickEvent); + + chai.expect(singleSpa.navigateToUrl.calledOnceWithExactly(anchor.href)).to.be.true; + chai.expect(clickEvent.defaultPrevented).to.be.true; + }); + + it('should NOT handle click events on anchors when anchors do not have href attribute', () => { const anchor = { id: 'click-me', }; @@ -420,7 +454,7 @@ describe('client router', () => { chai.expect(clickEvent.defaultPrevented).to.be.false; }); - it('should not prevent click events on anchors when these events were already default prevented', () => { + it('should NOT handle click events on anchors when these events were already default prevented', () => { const anchor = { id: 'click-me', href: registryConfig.routes[2].route, @@ -443,7 +477,7 @@ describe('client router', () => { chai.expect(singleSpa.navigateToUrl.called).to.be.false; }); - it('should not prevent click events on anchors when anchors are not going to a registered micro front-end page', () => { + it('should NOT handle click events on anchors when anchors are not going to a registered micro front-end page', () => { const anchor = { id: 'click-me', href: '/undefined',