-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
test-amp-ad-template-helper.js
112 lines (102 loc) · 3.5 KB
/
test-amp-ad-template-helper.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
109
110
111
112
/**
* Copyright 2018 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 {AmpAdTemplateHelper} from '../amp-ad-template-helper';
import {AmpMustache} from '../../../amp-mustache/0.1/amp-mustache';
import {Xhr} from '../../../../src/service/xhr-impl';
describes.fakeWin('AmpAdTemplateHelper', {amp: true}, (env) => {
const cdnUrl =
'https://adserver-com.cdn.ampproject.org/ad/s/' +
'adserver.com/amp_template_1';
const canonicalUrl = 'https://adserver.com/amp_template_1';
let win, doc;
let fetchTextMock;
let ampAdTemplateHelper;
beforeEach(() => {
win = env.win;
win.__AMP_MODE = {localDev: false};
doc = win.document;
fetchTextMock = env.sandbox.stub(Xhr.prototype, 'fetchText');
ampAdTemplateHelper = new AmpAdTemplateHelper(win);
});
it('should return a promise resolving to a string template', () => {
const template = 'content not important here';
fetchTextMock
.withArgs(cdnUrl, {
mode: 'cors',
method: 'GET',
ampCors: false,
credentials: 'omit',
})
.returns(
Promise.resolve({
headers: {},
text: () => template,
})
);
return ampAdTemplateHelper
.fetch(canonicalUrl)
.then((fetchedTemplate) => expect(fetchedTemplate).to.equal(template));
});
it('should use CDN url if one is supplied', () => {
expect(ampAdTemplateHelper.getTemplateProxyUrl_(cdnUrl)).to.equal(cdnUrl);
});
it('should convert canonical to CDN', () => {
expect(ampAdTemplateHelper.getTemplateProxyUrl_(canonicalUrl)).to.equal(
cdnUrl
);
});
it('should render a template with correct values', () => {
win.AMP.registerTemplate('amp-mustache', AmpMustache);
});
it('should render a template with correct values', () => {
win.AMP.registerTemplate('amp-mustache', AmpMustache);
const parentDiv = doc.createElement('div');
parentDiv./*OK*/ innerHTML =
'<template type="amp-mustache"><p>{{foo}}</p></template>';
doc.body.appendChild(parentDiv);
return ampAdTemplateHelper
.render({foo: 'bar'}, parentDiv)
.then((result) => {
expect(result).to.not.be.null;
expect(result./*OK*/ innerHTML).to.equal('bar');
});
});
it('should insert analytics component', () => {
const parentDiv = doc.createElement('div');
parentDiv./*OK*/ innerHTML = '<p>123</p>';
doc.body.appendChild(parentDiv);
const analytics = [
{
'remote': 'remoteUrl',
'inline': {
'requests': 'r',
},
},
{
'type': 'googleanalytics',
},
];
ampAdTemplateHelper.insertAnalytics(parentDiv, analytics);
expect(parentDiv.childNodes.length).to.equal(3);
expect(parentDiv.innerHTML).to.equal(
'<p>123</p>' +
'<amp-analytics config="remoteUrl">' +
'<script type="application/json">{"requests":"r"}</script>' +
'</amp-analytics>' +
'<amp-analytics type="googleanalytics"></amp-analytics>'
);
});
});