-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtest-document-submit.js
256 lines (230 loc) · 8.73 KB
/
test-document-submit.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Copyright 2016 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 {Services} from '../../src/services';
import {
installGlobalSubmitListenerForDoc,
onDocumentFormSubmit_,
} from '../../src/document-submit';
describes.sandboxed('test-document-submit', {}, () => {
describe('installGlobalSubmitListenerForDoc', () => {
let ampdoc;
let headNode;
let rootNode;
beforeEach(() => {
headNode = document.createElement('head');
rootNode = document.createElement('html');
ampdoc = {
getHeadNode: () => headNode,
getRootNode: () => rootNode,
waitForBodyOpen: () => Promise.resolve({}),
};
});
/**
* @param {string} extension
*/
const createScript = extension => {
const script = document.createElement('script');
script.setAttribute(
'src',
'https://cdn.ampproject.org/v0/' + extension + '-0.1.js'
);
script.setAttribute('custom-element', extension);
return script;
};
it('should not register submit listener if amp-form is not registered.', () => {
ampdoc.getHeadNode().appendChild(createScript('amp-list'));
sandbox.spy(rootNode, 'addEventListener');
return installGlobalSubmitListenerForDoc(ampdoc).then(() => {
expect(rootNode.addEventListener).not.to.have.been.called;
});
});
it('should register submit listener if amp-form extension is registered.', () => {
ampdoc.getHeadNode().appendChild(createScript('amp-form'));
sandbox.spy(rootNode, 'addEventListener');
return installGlobalSubmitListenerForDoc(ampdoc).then(() => {
expect(rootNode.addEventListener).called;
});
});
});
describes.realWin('onDocumentFormSubmit_', {amp: true}, env => {
let window, document;
let evt;
let tgt;
let preventDefaultSpy;
let stopImmediatePropagationSpy;
beforeEach(() => {
window = env.win;
document = window.document;
preventDefaultSpy = sandbox.spy();
stopImmediatePropagationSpy = sandbox.spy();
tgt = document.createElement('form');
tgt.action = 'https://www.google.com';
tgt.target = '_blank';
tgt.checkValidity = sandbox.stub().returns(true);
evt = {
target: tgt,
preventDefault: preventDefaultSpy,
stopImmediatePropagation: stopImmediatePropagationSpy,
defaultPrevented: false,
};
document.body.appendChild(tgt);
});
it('should check target and action attributes', () => {
tgt.removeAttribute('action');
allowConsoleError(() => {
expect(() => onDocumentFormSubmit_(evt)).to.throw(
/form action-xhr or action attribute is required for method=GET/
);
});
tgt.setAttribute('action', 'http://example.com');
tgt.__AMP_INIT_ACTION__ = undefined;
allowConsoleError(() => {
expect(() => onDocumentFormSubmit_(evt)).to.throw(
/form action must start with "https:/
);
});
tgt.setAttribute('action', 'https://cdn.ampproject.org');
tgt.__AMP_INIT_ACTION__ = undefined;
allowConsoleError(() => {
expect(() => onDocumentFormSubmit_(evt)).to.throw(
/form action should not be on AMP CDN/
);
});
tgt.setAttribute('action', 'https://valid.example.com');
tgt.__AMP_INIT_ACTION__ = undefined;
tgt.removeAttribute('target');
expect(() => onDocumentFormSubmit_(evt)).to.not.throw();
tgt.setAttribute('action', 'https://valid.example.com');
tgt.__AMP_INIT_ACTION__ = undefined;
tgt.setAttribute('target', '_self');
allowConsoleError(() => {
expect(() => onDocumentFormSubmit_(evt)).to.throw(
/form target=_self is invalid/
);
});
tgt.setAttribute('action', 'https://valid.example.com');
tgt.__AMP_INIT_ACTION__ = undefined;
tgt.setAttribute('target', '_blank');
expect(() => onDocumentFormSubmit_(evt)).to.not.throw;
});
it('should assert none of the inputs named __amp_source_origin', () => {
const illegalInput = document.createElement('input');
illegalInput.setAttribute('type', 'hidden');
illegalInput.setAttribute('name', '__amp_source_origin');
illegalInput.value = 'https://example.com';
tgt.appendChild(illegalInput);
allowConsoleError(() => {
expect(() => onDocumentFormSubmit_(evt)).to.throw(
/Illegal input name, __amp_source_origin found/
);
});
});
it('should assert __amp_source_origin is not set in action', () => {
evt.target.setAttribute(
'action',
'https://example.com/?__amp_source_origin=12'
);
allowConsoleError(() => {
expect(() => onDocumentFormSubmit_(evt)).to.throw(
/Source origin is not allowed in/
);
});
});
it('should fail when POST and action-xhr is not set', () => {
evt.target.removeAttribute('action');
evt.target.setAttribute('method', 'post');
allowConsoleError(() => {
expect(() => onDocumentFormSubmit_(evt)).to.throw(
/Only XHR based \(via action-xhr attribute\) submissions/
);
});
expect(preventDefaultSpy).to.have.been.called;
const {callCount} = preventDefaultSpy;
evt.target.setAttribute('method', 'post');
evt.target.setAttribute('action-xhr', 'https://example.com');
expect(() => onDocumentFormSubmit_(evt)).to.not.throw();
expect(preventDefaultSpy.callCount).to.equal(callCount + 1);
});
it('should do nothing if already prevented', () => {
evt.defaultPrevented = true;
onDocumentFormSubmit_(evt);
expect(preventDefaultSpy).to.have.not.been.called;
expect(tgt.checkValidity).to.have.not.been.called;
});
it('should default target to _top when missing', () => {
evt.target.removeAttribute('target');
onDocumentFormSubmit_(evt);
expect(evt.target.getAttribute('target')).to.equal('_top');
});
it('should throw if no target', () => {
evt.target = null;
allowConsoleError(() => {
expect(() => onDocumentFormSubmit_(evt)).to.throw(/Element expected/);
});
expect(preventDefaultSpy).to.have.not.been.called;
expect(tgt.checkValidity).to.have.not.been.called;
});
it('should prevent submit', () => {
tgt.checkValidity = sandbox.stub().returns(false);
onDocumentFormSubmit_(evt);
expect(preventDefaultSpy).to.be.calledOnce;
expect(tgt.checkValidity).to.be.calledOnce;
sandbox.restore();
preventDefaultSpy.resetHistory();
tgt.checkValidity.reset();
tgt.checkValidity = sandbox.stub().returns(false);
onDocumentFormSubmit_(evt);
expect(preventDefaultSpy).to.be.calledOnce;
expect(tgt.checkValidity).to.be.calledOnce;
});
it('should not check validity if novalidate provided', () => {
tgt.setAttribute('novalidate', '');
tgt.checkValidity = sandbox.stub().returns(false);
onDocumentFormSubmit_(evt);
expect(preventDefaultSpy).to.have.not.been.called;
expect(tgt.checkValidity).to.have.not.been.called;
});
it('should not prevent default', () => {
tgt.checkValidity = sandbox.stub().returns(true);
onDocumentFormSubmit_(evt);
expect(preventDefaultSpy).to.have.not.been.called;
expect(tgt.checkValidity).to.be.calledOnce;
});
it('should delegate xhr submit through action service', () => {
evt.target.setAttribute('action-xhr', 'https://example.com');
const actionService = Services.actionServiceForDoc(tgt);
sandbox.stub(actionService, 'execute');
onDocumentFormSubmit_(evt);
expect(actionService.execute).to.have.been.calledOnce;
expect(actionService.execute).to.have.been.calledWith(
tgt,
'submit',
null,
tgt,
tgt,
evt
);
expect(preventDefaultSpy).to.have.been.calledOnce;
expect(stopImmediatePropagationSpy).to.have.been.calledOnce;
});
it('should not delegate non-XHR submit through action service', () => {
const actionService = Services.actionServiceForDoc(tgt);
sandbox.stub(actionService, 'execute');
onDocumentFormSubmit_(evt);
expect(actionService.execute).to.have.not.been.called;
});
});
});