Skip to content

Commit

Permalink
Fix route handling in Preact 10 (fixes #340) (#342)
Browse files Browse the repository at this point in the history
Fix route handling in Preact 10 (fixes #340)
  • Loading branch information
marvinhagemeister authored Oct 21, 2019
2 parents 113ea77 + 1eec806 commit d63d776
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ class Router extends Component {

/** Re-render children with a new URL to match against. */
routeTo(url) {
this._didRoute = false;
this.setState({ url });

// if we're in the middle of an update, don't synchronously re-route.
if (this.updating) return this.canRoute(url);
const didRoute = this.canRoute(url);

this.forceUpdate();
return this._didRoute;
// trigger a manual re-route if we're not in the middle of an update:
if (!this.updating) this.forceUpdate();

return didRoute;
}

componentWillMount() {
Expand Down Expand Up @@ -228,7 +228,6 @@ class Router extends Component {
let active = this.getMatchingChildren(toChildArray(children), url, true);

let current = active[0] || null;
this._didRoute = !!current;

let previous = this.previousUrl;
if (url!==previous) {
Expand Down
37 changes: 28 additions & 9 deletions test/dom.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Router, Link, route } from 'src';
import { Match, Link as ActiveLink } from 'src/match';
import { h, render } from 'preact';
import { act } from 'preact/test-utils';

const Empty = () => null;

Expand Down Expand Up @@ -55,7 +56,9 @@ describe('dom', () => {
</div>
);
onChange.resetHistory();
$('a').click();
act(() => {
$('a').click();
});
expect(onChange)
.to.have.been.calledOnce
.and.to.have.been.calledWithMatch({ url:'/foo' });
Expand All @@ -74,7 +77,9 @@ describe('dom', () => {
</div>
);
onChange.resetHistory();
$('a').click();
act(() => {
$('a').click();
});
// fireEvent($('a'), 'click');
expect(onChange)
.to.have.been.calledOnce
Expand All @@ -92,7 +97,9 @@ describe('dom', () => {
</div>
);
onChange.resetHistory();
$('a').click();
act(() => {
$('a').click();
});
expect(onChange).not.to.have.been.called;
expect(location.href).to.contain('#foo');
});
Expand All @@ -113,10 +120,14 @@ describe('dom', () => {
</Router>
);
expect(A.prototype.componentWillMount).not.to.have.been.called;
route('/foo');
act(() => {
route('/foo');
});
expect(A.prototype.componentWillMount).to.have.been.calledOnce;
expect(A.prototype.componentWillUnmount).not.to.have.been.called;
route('/bar');
act(() => {
route('/bar');
});
expect(A.prototype.componentWillMount).to.have.been.calledOnce;
expect(A.prototype.componentWillUnmount).to.have.been.calledOnce;
});
Expand All @@ -141,7 +152,9 @@ describe('dom', () => {
</Router>
);
expect(A.prototype.componentWillMount).not.to.have.been.called;
route('/a');
act(() => {
route('/a');
});
expect(A.prototype.componentWillMount).to.have.been.calledOnce;
A.prototype.componentWillMount.resetHistory();
expect(location.pathname).to.equal('/b');
Expand All @@ -163,11 +176,17 @@ describe('dom', () => {
<A path="/foo" />
</Router>
);
route('/foo');
act(() => {
route('/foo');
});
expect(routerRef.base.outerHTML).to.eql('<p>bar is </p>');
route('/foo?bar=5');
act(() => {
route('/foo?bar=5');
});
expect(routerRef.base.outerHTML).to.eql('<p>bar is 5</p>');
route('/foo');
act(() => {
route('/foo');
});
expect(routerRef.base.outerHTML).to.eql('<p>bar is </p>');
});
});
Expand Down

0 comments on commit d63d776

Please sign in to comment.