Skip to content

Commit 4f9a313

Browse files
committed
test: Progress, though still incomplete
1 parent 895b644 commit 4f9a313

File tree

1 file changed

+47
-78
lines changed

1 file changed

+47
-78
lines changed

test/router.test.js

Lines changed: 47 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ describe('Router', () => {
6666
scratch
6767
);
6868

69-
loc.route('/a/');
69+
navigation.navigate('/a/');
70+
7071
await sleep(1);
7172

7273
expect(loc).to.deep.include({
@@ -195,7 +196,7 @@ describe('Router', () => {
195196
});
196197

197198
Home.resetHistory();
198-
loc.route('/profiles');
199+
navigation.navigate('/profiles');
199200
await sleep(1);
200201

201202
expect(scratch).to.have.property('textContent', 'Profiles');
@@ -211,7 +212,7 @@ describe('Router', () => {
211212
});
212213

213214
Profiles.resetHistory();
214-
loc.route('/profiles/bob');
215+
navigation.navigate('/profiles/bob');
215216
await sleep(1);
216217

217218
expect(scratch).to.have.property('textContent', 'Profile: bob');
@@ -229,7 +230,7 @@ describe('Router', () => {
229230
});
230231

231232
Profile.resetHistory();
232-
loc.route('/other?a=b&c=d');
233+
navigation.navigate('/other?a=b&c=d');
233234
await sleep(1);
234235

235236
expect(scratch).to.have.property('textContent', 'Fallback');
@@ -283,7 +284,7 @@ describe('Router', () => {
283284
expect(A).to.have.been.calledWith({ path: '/', searchParams: {}, pathParams: {} });
284285

285286
A.resetHistory();
286-
loc.route('/b');
287+
navigation.navigate('/b');
287288

288289
expect(scratch).to.have.property('innerHTML', '<h1>A</h1><p>hello</p>');
289290
expect(A).not.to.have.been.called;
@@ -303,18 +304,18 @@ describe('Router', () => {
303304
expect(B).to.have.been.calledWith({ path: '/b', searchParams: {}, pathParams: {} });
304305

305306
B.resetHistory();
306-
loc.route('/c');
307-
loc.route('/c?1');
308-
loc.route('/c');
307+
navigation.navigate('/c');
308+
navigation.navigate('/c?1');
309+
navigation.navigate('/c');
309310

310311
expect(scratch).to.have.property('innerHTML', '<h1>B</h1><p>hello</p>');
311312
expect(B).not.to.have.been.called;
312313

313314
await sleep(1);
314315

315-
loc.route('/c');
316-
loc.route('/c?2');
317-
loc.route('/c');
316+
navigation.navigate('/c');
317+
navigation.navigate('/c?2');
318+
navigation.navigate('/c');
318319

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

333334
C.resetHistory();
334335
B.resetHistory();
335-
loc.route('/b');
336+
navigation.navigate('/b');
336337
await sleep(1);
337338

338339
expect(scratch).to.have.property('innerHTML', '<h1>B</h1><p>hello</p>');
@@ -342,7 +343,7 @@ describe('Router', () => {
342343

343344
A.resetHistory();
344345
B.resetHistory();
345-
loc.route('/');
346+
navigation.navigate('/');
346347
await sleep(1);
347348

348349
expect(scratch).to.have.property('innerHTML', '<h1>A</h1><p>hello</p>');
@@ -386,21 +387,21 @@ describe('Router', () => {
386387
expect(renderRefCount).to.equal(2);
387388

388389
renderRefCount = 0;
389-
loc.route('/b/a');
390+
navigation.navigate('/b/a');
390391
await sleep(10);
391392

392393
expect(scratch).to.have.property('innerHTML', '<h1>b/a</h1>');
393394
expect(renderRefCount).to.equal(4);
394395

395396
renderRefCount = 0;
396-
loc.route('/b/b');
397+
navigation.navigate('/b/b');
397398
await sleep(10);
398399

399400
expect(scratch).to.have.property('innerHTML', '<h1>b/b</h1>');
400401
expect(renderRefCount).to.equal(1);
401402

402403
renderRefCount = 0;
403-
loc.route('/');
404+
navigation.navigate('/');
404405
await sleep(10);
405406

406407
expect(scratch).to.have.property('innerHTML', '<h1>a</h1>');
@@ -490,7 +491,8 @@ describe('Router', () => {
490491
loadEnd.resetHistory();
491492
routeChange.resetHistory();
492493

493-
loc.route('/b');
494+
navigation.navigate('/b');
495+
494496
await sleep(1);
495497

496498
expect(loadStart).to.have.been.calledWith('/b');
@@ -547,7 +549,7 @@ describe('Router', () => {
547549
expect(loadEnd).not.to.have.been.called;
548550
});
549551

550-
describe('intercepted VS external links', () => {
552+
describe.only('intercepted VS external links', () => {
551553
const shouldIntercept = [null, '', '_self', 'self', '_SELF'];
552554
const shouldNavigate = ['_top', '_parent', '_blank', 'custom', '_BLANK'];
553555

@@ -627,8 +629,6 @@ describe('Router', () => {
627629
const shouldIntercept = ['/app', '/app/deeper'];
628630
const shouldNavigate = ['/site', '/site/deeper'];
629631

630-
const clickHandler = sinon.fake(e => e.preventDefault());
631-
632632
const Links = () => (
633633
<>
634634
<a href="/app">Internal Link</a>
@@ -638,23 +638,6 @@ describe('Router', () => {
638638
</>
639639
);
640640

641-
let pushState;
642-
643-
before(() => {
644-
pushState = sinon.spy(history, 'pushState');
645-
addEventListener('click', clickHandler);
646-
});
647-
648-
after(() => {
649-
pushState.restore();
650-
removeEventListener('click', clickHandler);
651-
});
652-
653-
beforeEach(async () => {
654-
clickHandler.resetHistory();
655-
pushState.resetHistory();
656-
});
657-
658641
it('should intercept clicks on links matching the `scope` props (string)', async () => {
659642
render(
660643
<LocationProvider scope="/app">
@@ -668,15 +651,10 @@ describe('Router', () => {
668651
scratch.querySelector(`a[href="${url}"]`).click();
669652
await sleep(1);
670653
expect(loc).to.deep.include({ url });
671-
expect(pushState).to.have.been.calledWith(null, '', url);
672-
expect(clickHandler).to.have.been.called;
673-
674-
pushState.resetHistory();
675-
clickHandler.resetHistory();
676654
}
677655
});
678656

679-
it('should allow default browser navigation for links not matching the `scope` props (string)', async () => {
657+
it.skip('should allow default browser navigation for links not matching the `scope` props (string)', async () => {
680658
render(
681659
<LocationProvider scope="app">
682660
<Links />
@@ -688,11 +666,8 @@ describe('Router', () => {
688666
for (const url of shouldNavigate) {
689667
scratch.querySelector(`a[href="${url}"]`).click();
690668
await sleep(1);
691-
expect(pushState).not.to.have.been.called;
692-
expect(clickHandler).to.have.been.called;
693669

694-
pushState.resetHistory();
695-
clickHandler.resetHistory();
670+
// TODO: How to test this?
696671
}
697672
});
698673

@@ -709,15 +684,10 @@ describe('Router', () => {
709684
scratch.querySelector(`a[href="${url}"]`).click();
710685
await sleep(1);
711686
expect(loc).to.deep.include({ url });
712-
expect(pushState).to.have.been.calledWith(null, '', url);
713-
expect(clickHandler).to.have.been.called;
714-
715-
pushState.resetHistory();
716-
clickHandler.resetHistory();
717687
}
718688
});
719689

720-
it('should allow default browser navigation for links not matching the `scope` props (regex)', async () => {
690+
it.skip('should allow default browser navigation for links not matching the `scope` props (regex)', async () => {
721691
render(
722692
<LocationProvider scope={/^\/app/}>
723693
<Links />
@@ -729,19 +699,23 @@ describe('Router', () => {
729699
for (const url of shouldNavigate) {
730700
scratch.querySelector(`a[href="${url}"]`).click();
731701
await sleep(1);
732-
expect(pushState).not.to.have.been.called;
733-
expect(clickHandler).to.have.been.called;
734702

735-
pushState.resetHistory();
736-
clickHandler.resetHistory();
703+
// TODO: How to test this?
737704
}
738705
});
739706
});
740707

741708
it('should scroll to top when navigating forward', async () => {
742709
const scrollTo = sinon.spy(window, 'scrollTo');
743710

744-
const Route = sinon.fake(() => <div style={{ height: '1000px' }}><a href="/link">link</a></div>);
711+
const Route = sinon.fake(
712+
() => (
713+
<div style={{ height: '1000px' }}>
714+
<a href="/link">link</a>
715+
</div>
716+
)
717+
);
718+
745719
render(
746720
<LocationProvider>
747721
<Router>
@@ -756,7 +730,7 @@ describe('Router', () => {
756730
expect(Route).to.have.been.calledOnce;
757731
Route.resetHistory();
758732

759-
loc.route('/programmatic');
733+
navigation.navigate('/programmatic');
760734
await sleep(1);
761735

762736
expect(loc).to.deep.include({ url: '/programmatic' });
@@ -779,14 +753,13 @@ describe('Router', () => {
779753
});
780754

781755
it('should ignore clicks on document fragment links', async () => {
782-
const pushState = sinon.spy(history, 'pushState');
783-
784756
const Route = sinon.fake(
785757
() => <div>
786758
<a href="#foo">just #foo</a>
787759
<a href="/other#bar">other #bar</a>
788760
</div>
789761
);
762+
790763
render(
791764
<LocationProvider>
792765
<Router>
@@ -799,7 +772,6 @@ describe('Router', () => {
799772
scratch
800773
);
801774

802-
expect(Route).to.have.been.calledOnce;
803775
Route.resetHistory();
804776

805777
scratch.querySelector('a[href="#foo"]').click();
@@ -808,22 +780,17 @@ describe('Router', () => {
808780
// NOTE: we don't (currently) propagate in-page anchor navigations into context, to avoid useless renders.
809781
expect(loc).to.deep.include({ url: '/' });
810782
expect(Route).not.to.have.been.called;
811-
expect(pushState).not.to.have.been.called;
812783
expect(location.hash).to.equal('#foo');
813784

814785
scratch.querySelector('a[href="/other#bar"]').click();
815786
await sleep(1);
816787

817788
expect(Route).to.have.been.calledOnce;
818789
expect(loc).to.deep.include({ url: '/other#bar', path: '/other' });
819-
expect(pushState).to.have.been.called;
820790
expect(location.hash).to.equal('#bar');
821-
822-
pushState.restore();
823791
});
824792

825793
it('should normalize children', async () => {
826-
const pushState = sinon.spy(history, 'pushState');
827794
const Route = sinon.fake(() => <a href="/foo#foo">foo</a>);
828795

829796
const routes = ['/foo', '/bar'];
@@ -846,9 +813,6 @@ describe('Router', () => {
846813

847814
expect(Route).to.have.been.calledOnce;
848815
expect(loc).to.deep.include({ url: '/foo#foo', path: '/foo' });
849-
expect(pushState).to.have.been.called;
850-
851-
pushState.restore();
852816
});
853817

854818
it('should match nested routes', async () => {
@@ -905,25 +869,30 @@ describe('Router', () => {
905869
});
906870

907871
it('should replace the current URL', async () => {
908-
const pushState = sinon.spy(history, 'pushState');
909-
const replaceState = sinon.spy(history, 'replaceState');
910-
911872
render(
912873
<LocationProvider>
913874
<Router>
875+
<Route path="/" component={() => null} />
914876
<Route path="/foo" component={() => null} />
877+
<Route path="/bar" component={() => null} />
915878
</Router>
916879
<ShallowLocation />
917880
</LocationProvider>,
918881
scratch
919882
);
920883

921-
loc.route("/foo", true);
922-
expect(pushState).not.to.have.been.called;
923-
expect(replaceState).to.have.been.calledWith(null, "", "/foo");
884+
navigation.navigate('/foo');
885+
navigation.navigate('/bar', { history: 'replace' });
886+
887+
const entries = navigation.entries();
888+
889+
// Top of the stack
890+
const last = new URL(entries[entries.length - 1].url);
891+
expect(last.pathname).to.equal('/bar');
924892

925-
pushState.restore();
926-
replaceState.restore();
893+
// Entry before
894+
const secondLast = new URL(entries[entries.length - 2].url);
895+
expect(secondLast.pathname).to.equal('/');
927896
});
928897

929898
it('should support using `Router` as an implicit suspense boundary', async () => {

0 commit comments

Comments
 (0)