forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-dialog.js
134 lines (122 loc) · 4.84 KB
/
test-dialog.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
/**
* 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 {CSS} from '../../../../build/amp-subscriptions-0.1.css';
import {Dialog} from '../dialog';
import {Services} from '../../../../src/services';
import {createElementWithAttributes} from '../../../../src/dom';
import {installStylesForDoc} from '../../../../src/style-installer';
describes.realWin('AmpSubscriptions Dialog', {amp: true}, env => {
let win, doc, ampdoc;
let dialog;
let content;
let vsync, viewport;
let updatePaddingSpy;
beforeEach(() => {
win = env.win;
doc = win.document;
doc.body.parentNode.setAttribute('amp-version', '1');
ampdoc = env.ampdoc;
installStylesForDoc(ampdoc, CSS, () => {}, false, 'amp-subscriptions');
vsync = Services.vsyncFor(ampdoc.win);
viewport = Services.viewportForDoc(ampdoc);
updatePaddingSpy = sandbox.stub(viewport, 'updatePaddingBottom');
dialog = new Dialog(ampdoc);
content = createElementWithAttributes(doc, 'div', {
style: 'height:17px',
});
});
it('should construct correctly', () => {
expect(dialog.getRoot().tagName.toLowerCase())
.to.equal('amp-subscriptions-dialog');
expect(dialog.getRoot().getAttribute('role'))
.to.equal('dialog');
expect(dialog.getRoot().parentNode).to.equal(doc.body);
expect(dialog.closeButton_).to.have.display('none');
expect(dialog.getRoot()).to.have.display('none');
const styles = getComputedStyle(dialog.getRoot());
expect(styles.position).to.equal('fixed');
});
it('should open content when invisible', () => {
const promise = dialog.open(content, false);
expect(dialog.getRoot()).to.have.display('none');
return vsync.mutatePromise(() => {}).then(() => {
// First vsync displays the dialog.
expect(content.parentNode).to.equal(dialog.getRoot());
expect(dialog.isVisible()).to.be.true;
expect(dialog.getRoot()).to.have.display('block');
const styles = getComputedStyle(dialog.getRoot());
expect(styles.transform).to.contain('17');
return promise;
}).then(() => {
expect(dialog.getRoot()).to.have.display('block');
const styles = getComputedStyle(dialog.getRoot());
expect(styles.transform).to.not.contain('17');
expect(dialog.closeButton_).to.have.display('none');
expect(updatePaddingSpy).to.be.calledOnce.calledWith(17);
expect(dialog.isVisible()).to.be.true;
});
});
it('should re-open content when visible', () => {
const content2 = createElementWithAttributes(doc, 'div', {
style: 'height:21px',
});
return dialog.open(content, false).then(() => {
expect(content.parentNode).to.equal(dialog.getRoot());
return dialog.open(content2, false);
}).then(() => {
expect(content2.parentNode).to.equal(dialog.getRoot());
expect(content.parentNode).to.be.null;
expect(dialog.getRoot()).to.have.display('block');
const styles = getComputedStyle(dialog.getRoot());
expect(styles.transform).to.not.contain('21');
});
});
it('should close', () => {
return dialog.open(content, false).then(() => {
expect(content.parentNode).to.equal(dialog.getRoot());
expect(dialog.getRoot()).to.have.display('block');
return dialog.close();
}).then(() => {
expect(dialog.getRoot()).to.have.display('none');
expect(dialog.isVisible()).to.be.false;
expect(content.parentNode).to.equal(dialog.getRoot());
expect(dialog.getRoot().parentNode).to.equal(doc.body);
});
});
it('should re-open after close', () => {
return dialog.open(content, false).then(() => {
expect(dialog.isVisible()).to.be.true;
return dialog.close();
}).then(() => {
expect(dialog.isVisible()).to.be.false;
return dialog.open(content, false);
}).then(() => {
expect(dialog.isVisible()).to.be.true;
});
});
it('should show close button', () => {
doc.body.classList.add('i-amphtml-subs-grant-yes');
return dialog.open(content, true).then(() => {
expect(dialog.closeButton_).to.have.display('block');
});
});
it('should not show close button if content is not granted', () => {
doc.body.classList.remove('i-amphtml-subs-grant-yes');
return dialog.open(content, true).then(() => {
expect(dialog.closeButton_).to.have.display('none');
});
});
});