Skip to content

Commit

Permalink
adds basic unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Enriqe committed Jan 16, 2020
1 parent 3d1db39 commit b024467
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/amp-story-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ export class AmpStoryEmbed {
return this.element_;
}

/**
* @visibleForTesting
* @return {!Element}
*/
getRoot() {
return this.rootEl_;
}

/** @public */
buildCallback() {
this.stories_ = toArray(this.element_.querySelectorAll('a'));
Expand Down Expand Up @@ -264,22 +272,33 @@ function layoutFallback(embedImpl) {
setTimeout(() => {
tick = true;

const embedTop = embedImpl.getElement()./*OK*/ getBoundingClientRect()
.top;
if (self./*OK*/ innerHeight * 2 > embedTop) {
embedImpl.layoutCallback();
}
layoutIfVisible(embedImpl);
}, SCROLL_THROTTLE_MS);

tick = false;
});

// Calls it once it in case scroll event never fires.
layoutIfVisible(embedImpl);
}

/**
* Checks if embed is close to the viewport and calls layoutCallback when it is.
* @param {!AmpStoryEmbed} embedImpl
*/
function layoutIfVisible(embedImpl) {
const embedTop = embedImpl.getElement()./*OK*/ getBoundingClientRect().top;
if (self./*OK*/ innerHeight * 2 > embedTop) {
embedImpl.layoutCallback();
}
}

/**
* Calls layoutCallback on the embed when it is close to the viewport.
* @param {!AmpStoryEmbed} embedImpl
* @visibleForTesting
*/
function layoutEmbed(embedImpl) {
export function layoutEmbed(embedImpl) {
if (IntersectionObserver && self === self.parent) {
const intersectingCallback = entries => {
entries.forEach(entry => {
Expand Down
72 changes: 72 additions & 0 deletions test/unit/test-amp-story-embed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {AmpStoryEmbed, layoutEmbed} from '../../src/amp-story-embed';
import {setStyles} from '../../src/style';

describe('amp-story-embed', () => {
describes.realWin('AmpStoryEmbed', {amp: false}, env => {
let win;
let storyEmbed;
let embedEl;
let url;

function buildStoryEmbed() {
setStyles(win.document.body, {'width': '500px', 'height': '5500px'});
embedEl = win.document.createElement('amp-story-embed');
const storyAnchor = win.document.createElement('a');
url =
'https://www-washingtonpost-com.cdn.ampproject.org/v/s/www.washingtonpost.com/graphics/2019/lifestyle/travel/amp-stories/a-locals-guide-to-what-to-eat-and-do-in-new-york-city/?testParam=true';
storyAnchor.setAttribute('href', url);
embedEl.appendChild(storyAnchor);
storyEmbed = new AmpStoryEmbed(win, embedEl);
win.document.body.appendChild(embedEl);
}

beforeEach(() => {
win = env.win;
buildStoryEmbed();
});

it('should build an iframe for each story', () => {
storyEmbed.buildCallback();
layoutEmbed(storyEmbed);

const shadowRoot = storyEmbed.getRoot();
expect(shadowRoot.children.length).to.equal(1);
});

it('should correctly append params at the end of the story url', () => {
storyEmbed.buildCallback();
layoutEmbed(storyEmbed);

const shadowRoot = storyEmbed.getRoot();
expect(shadowRoot.firstElementChild.getAttribute('src')).to.equals(
url + '?amp_js_v=0.1#&visibilityState=inactive&origin=about%3Asrcdoc'
);
});

it('should correctly append params at the end of a story url with existing params', () => {
storyEmbed.buildCallback();
layoutEmbed(storyEmbed);

const shadowRoot = storyEmbed.getRoot();
expect(shadowRoot.firstElementChild.getAttribute('src')).to.equals(
url + '&amp_js_v=0.1#&visibilityState=inactive&origin=about%3Asrcdoc'
);
});
});
});

0 comments on commit b024467

Please sign in to comment.