forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-amp-viewer-assistance.js
168 lines (156 loc) · 5.77 KB
/
test-amp-viewer-assistance.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
import {ActionInvocation} from '../../../../src/service/action-impl';
import {AmpViewerAssistance} from '../amp-viewer-assistance';
import {mockServiceForDoc} from '../../../../testing/test-helper';
describes.fakeWin('AmpViewerAssistance', {
amp: true,
location: 'https://pub.com/doc1',
}, env => {
let document;
let ampdoc;
let element;
let viewerMock;
beforeEach(() => {
ampdoc = env.ampdoc;
document = env.win.document;
viewerMock = mockServiceForDoc(env.sandbox, env.ampdoc, 'viewer', [
'isTrustedViewer',
'sendMessage',
'sendMessageAwaitResponse',
]);
viewerMock.isTrustedViewer.returns(Promise.resolve(true));
viewerMock.sendMessageAwaitResponse.returns(Promise.resolve('idToken'));
element = document.createElement('script');
element.setAttribute('id', 'amp-viewer-assistance');
element.setAttribute('type', 'application/json');
document.body.appendChild(element);
});
it('should disable service when no config', () => {
document.body.removeChild(element);
const service = new AmpViewerAssistance(ampdoc);
expect(service.enabled_).to.be.false;
expect(service.assistanceElement_).to.be.undefined;
});
it('should disable service when the viewer is not trusted', () => {
viewerMock.isTrustedViewer.returns(Promise.resolve(false));
const config = {
'providerId': 'foo-bar',
};
element.textContent = JSON.stringify(config);
const service = new AmpViewerAssistance(ampdoc);
return service.start_().then(() => {
expect(service.enabled_).to.be.false;
});
});
it('should fail if config is malformed', () => {
expect(() => {
new AmpViewerAssistance(ampdoc);
}).to.throw(Error);
});
it('should send the config to the viewer', () => {
const config = {
'providerId': 'foo-bar',
};
element.textContent = JSON.stringify(config);
const service = new AmpViewerAssistance(ampdoc);
expect(service.enabled_).to.be.true;
expect(service.assistanceElement_).to.equal(element);
const sendMessageStub = service.viewer_.sendMessage;
return service.start_().then(() => {
expect(sendMessageStub).to.be.calledOnce;
expect(sendMessageStub.firstCall.args[0]).to
.equal('viewerAssistanceConfig');
expect(sendMessageStub.firstCall.args[1]).to.deep.equal({
'config': config,
});
});
});
it('should send updateActionState to the viewer', () => {
const config = {
'providerId': 'foo-bar',
};
element.textContent = JSON.stringify(config);
const service = new AmpViewerAssistance(ampdoc);
const sendMessageStub = service.viewer_.sendMessageAwaitResponse;
const invocationArgs = {
'foo': 'bar',
};
return service.start_().then(() => {
sendMessageStub.resetHistory();
const invocation = new ActionInvocation(
element, 'updateActionState', invocationArgs);
service.actionHandler_(invocation);
expect(sendMessageStub).to.be.calledOnce;
expect(sendMessageStub.firstCall.args[0]).to.equal('updateActionState');
expect(sendMessageStub.firstCall.args[1]).to.deep.equal(invocationArgs);
});
});
it('should fail to send updateActionState if args are missing', () => {
const config = {
'providerId': 'foo-bar',
};
element.textContent = JSON.stringify(config);
const service = new AmpViewerAssistance(ampdoc);
const sendMessageStub = service.viewer_.sendMessage;
return service.start_().then(() => {
sendMessageStub.reset();
const invocation = new ActionInvocation(element, 'updateActionState');
service.actionHandler_(invocation);
expect(sendMessageStub).to.not.be.called;
});
});
it('should send handle the signIn action', () => {
const config = {
'providerId': 'foo-bar',
};
element.textContent = JSON.stringify(config);
const service = new AmpViewerAssistance(ampdoc);
const sendMessageStub = service.viewer_.sendMessageAwaitResponse;
return service.start_().then(() => {
sendMessageStub.resetHistory();
sendMessageStub.returns(Promise.reject());
const invocation = new ActionInvocation(element, 'signIn');
service.actionHandler_(invocation);
expect(sendMessageStub).to.be.calledOnce;
expect(sendMessageStub.firstCall.args[0]).to.equal('requestSignIn');
expect(sendMessageStub.firstCall.args[1]).to.deep.equal({
providers: ['actions-on-google-gsi'],
});
});
});
it('should make IDENTITY_TOKEN available through a promise', () => {
const config = {
'providerId': 'foo-bar',
};
element.textContent = JSON.stringify(config);
const service = new AmpViewerAssistance(ampdoc);
return service.start_()
.then(() => service.getIdTokenPromise())
.then(token => expect(token).to.equal('idToken'));
});
it('should set the css classes if IDENTITY_TOKEN is unavailable', () => {
const config = {
'providerId': 'foo-bar',
};
element.textContent = JSON.stringify(config);
const service = new AmpViewerAssistance(ampdoc);
service.vsync_ = {
mutate: callback => {
callback();
},
};
const sendMessageStub = service.viewer_.sendMessageAwaitResponse;
sendMessageStub.returns(Promise.reject());
return service.getIdTokenPromise().then(() => {
expect(sendMessageStub).to.be.calledOnce;
expect(sendMessageStub.firstCall.args[0]).to.equal(
'getAccessTokenPassive');
expect(sendMessageStub.firstCall.args[1]).to.deep.equal({
providers: ['actions-on-google-gsi'],
});
expect(document.documentElement).not.to.have.class(
'amp-viewer-assistance-identity-available');
expect(document.documentElement).to.have.class(
'amp-viewer-assistance-identity-unavailable');
});
});
});