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

Fixes for history state due to nesting of services #1571

Merged
merged 1 commit into from
Jan 26, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/service/history-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,11 @@ export class HistoryBindingNatural_ {
let pushState, replaceState;
if (history.pushState && history.replaceState) {
/** @private @const {function(*, string=, string=)|undefined} */
this.origPushState_ = history.pushState.bind(history);
this.origPushState_ = history.originalPushState ||
history.pushState.bind(history);
/** @private @const {function(*, string=, string=)|undefined} */
this.origReplaceState_ = history.replaceState.bind(history);
this.origReplaceState_ = history.originalReplaceState ||
history.replaceState.bind(history);
pushState = (state, opt_title, opt_url) => {
this.unsupportedState_ = state;
this.origPushState_(state, opt_title, opt_url);
Expand All @@ -290,6 +292,12 @@ export class HistoryBindingNatural_ {
this.origReplaceState_(state, opt_title);
}
};
if (!history.originalPushState) {
history.originalPushState = this.origPushState_;
}
if (!history.originalReplaceState) {
history.originalReplaceState = this.origReplaceState_;
}
} else {
pushState = (state, opt_title, opt_url) => {
this.unsupportedState_ = state;
Expand Down
26 changes: 13 additions & 13 deletions test/functional/test-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ describe('HistoryBindingNatural', () => {
expect(onStackIndexUpdated.callCount).to.equal(0);
});

// TODO(@dvoytenko): Unskip. Broke after change in service initialization
// sequence.
it.skip('should initialize correctly with preexisting state', () => {
it('should initialize correctly with preexisting state', () => {
history.origPushState_({'AMP.History': window.history.length}, undefined);
history.origReplaceState_({'AMP.History': window.history.length - 2},
undefined);
Expand Down Expand Up @@ -181,17 +179,19 @@ describe('HistoryBindingNatural', () => {
// This prevents IE11/Edge from coercing undefined to become the new url
it('should not pass in `url` argument to original replace state if ' +
'parameter is undefined', () => {
let argumentLength = 0;
const origReplace = window.history.replaceState;
window.history.replaceState = function() {
argumentLength = arguments.length;
const replaceStateSpy = sinon.spy();
const windowStub = {
history: {
replaceState: replaceStateSpy,
pushState: () => {},
state: {},
length: 11,
},
addEventListener: () => {},
};

new HistoryBindingNatural_(window);

expect(argumentLength).to.equal(2);

window.history.replaceState = origReplace;
new HistoryBindingNatural_(windowStub);
expect(replaceStateSpy.callCount).to.be.greaterThan(0);
expect(replaceStateSpy.lastCall.args.length).to.equal(2);
});

it('should push new state in the window.history and notify', () => {
Expand Down