forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-amp-story-access.js
More file actions
206 lines (174 loc) · 5.9 KB
/
test-amp-story-access.js
File metadata and controls
206 lines (174 loc) · 5.9 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* 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 {
Action,
AmpStoryStoreService,
StateProperty,
} from '../amp-story-store-service';
import {AmpStoryAccess, Type} from '../amp-story-access';
import {registerServiceBuilder} from '../../../../src/service';
describes.realWin('amp-story-access', {amp: true}, (env) => {
let win;
let accessConfigurationEl;
let defaultConfig;
let storeService;
let storyAccess;
let storyEl;
const setConfig = (config) => {
accessConfigurationEl.textContent = JSON.stringify(config);
};
beforeEach(() => {
win = env.win;
storeService = new AmpStoryStoreService(win);
registerServiceBuilder(win, 'story-store', function () {
return storeService;
});
accessConfigurationEl = win.document.createElement('script');
accessConfigurationEl.setAttribute('id', 'amp-access');
accessConfigurationEl.setAttribute('type', 'application/json');
defaultConfig = {login: 'https://example.com'};
setConfig(defaultConfig);
storyEl = win.document.createElement('amp-story');
const storyAccessEl = win.document.createElement('amp-story-access');
storyAccessEl.getAmpDoc = () => storyAccessEl;
storyEl.appendChild(storyAccessEl);
win.document.body.appendChild(accessConfigurationEl);
win.document.body.appendChild(storyEl);
storyAccess = new AmpStoryAccess(storyAccessEl);
});
it('should append the publisher provided template in a drawer', () => {
const publisherTemplateEl = win.document.createElement('button');
publisherTemplateEl.classList.add('subscribe-button');
publisherTemplateEl.innerText = 'Subscribe for $1';
storyAccess.element.appendChild(publisherTemplateEl);
storyAccess.buildCallback();
// Publisher provided button is no longer a child of <amp-story-access>.
expect(
storyAccess.element.querySelector('amp-story-access > .subscribe-button')
).to.be.null;
// But has been copied in the drawer.
const buttonInDrawerEl = storyAccess.element.querySelector(
'.i-amphtml-story-access-content > .subscribe-button'
);
expect(buttonInDrawerEl).to.exist;
});
it('should display the access blocking paywall on state update', (done) => {
storyAccess.buildCallback();
storeService.dispatch(Action.TOGGLE_ACCESS, true);
win.requestAnimationFrame(() => {
expect(storyAccess.element).to.have.class(
'i-amphtml-story-access-visible'
);
done();
});
});
it('should show the access notification on state update', (done) => {
storyAccess.element.setAttribute('type', Type.NOTIFICATION);
storyAccess.buildCallback();
storeService.dispatch(Action.CHANGE_PAGE, {
id: 'foo',
index: 0,
});
win.requestAnimationFrame(() => {
expect(storyAccess.element).to.have.class(
'i-amphtml-story-access-visible'
);
done();
});
});
it('should allowlist the default <amp-access> actions', () => {
storyAccess.buildCallback();
const actions = storyAccess.storeService_.get(
StateProperty.ACTIONS_ALLOWLIST
);
expect(actions).to.deep.contain({tagOrTarget: 'SCRIPT', method: 'login'});
});
it('should allowlist the typed <amp-access> actions', () => {
defaultConfig.login = {
typefoo: 'https://example.com',
typebar: 'https://example.com',
};
setConfig(defaultConfig);
storyAccess.buildCallback();
const actions = storyAccess.storeService_.get(
StateProperty.ACTIONS_ALLOWLIST
);
expect(actions).to.deep.contain({
tagOrTarget: 'SCRIPT',
method: 'login-typefoo',
});
expect(actions).to.deep.contain({
tagOrTarget: 'SCRIPT',
method: 'login-typebar',
});
});
it('should allowlist the namespaced and default <amp-access> actions', () => {
defaultConfig.namespace = 'foo';
setConfig(defaultConfig);
storyAccess.buildCallback();
// Both namespaced and default actions are allowed.
const actions = storyAccess.storeService_.get(
StateProperty.ACTIONS_ALLOWLIST
);
expect(actions).to.deep.contain({tagOrTarget: 'SCRIPT', method: 'login'});
expect(actions).to.deep.contain({
tagOrTarget: 'SCRIPT',
method: 'login-foo',
});
});
it('should allowlist namespaced and typed <amp-access> actions', () => {
const config = [
{
namespace: 'namespace1',
login: {
type1: 'https://example.com',
type2: 'https://example.com',
},
},
{
namespace: 'namespace2',
login: 'https://example.com',
},
];
setConfig(config);
storyAccess.buildCallback();
const actions = storyAccess.storeService_.get(
StateProperty.ACTIONS_ALLOWLIST
);
expect(actions).to.deep.contain({
tagOrTarget: 'SCRIPT',
method: 'login-namespace1-type1',
});
expect(actions).to.deep.contain({
tagOrTarget: 'SCRIPT',
method: 'login-namespace1-type2',
});
expect(actions).to.deep.contain({
tagOrTarget: 'SCRIPT',
method: 'login-namespace2',
});
});
it('should require publisher-logo-src to be a URL', () => {
storyEl.setAttribute('publisher-logo-src', 'foo:bar');
allowConsoleError(() => {
expect(() => {
storyAccess.buildCallback();
}).to.throw(
'amp-story publisher-logo-src must start with "https://" or "//"'
);
});
});
});