Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We should skip the same URL #317

Merged
merged 5 commits into from May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/routing/history/StateHistory.ts
Expand Up @@ -53,7 +53,12 @@ export class StateHistory implements HistoryInterface {
}

public set(path: string) {
this._window.history.pushState({}, '', this.prefix(stripBase(this._base, path)));
const value = stripBase(this._base, path);
if (this._current === value) {
return;
}

this._window.history.pushState({}, '', this.prefix(value));
this._onChange();
}

Expand All @@ -63,11 +68,8 @@ export class StateHistory implements HistoryInterface {

private _onChange = () => {
const pathName = this._window.location.pathname.replace(/\/$/, '');
const value = stripBase(this._base, pathName + this._window.location.search);
if (this._current === value) {
return;
}
this._current = value;
this._current = stripBase(this._base, pathName + this._window.location.search);

this._onChangeFunction(this._current);
};
}
Expand Down
9 changes: 9 additions & 0 deletions tests/routing/unit/history/StateHistory.ts
Expand Up @@ -57,6 +57,15 @@ describe('StateHistory', () => {
assert.equal(sandbox.contentWindow!.location.pathname, '/foo');
});

it('update path, does not update path if path is set to the current value', () => {
const history = new StateHistory({ onChange, window: sandbox.contentWindow! });
const beforeLength = sandbox.contentWindow!.history.length;
history.set('/bar');
history.set('/bar');
const afterLength = sandbox.contentWindow!.history.length;
assert.equal(afterLength - beforeLength, 1);
});

it('update path, adds leading slash if necessary', () => {
const history = new StateHistory({ onChange, window: sandbox.contentWindow! });
history.set('foo');
Expand Down