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

🐛 Delay adding the ampshare fragment parameter until after the document is first visible #17237

Merged
merged 4 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion src/service/viewer-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,9 @@ export class Viewer {

// This fragment may get cleared by impression tracking. If so, it will be
// restored afterward.
this.maybeUpdateFragmentForCct();
this.whenFirstVisiblePromise_.then(() => {
this.maybeUpdateFragmentForCct();
});
}

/**
Expand Down
42 changes: 28 additions & 14 deletions test/functional/test-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ describe('Viewer', () => {
windowApi.location.search = '?amp_gsa=1&amp_js_v=a0';
const viewer = new Viewer(ampdoc);
expect(viewer.isCctEmbedded()).to.be.true;
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#ampshare=http%3A%2F%2Fwww.example.com%2F');
return Promise.resolve().then(() => {
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#ampshare=http%3A%2F%2Fwww.example.com%2F');
});
});

it('should merge fragments within custom tab', () => {
Expand All @@ -180,8 +182,10 @@ describe('Viewer', () => {
const viewer = new Viewer(ampdoc);
expect(viewer.getParam('test')).to.equal('1');
expect(viewer.isCctEmbedded()).to.be.true;
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#test=1&ampshare=http%3A%2F%2Fwww.example.com%2F');
return Promise.resolve(() => {
Copy link

@dreamofabear dreamofabear Aug 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be viewer.whenFirstVisible(). If you change the test function from a closure to a generator function you can use yield.

it('should merge fragments within custom tab', function*() {
  ...
  yield viewer.whenFirstVisible();
  expect(windowApi...);
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion! Much cleaner. Fixed!

expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#test=1&ampshare=http%3A%2F%2Fwww.example.com%2F');
});
});

it('should not duplicate ampshare when merging', () => {
Expand All @@ -192,8 +196,10 @@ describe('Viewer', () => {
const viewer = new Viewer(ampdoc);
expect(viewer.getParam('test')).to.equal('1');
expect(viewer.isCctEmbedded()).to.be.true;
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#test=1&ampshare=http%3A%2F%2Fwww.example.com%2F');
return Promise.resolve(() => {
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#test=1&ampshare=http%3A%2F%2Fwww.example.com%2F');
});
});

it('should remove multiple ampshares when merging', () => {
Expand All @@ -206,8 +212,10 @@ describe('Viewer', () => {
const viewer = new Viewer(ampdoc);
expect(viewer.getParam('test')).to.equal('1');
expect(viewer.isCctEmbedded()).to.be.true;
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#test=1&ampshare=http%3A%2F%2Fwww.example.com%2F');
return Promise.resolve(() => {
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#test=1&ampshare=http%3A%2F%2Fwww.example.com%2F');
});
});

it('should remove extra ampshare even when it\'s first', () => {
Expand All @@ -218,8 +226,10 @@ describe('Viewer', () => {
const viewer = new Viewer(ampdoc);
expect(viewer.getParam('test')).to.equal('1');
expect(viewer.isCctEmbedded()).to.be.true;
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#ampshare=http%3A%2F%2Fwww.example.com%2F&test=1');
return Promise.resolve(() => {
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#ampshare=http%3A%2F%2Fwww.example.com%2F&test=1');
});
});

it('should remove extra ampshare even when it\'s sandwiched', () => {
Expand All @@ -233,8 +243,10 @@ describe('Viewer', () => {
expect(viewer.getParam('test')).to.equal('1');
expect(viewer.getParam('note')).to.equal('ok');
expect(viewer.isCctEmbedded()).to.be.true;
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#note=ok&ampshare=http%3A%2F%2Fwww.example.com%2F&test=1');
return Promise.resolve(() => {
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#note=ok&ampshare=http%3A%2F%2Fwww.example.com%2F&test=1');
});
});

it('should clear fragment when click param is present', () => {
Expand All @@ -257,8 +269,10 @@ describe('Viewer', () => {
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'http://www.example.com');
expect(viewer.getParam('click')).to.equal('abc');
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#ampshare=http%3A%2F%2Fwww.example.com%2F');
return Promise.resolve(() => {
expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'#ampshare=http%3A%2F%2Fwww.example.com%2F');
});
});

it('should configure visibilityState visible by default', () => {
Expand Down