Skip to content

Commit 84f9cf3

Browse files
viacheslafffahad19
authored andcommitted
frint-router-react: Link: avoid pushing url if its current one (#246)
1 parent b88c8a0 commit 84f9cf3

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

packages/frint-router-react/src/Link.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ export default class Link extends React.Component {
7171
handleClick = (e) => {
7272
e.preventDefault();
7373

74-
// TODO avoid pushing route if it's current URL
75-
this.context.app
76-
.get('router')
77-
.push(this.props.to);
74+
const router = this.context.app.get('router');
75+
const to = this.props.to;
76+
77+
if (router.getMatch(to, router.getHistory(), { exact: true }) === null) {
78+
router.push(to);
79+
}
7880
};
7981

8082
render() {

packages/frint-router-react/src/Link.spec.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,22 @@ describe('frint-route-react › Link', () => {
236236
expect(unsubscribeCount).to.equal(2);
237237
});
238238

239-
it('pushes new url to router, prevents default and doesn\'t stopPropagation when clicked', function () {
239+
describe('prevents default and doesn\'t stopPropagation when clicked ', function () {
240240
const pushedUrls = [];
241+
const actualRouter = new MemoryRouterService();
241242

242243
const router = {
243244
push(url) {
245+
actualRouter.push(url);
244246
pushedUrls.push(url);
247+
},
248+
249+
getHistory() {
250+
return actualRouter.getHistory();
251+
},
252+
253+
getMatch(...args) {
254+
return actualRouter.getMatch.call(actualRouter, ...args);
245255
}
246256
};
247257

@@ -250,15 +260,32 @@ describe('frint-route-react › Link', () => {
250260
{ context: createContextWithRouter(router) }
251261
);
252262

253-
let preventDefaultCalled = false;
254-
let stopPropagationCalled = false;
255-
wrapper.simulate('click', {
256-
preventDefault() { preventDefaultCalled = true; },
257-
stopPropagation() { stopPropagationCalled = true; }
263+
it('and pushes new url to router when it doesn\'t match current url', function () {
264+
let preventDefaultCalled = 0;
265+
let stopPropagationCalled = 0;
266+
wrapper.simulate('click', {
267+
preventDefault() { preventDefaultCalled += 1; },
268+
stopPropagation() { stopPropagationCalled += 1; }
269+
});
270+
271+
expect(pushedUrls).to.deep.equal(['/about']);
272+
expect(preventDefaultCalled).to.equal(1);
273+
expect(stopPropagationCalled).to.equal(0);
258274
});
259275

260-
expect(pushedUrls).to.deep.equal(['/about']);
261-
expect(preventDefaultCalled).to.be.true;
262-
expect(stopPropagationCalled).to.be.false;
276+
277+
it('and doesn\'t push new url to router when it matches current url', function () {
278+
let preventDefaultCalled = 0;
279+
let stopPropagationCalled = 0;
280+
wrapper.simulate('click', {
281+
preventDefault() { preventDefaultCalled += 1; },
282+
stopPropagation() { stopPropagationCalled += 1; }
283+
});
284+
285+
// nothing new was added to the pushedUrls and that's the point
286+
expect(pushedUrls).to.deep.equal(['/about']);
287+
expect(preventDefaultCalled).to.equal(1);
288+
expect(stopPropagationCalled).to.equal(0);
289+
});
263290
});
264291
});

0 commit comments

Comments
 (0)