Skip to content

Commit

Permalink
Merge pull request #179 from namecheap/feature/correct_root_route_han…
Browse files Browse the repository at this point in the history
…dling

fix: fixed links handling which lead to "/"
  • Loading branch information
StyleT authored Jun 18, 2020
2 parents 1c654aa + 2e9bd6d commit 42ac734
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ilc/client/ClientRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};
Expand Down
42 changes: 38 additions & 4 deletions ilc/client/ClientRouter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
];

Expand Down Expand Up @@ -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,
Expand All @@ -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`
<a id="${anchor.id}" href="${anchor.href}">
Hi there! I am anchor tag and I have href attribute.
So I should forward you to registered micro front-end page.
</a>
`;

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',
};
Expand All @@ -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,
Expand All @@ -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',
Expand Down

0 comments on commit 42ac734

Please sign in to comment.