Skip to content

Commit d3b26be

Browse files
committed
test: Progress, though still incomplete
1 parent eeb2c57 commit d3b26be

File tree

1 file changed

+47
-78
lines changed

1 file changed

+47
-78
lines changed

test/router.test.js

+47-78
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ describe('Router', () => {
5050
scratch
5151
);
5252

53-
loc.route('/a/');
53+
navigation.navigate('/a/');
54+
5455
await sleep(1);
5556

5657
expect(loc).to.deep.include({
@@ -154,7 +155,7 @@ describe('Router', () => {
154155
});
155156

156157
Home.resetHistory();
157-
loc.route('/profiles');
158+
navigation.navigate('/profiles');
158159
await sleep(1);
159160

160161
expect(scratch).to.have.property('textContent', 'Profiles');
@@ -170,7 +171,7 @@ describe('Router', () => {
170171
});
171172

172173
Profiles.resetHistory();
173-
loc.route('/profiles/bob');
174+
navigation.navigate('/profiles/bob');
174175
await sleep(1);
175176

176177
expect(scratch).to.have.property('textContent', 'Profile: bob');
@@ -188,7 +189,7 @@ describe('Router', () => {
188189
});
189190

190191
Profile.resetHistory();
191-
loc.route('/other?a=b&c=d');
192+
navigation.navigate('/other?a=b&c=d');
192193
await sleep(1);
193194

194195
expect(scratch).to.have.property('textContent', 'Fallback');
@@ -242,7 +243,7 @@ describe('Router', () => {
242243
expect(A).to.have.been.calledWith({ path: '/', searchParams: {}, pathParams: {} });
243244

244245
A.resetHistory();
245-
loc.route('/b');
246+
navigation.navigate('/b');
246247

247248
expect(scratch).to.have.property('innerHTML', '<h1>A</h1><p>hello</p>');
248249
expect(A).not.to.have.been.called;
@@ -263,18 +264,18 @@ describe('Router', () => {
263264
expect(B).to.have.been.calledWith({ path: '/b', searchParams: {}, pathParams: {} });
264265

265266
B.resetHistory();
266-
loc.route('/c');
267-
loc.route('/c?1');
268-
loc.route('/c');
267+
navigation.navigate('/c');
268+
navigation.navigate('/c?1');
269+
navigation.navigate('/c');
269270

270271
expect(scratch).to.have.property('innerHTML', '<h1>B</h1><p>hello</p>');
271272
expect(B).not.to.have.been.called;
272273

273274
await sleep(1);
274275

275-
loc.route('/c');
276-
loc.route('/c?2');
277-
loc.route('/c');
276+
navigation.navigate('/c');
277+
navigation.navigate('/c?2');
278+
navigation.navigate('/c');
278279

279280
expect(scratch).to.have.property('innerHTML', '<h1>B</h1><p>hello</p>');
280281
// We should never re-invoke <B /> while loading <C /> (that would be a remount of the old route):
@@ -293,7 +294,7 @@ describe('Router', () => {
293294

294295
C.resetHistory();
295296
B.resetHistory();
296-
loc.route('/b');
297+
navigation.navigate('/b');
297298
await sleep(1);
298299

299300
expect(scratch).to.have.property('innerHTML', '<h1>B</h1><p>hello</p>');
@@ -303,7 +304,7 @@ describe('Router', () => {
303304

304305
A.resetHistory();
305306
B.resetHistory();
306-
loc.route('/');
307+
navigation.navigate('/');
307308
await sleep(1);
308309

309310
expect(scratch).to.have.property('innerHTML', '<h1>A</h1><p>hello</p>');
@@ -347,21 +348,21 @@ describe('Router', () => {
347348
expect(renderRefCount).to.equal(2);
348349

349350
renderRefCount = 0;
350-
loc.route('/b/a');
351+
navigation.navigate('/b/a');
351352
await sleep(10);
352353

353354
expect(scratch).to.have.property('innerHTML', '<h1>b/a</h1>');
354355
expect(renderRefCount).to.equal(4);
355356

356357
renderRefCount = 0;
357-
loc.route('/b/b');
358+
navigation.navigate('/b/b');
358359
await sleep(10);
359360

360361
expect(scratch).to.have.property('innerHTML', '<h1>b/b</h1>');
361362
expect(renderRefCount).to.equal(1);
362363

363364
renderRefCount = 0;
364-
loc.route('/');
365+
navigation.navigate('/');
365366
await sleep(10);
366367

367368
expect(scratch).to.have.property('innerHTML', '<h1>a</h1>');
@@ -451,7 +452,8 @@ describe('Router', () => {
451452
loadEnd.resetHistory();
452453
routeChange.resetHistory();
453454

454-
loc.route('/b');
455+
navigation.navigate('/b');
456+
455457
await sleep(1);
456458

457459
expect(loadStart).to.have.been.calledWith('/b');
@@ -508,7 +510,7 @@ describe('Router', () => {
508510
expect(loadEnd).not.to.have.been.called;
509511
});
510512

511-
describe('intercepted VS external links', () => {
513+
describe.only('intercepted VS external links', () => {
512514
const shouldIntercept = [null, '', '_self', 'self', '_SELF'];
513515
const shouldNavigate = ['_top', '_parent', '_blank', 'custom', '_BLANK'];
514516

@@ -588,8 +590,6 @@ describe('Router', () => {
588590
const shouldIntercept = ['/app', '/app/deeper'];
589591
const shouldNavigate = ['/site', '/site/deeper'];
590592

591-
const clickHandler = sinon.fake(e => e.preventDefault());
592-
593593
const Links = () => (
594594
<>
595595
<a href="/app">Internal Link</a>
@@ -599,23 +599,6 @@ describe('Router', () => {
599599
</>
600600
);
601601

602-
let pushState;
603-
604-
before(() => {
605-
pushState = sinon.spy(history, 'pushState');
606-
addEventListener('click', clickHandler);
607-
});
608-
609-
after(() => {
610-
pushState.restore();
611-
removeEventListener('click', clickHandler);
612-
});
613-
614-
beforeEach(async () => {
615-
clickHandler.resetHistory();
616-
pushState.resetHistory();
617-
});
618-
619602
it('should intercept clicks on links matching the `scope` props (string)', async () => {
620603
render(
621604
<LocationProvider scope="/app">
@@ -629,15 +612,10 @@ describe('Router', () => {
629612
scratch.querySelector(`a[href="${url}"]`).click();
630613
await sleep(1);
631614
expect(loc).to.deep.include({ url });
632-
expect(pushState).to.have.been.calledWith(null, '', url);
633-
expect(clickHandler).to.have.been.called;
634-
635-
pushState.resetHistory();
636-
clickHandler.resetHistory();
637615
}
638616
});
639617

640-
it('should allow default browser navigation for links not matching the `scope` props (string)', async () => {
618+
it.skip('should allow default browser navigation for links not matching the `scope` props (string)', async () => {
641619
render(
642620
<LocationProvider scope="app">
643621
<Links />
@@ -649,11 +627,8 @@ describe('Router', () => {
649627
for (const url of shouldNavigate) {
650628
scratch.querySelector(`a[href="${url}"]`).click();
651629
await sleep(1);
652-
expect(pushState).not.to.have.been.called;
653-
expect(clickHandler).to.have.been.called;
654630

655-
pushState.resetHistory();
656-
clickHandler.resetHistory();
631+
// TODO: How to test this?
657632
}
658633
});
659634

@@ -670,15 +645,10 @@ describe('Router', () => {
670645
scratch.querySelector(`a[href="${url}"]`).click();
671646
await sleep(1);
672647
expect(loc).to.deep.include({ url });
673-
expect(pushState).to.have.been.calledWith(null, '', url);
674-
expect(clickHandler).to.have.been.called;
675-
676-
pushState.resetHistory();
677-
clickHandler.resetHistory();
678648
}
679649
});
680650

681-
it('should allow default browser navigation for links not matching the `scope` props (regex)', async () => {
651+
it.skip('should allow default browser navigation for links not matching the `scope` props (regex)', async () => {
682652
render(
683653
<LocationProvider scope={/^\/app/}>
684654
<Links />
@@ -690,19 +660,23 @@ describe('Router', () => {
690660
for (const url of shouldNavigate) {
691661
scratch.querySelector(`a[href="${url}"]`).click();
692662
await sleep(1);
693-
expect(pushState).not.to.have.been.called;
694-
expect(clickHandler).to.have.been.called;
695663

696-
pushState.resetHistory();
697-
clickHandler.resetHistory();
664+
// TODO: How to test this?
698665
}
699666
});
700667
});
701668

702669
it('should scroll to top when navigating forward', async () => {
703670
const scrollTo = sinon.spy(window, 'scrollTo');
704671

705-
const Route = sinon.fake(() => <div style={{ height: '1000px' }}><a href="/link">link</a></div>);
672+
const Route = sinon.fake(
673+
() => (
674+
<div style={{ height: '1000px' }}>
675+
<a href="/link">link</a>
676+
</div>
677+
)
678+
);
679+
706680
render(
707681
<LocationProvider>
708682
<Router>
@@ -717,7 +691,7 @@ describe('Router', () => {
717691
expect(Route).to.have.been.calledOnce;
718692
Route.resetHistory();
719693

720-
loc.route('/programmatic');
694+
navigation.navigate('/programmatic');
721695
await sleep(1);
722696

723697
expect(loc).to.deep.include({ url: '/programmatic' });
@@ -740,14 +714,13 @@ describe('Router', () => {
740714
});
741715

742716
it('should ignore clicks on document fragment links', async () => {
743-
const pushState = sinon.spy(history, 'pushState');
744-
745717
const Route = sinon.fake(
746718
() => <div>
747719
<a href="#foo">just #foo</a>
748720
<a href="/other#bar">other #bar</a>
749721
</div>
750722
);
723+
751724
render(
752725
<LocationProvider>
753726
<Router>
@@ -760,7 +733,6 @@ describe('Router', () => {
760733
scratch
761734
);
762735

763-
expect(Route).to.have.been.calledOnce;
764736
Route.resetHistory();
765737

766738
scratch.querySelector('a[href="#foo"]').click();
@@ -769,22 +741,17 @@ describe('Router', () => {
769741
// NOTE: we don't (currently) propagate in-page anchor navigations into context, to avoid useless renders.
770742
expect(loc).to.deep.include({ url: '/' });
771743
expect(Route).not.to.have.been.called;
772-
expect(pushState).not.to.have.been.called;
773744
expect(location.hash).to.equal('#foo');
774745

775746
scratch.querySelector('a[href="/other#bar"]').click();
776747
await sleep(1);
777748

778749
expect(Route).to.have.been.calledOnce;
779750
expect(loc).to.deep.include({ url: '/other#bar', path: '/other' });
780-
expect(pushState).to.have.been.called;
781751
expect(location.hash).to.equal('#bar');
782-
783-
pushState.restore();
784752
});
785753

786754
it('should normalize children', async () => {
787-
const pushState = sinon.spy(history, 'pushState');
788755
const Route = sinon.fake(() => <a href="/foo#foo">foo</a>);
789756

790757
const routes = ['/foo', '/bar'];
@@ -807,9 +774,6 @@ describe('Router', () => {
807774

808775
expect(Route).to.have.been.calledOnce;
809776
expect(loc).to.deep.include({ url: '/foo#foo', path: '/foo' });
810-
expect(pushState).to.have.been.called;
811-
812-
pushState.restore();
813777
});
814778

815779
it('should match nested routes', async () => {
@@ -865,25 +829,30 @@ describe('Router', () => {
865829
});
866830

867831
it('should replace the current URL', async () => {
868-
const pushState = sinon.spy(history, 'pushState');
869-
const replaceState = sinon.spy(history, 'replaceState');
870-
871832
render(
872833
<LocationProvider>
873834
<Router>
835+
<Route path="/" component={() => null} />
874836
<Route path="/foo" component={() => null} />
837+
<Route path="/bar" component={() => null} />
875838
</Router>
876839
<ShallowLocation />
877840
</LocationProvider>,
878841
scratch
879842
);
880843

881-
loc.route("/foo", true);
882-
expect(pushState).not.to.have.been.called;
883-
expect(replaceState).to.have.been.calledWith(null, "", "/foo");
844+
navigation.navigate('/foo');
845+
navigation.navigate('/bar', { history: 'replace' });
846+
847+
const entries = navigation.entries();
848+
849+
// Top of the stack
850+
const last = new URL(entries[entries.length - 1].url);
851+
expect(last.pathname).to.equal('/bar');
884852

885-
pushState.restore();
886-
replaceState.restore();
853+
// Entry before
854+
const secondLast = new URL(entries[entries.length - 2].url);
855+
expect(secondLast.pathname).to.equal('/');
887856
});
888857
});
889858

0 commit comments

Comments
 (0)