forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-concurrent-load.js
108 lines (94 loc) · 3.63 KB
/
test-concurrent-load.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Copyright 2017 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 {
getAmpAdRenderOutsideViewport,
is3pThrottled,
incrementLoadingAds,
} from '../concurrent-load';
import {createElementWithAttributes} from '../../../../src/dom';
import {installTimerService} from '../../../../src/service/timer-impl';
import {macroTask} from '../../../../testing/yield';
import * as lolex from 'lolex';
describes.realWin('concurrent-load', {}, env => {
describe('getAmpAdRenderOutsideViewport', () => {
it('should return null if ' +
'data-loading-strategy attribute does not exist', () => {
const element = env.win.document.createElement('amp-ad');
expect(getAmpAdRenderOutsideViewport(element)).to.be.null;
});
it('should respect data-loading-strategy attribute', () => {
// data-loading-strategy=prefer-viewability-over-views is 1.25
verifyGetAmpAdRenderOutsideViewport(
'prefer-viewability-over-views', 1.25);
// data-loading-strategy attribute with no value is 1.25
verifyGetAmpAdRenderOutsideViewport('', 1.25);
verifyGetAmpAdRenderOutsideViewport('0', 0);
verifyGetAmpAdRenderOutsideViewport('0.256', 0.256);
verifyGetAmpAdRenderOutsideViewport('1.25', 1.25);
verifyGetAmpAdRenderOutsideViewport('3.0', 3);
expectGetAmpAdRenderOutsideViewportThrow('3.1');
expectGetAmpAdRenderOutsideViewportThrow('-0.1');
expectGetAmpAdRenderOutsideViewportThrow('invalid-value');
});
function verifyGetAmpAdRenderOutsideViewport(loadingStrategy, viewportNum) {
const element = createElementWithAttributes(env.win.document, 'amp-ad', {
'data-loading-strategy': loadingStrategy,
});
expect(getAmpAdRenderOutsideViewport(element)).to.equal(viewportNum);
}
function expectGetAmpAdRenderOutsideViewportThrow(loadingStrategy) {
const element = createElementWithAttributes(env.win.document, 'amp-ad', {
'data-loading-strategy': loadingStrategy,
});
expect(() => getAmpAdRenderOutsideViewport(element)).to.throw();
}
});
describe('incrementLoadingAds', () => {
let win;
let clock;
beforeEach(() => {
win = env.win;
clock = lolex.install({
target: win, toFake: ['Date', 'setTimeout', 'clearTimeout']});
installTimerService(win);
});
afterEach(() => {
clock.uninstall();
});
it('should throttle ad loading one per second', function* () {
expect(is3pThrottled(win)).to.be.false;
incrementLoadingAds(win);
expect(is3pThrottled(win)).to.be.true;
clock.tick(999);
yield macroTask();
expect(is3pThrottled(win)).to.be.true;
clock.tick(1);
yield macroTask();
expect(is3pThrottled(win)).to.be.false;
});
it('should throttle ad one a time', function* () {
expect(is3pThrottled(win)).to.be.false;
let resolver;
incrementLoadingAds(win, new Promise(res => {
resolver = res;
}));
expect(is3pThrottled(win)).to.be.true;
resolver();
yield macroTask();
expect(is3pThrottled(win)).to.be.false;
});
});
});