Skip to content

Commit

Permalink
cleanup more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Voytenko committed Jun 19, 2020
1 parent 01054fe commit 788106c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 186 deletions.
107 changes: 0 additions & 107 deletions extensions/amp-a4a/0.1/test/test-refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
RefreshManager,
getPublisherSpecifiedRefreshInterval,
} from '../refresh-manager';
import {RefreshIntersectionObserverPolyfillWrapper} from '../refresh-intersection-observer-polyfill-wrapper';
import {Services} from '../../../../src/services';

describe('refresh', () => {
Expand Down Expand Up @@ -187,110 +186,4 @@ describe('refresh', () => {
});
});
});

describe('RefreshIntersectionObserverPolyfillWrapper', () => {
let callback;
let callbackPromise;
let getRect;
let observerWrapper;

beforeEach(() => {
getRect = () => ({
top: 0,
bottom: 10,
left: 0,
right: 10,
width: 10,
height: 10,
x: 0,
y: 0,
});

window.sandbox.stub(Services, 'viewportForDoc').callsFake(() => {
return {
getRect,
};
});
window.sandbox.stub(Services, 'ampdoc').callsFake(() => {
return {
getRootNode: () => {
return window.document;
},
win: window,
isSingleDoc: () => {
return true;
},
};
});

mockA4a.element.setAttribute(DATA_MANAGER_ID_NAME, '0');
mockA4a.element.viewportCallback = () => {};
mockA4a.element.getLayoutBox = getRect;
mockA4a.element.getOwner = () => mockA4a.element;
mockA4a.getViewport = () => ({getRect});

let resolver;
callbackPromise = new Promise((resolve) => {
resolver = resolve;
});
callback = (entries) => resolver(entries);
observerWrapper = new RefreshIntersectionObserverPolyfillWrapper(
callback,
mockA4a,
{threshold: 0.5}
);
});

it('should invoke callback with intersection ratio 1', () => {
observerWrapper.observe(mockA4a.element);
return callbackPromise.then((entries) => {
expect(entries).to.be.ok;
expect(entries[0]).to.be.ok;
expect(entries[0].intersectionRatio).to.equal(1);
});
});

it('should invoke callback with intersection ratio 0.5', () => {
observerWrapper.viewport_ = {
getRect: () => ({
top: 0,
bottom: 5,
left: 0,
right: 10,
width: 10,
height: 5,
x: 0,
y: 0,
}),
};
observerWrapper.observe(mockA4a.element);
return callbackPromise.then((entries) => {
expect(entries).to.be.ok;
expect(entries[0]).to.be.ok;
expect(entries[0].intersectionRatio).to.equal(0.5);
});
});

it('should not invoke callback', () => {
const callbackSpy = window.sandbox.spy(callback);
observerWrapper.viewport_ = {
getRect: () => ({
top: 10,
bottom: 5,
left: 0,
right: 10,
width: 10,
height: 5,
x: 0,
y: 0,
}),
};
observerWrapper.observe(mockA4a.element);
return Services.timerFor(window)
.promise(500)
.then(() => {
expect(callbackSpy).to.not.be.called;
});
});
});
});
80 changes: 1 addition & 79 deletions extensions/amp-analytics/0.1/test/test-visibility-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
* limitations under the License.
*/

import {AnalyticsEventType} from '../events';
import {FIE_EMBED_PROP} from '../../../../src/iframe-helper';
import {
IntersectionObserverPolyfill,
nativeIntersectionObserverSupported,
} from '../../../../src/utils/intersection-observer-polyfill';
import {Services} from '../../../../src/services';
import {
VisibilityManagerForDoc,
Expand Down Expand Up @@ -385,72 +380,6 @@ describes.fakeWin('VisibilityManagerForDoc', {amp: true}, (env) => {
expect(inOb.elements).to.not.contain(otherTarget);
});

it('should polyfill and dispose intersection observer', () => {
delete win.IntersectionObserver;

const startScrollCount = viewport.scrollObservable_.getHandlerCount();
const startChangeCount = viewport.changeObservable_.getHandlerCount();

// Check observer is correctly set.
const inOb = root.getIntersectionObserver_();
expect(inOb).to.be.instanceOf(IntersectionObserverPolyfill);
expect(viewport.scrollObservable_.getHandlerCount()).to.equal(
startScrollCount + 1
);
expect(viewport.changeObservable_.getHandlerCount()).to.equal(
startChangeCount + 1
);

root.dispose();
expect(viewport.scrollObservable_.getHandlerCount()).to.equal(
startScrollCount
);
expect(viewport.changeObservable_.getHandlerCount()).to.equal(
startChangeCount
);
});

it('should support polyfill on non-amp root element', () => {
delete win.IntersectionObserver;
const inOb = root.getIntersectionObserver_();

const rootElement = win.document.documentElement;
root.listenElement(rootElement, {}, null, null, eventResolver);
expect(root.models_).to.have.length(1);
const model = root.models_[0];
expect(inOb.observeEntries_).to.have.length(1);

// AMP API is polyfilled.
expect(rootElement.getLayoutBox).to.be.a('function');
expect(rootElement.getOwner()).to.be.null;

// Starts as invisible.
expect(model.getVisibility_()).to.equal(0);

// Trigger tick.
env.sandbox.stub(viewport, 'getRect').callsFake(() => {
return layoutRectLtwh(0, 0, 100, 100);
});
env.sandbox.stub(viewport, 'getLayoutRect').callsFake((element) => {
if (element == rootElement) {
return layoutRectLtwh(0, 50, 100, 100);
}
return null;
});
expect(rootElement.getLayoutBox()).to.contain({
left: 0,
top: 50,
width: 100,
height: 100,
});
viewport.scrollObservable_.fire({type: AnalyticsEventType.SCROLL});
expect(model.getVisibility_()).to.equal(0.5);

return eventPromise.then(() => {
expect(inOb.observeEntries_).to.have.length(0);
});
});

it('should listen on root', () => {
clock.tick(1);
const disposed = env.sandbox.spy();
Expand Down Expand Up @@ -1070,14 +999,7 @@ describes.realWin('VisibilityManager integrated', {amp: true}, (env) => {
unobserve: unobserveSpy,
};
};
if (nativeIntersectionObserverSupported(ampdoc.win)) {
env.sandbox.stub(win, 'IntersectionObserver').callsFake(inob);
} else {
win.IntersectionObserver = inob;
win.IntersectionObserverEntry = function () {};
win.IntersectionObserverEntry.prototype.intersectionRatio = 0;
expect(nativeIntersectionObserverSupported(ampdoc.win)).to.be.true;
}
env.sandbox.stub(win, 'IntersectionObserver').callsFake(inob);

readyPromise = new Promise((resolve) => {
readyResolver = resolve;
Expand Down

0 comments on commit 788106c

Please sign in to comment.