Skip to content

Commit 511928a

Browse files
committed
refactor: Clean up
1 parent 6132253 commit 511928a

2 files changed

Lines changed: 7 additions & 59 deletions

File tree

src/router-navigation-api.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ function isInScope(url) {
3838
* @param {NavigateEvent} e
3939
*/
4040
function handleNav(state, e) {
41-
// TODO: Double-check this can't fail to parse.
42-
// `.destination` is read-only, so I'm hoping it guarantees a valid URL.
4341
const url = new URL(e.destination.url);
4442

4543
if (
@@ -93,14 +91,12 @@ export const exec = (url, route, matches = {}) => {
9391
* @param {import('preact').ComponentChildren} [props.children]
9492
*/
9593
export function LocationProvider(props) {
96-
// @ts-expect-error - props.url is not implemented correctly & will be removed in the future
97-
const [url, route] = useReducer(handleNav, props.url || location.pathname + location.search);
94+
const [url, route] = useReducer(handleNav, location.pathname + location.search);
9895
if (props.scope) scope = props.scope;
9996

10097
const value = useMemo(() => {
10198
const u = new URL(url, location.origin);
10299
const path = u.pathname.replace(/\/+$/g, '') || '/';
103-
// @ts-ignore-next
104100
return {
105101
url,
106102
path,
@@ -116,7 +112,6 @@ export function LocationProvider(props) {
116112
};
117113
}, []);
118114

119-
// @ts-ignore
120115
return h(LocationProvider.ctx.Provider, { value }, props.children);
121116
}
122117

@@ -259,9 +254,6 @@ export function Router(props) {
259254

260255
// The route is loaded and rendered.
261256
if (prevRoute.current !== path) {
262-
// TODO: Definitely need to think about this, for now, just removing `wasPush` as it
263-
// doesn't make sense in context of the Navigation API.
264-
scrollTo(0, 0);
265257
if (props.onRouteChange) props.onRouteChange(url);
266258

267259
prevRoute.current = path;
@@ -286,10 +278,10 @@ const RenderRef = ({ r }) => r.current;
286278
Router.Provider = LocationProvider;
287279

288280
LocationProvider.ctx = createContext(
289-
/** @type {import('./router.d.ts').LocationHook} */ ({})
281+
/** @type {import('./router-navigation-api.d.ts').LocationHook} */ ({})
290282
);
291283
const RouteContext = createContext(
292-
/** @type {import('./router.d.ts').RouteHook & { rest: string }} */ ({})
284+
/** @type {import('./router-navigation-api.d.ts').RouteHook & { rest: string }} */ ({})
293285
);
294286

295287
export const Route = props => h(props.component, props);

test/router-navigation-api.test.js

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ describe('Router', () => {
6060

6161
it('should strip trailing slashes from path', async () => {
6262
render(
63-
<LocationProvider url="/a/">
63+
<LocationProvider>
6464
<ShallowLocation />
6565
</LocationProvider>,
6666
scratch
6767
);
6868

69+
navigation.navigate('/a/');
70+
await sleep(1);
71+
6972
expect(loc).to.deep.include({
7073
url: '/a/',
7174
path: '/a',
@@ -735,53 +738,6 @@ describe('Router', () => {
735738
});
736739
});
737740

738-
it('should scroll to top when navigating forward', async () => {
739-
const scrollTo = sinon.spy(window, 'scrollTo');
740-
741-
const Route = sinon.fake(
742-
() => (
743-
<div style={{ height: '1000px' }}>
744-
<a href="/link">link</a>
745-
</div>
746-
)
747-
);
748-
749-
render(
750-
<LocationProvider>
751-
<Router>
752-
<Route default />
753-
</Router>
754-
<ShallowLocation />
755-
</LocationProvider>,
756-
scratch
757-
);
758-
759-
expect(scrollTo).not.to.have.been.called;
760-
expect(Route).to.have.been.calledOnce;
761-
Route.resetHistory();
762-
763-
navigation.navigate('/programmatic');
764-
await sleep(1);
765-
766-
expect(loc).to.deep.include({ url: '/programmatic' });
767-
expect(scrollTo).to.have.been.calledWith(0, 0);
768-
expect(scrollTo).to.have.been.calledOnce;
769-
expect(Route).to.have.been.calledOnce;
770-
Route.resetHistory();
771-
scrollTo.resetHistory();
772-
773-
scratch.querySelector('a').click();
774-
await sleep(1);
775-
776-
expect(loc).to.deep.include({ url: '/link' });
777-
expect(scrollTo).to.have.been.calledWith(0, 0);
778-
expect(scrollTo).to.have.been.calledOnce;
779-
expect(Route).to.have.been.calledOnce;
780-
Route.resetHistory();
781-
782-
scrollTo.restore();
783-
});
784-
785741
it('should ignore clicks on document fragment links', async () => {
786742
const Route = sinon.fake(
787743
() => <div>

0 commit comments

Comments
 (0)