-
Notifications
You must be signed in to change notification settings - Fork 0
/
straw-ios-spec.js
220 lines (133 loc) · 6.15 KB
/
straw-ios-spec.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
// straw-ios-spec.js
/* global describe, before, it, chai, sinon, straw */
describe('straw.core', function () {
'use strict';
var expect = chai.expect;
var console;
before(function () {
// create console mock
console = {log: sinon.mock()};
// inject mock console object
straw.core.setConsole(console);
});
it('is an object', function () {
expect(straw).to.exist;
expect(straw.core).to.exist;
expect(straw.core).to.be.a('object');
});
describe('serviceCall', function () {
it('puts a custom url to iframe for the Straw Service call', function () {
sinon.spy(window.document.documentElement, 'appendChild');
var callId = straw.core.serviceCall('serviceX', 'methodY', {abc: 123});
expect(window.document.documentElement.appendChild.calledOnce).to.be.true;
var call = window.document.documentElement.appendChild.getCall(0);
expect(call.args[0]).to.exist;
expect(call.args[0]).to.be.instanceof(HTMLIFrameElement);
expect(call.args[0].getAttribute('src')).to.equal('straw://' + callId);
window.document.documentElement.appendChild.restore();
});
});
describe('getRequestParams', function () {
it('returns the request parameters of the corresponding Straw Service call', function () {
var callId = straw.core.serviceCall('srv_x', 'meth_y', {abc: 123});
var params = straw.core.getRequestParams(callId);
expect(params).to.deep.equal({
service: 'srv_x',
method: 'meth_y',
params: {abc: 123},
callId: callId
});
});
it('returns nothing when called twice and warn message', function () {
// create a service call
var callId = straw.core.serviceCall('srv_x', 'meth_y', {abc: 123});
// set expectation for logging
console.log.once().withArgs('[Warn] param for id=' + callId + ' not found');
// discard the first call result
straw.core.getRequestParams(callId);
// the second call
var params = straw.core.getRequestParams(callId);
expect(params).not.to.exist;
console.log.verify();
});
});
describe('succeed', function () {
it('invokes the success callback', function () {
// create a mock callback
var successCallback = sinon.mock();
// set expectations
successCallback.once().withArgs({value: 1});
// create a service call
var callId = straw.core.serviceCall('serviceX', 'methodY', {}, successCallback);
// succeed the service call
straw.core.succeed(callId, {value: 1}, true);
// verify the expectations
successCallback.verify();
});
it('does nothing without error if callback is not registered', function () {
// create a service call
var callId = straw.core.serviceCall('serviceX', 'methodY', {});
// succeed the service call
straw.core.succeed(callId, {value: 1}, true);
});
it('does nothing without error if callback is not a function', function () {
// create a service call
var callId = straw.core.serviceCall('serviceX', 'methodY', {}, 'bogus callback');
// succeed the service call
straw.core.succeed(callId, {value: 1}, true);
});
describe('with keepAlive', function () {
it('keeps the callback and can invoke the same callback multiple times', function () {
// create a mock callback
var successCallback = sinon.mock();
// set expectations
successCallback.thrice();
// create a service call
var callId = straw.core.serviceCall('serviceX', 'methodY', {}, successCallback);
// succeed the service call
straw.core.succeed(callId, {value: 3}, true);
straw.core.succeed(callId, {value: 5}, true);
straw.core.succeed(callId, {value: 8}, true);
// verify the expectations
successCallback.verify();
});
});
describe('without keepAlive', function () {
it('drops the callback and cannot invoke the same callback twice', function () {
// create a mock callback
var successCallback = sinon.mock();
// set expectations
successCallback.once();
// create a service call
var callId = straw.core.serviceCall('serviceX', 'methodY', {}, successCallback);
// succeed the service call with keepAlive false
straw.core.succeed(callId, {value: 3}, false);
// succeed the same call id but these should be meaningless
straw.core.succeed(callId, {value: 5}, true);
straw.core.succeed(callId, {value: 8}, false);
// verify the expectations
successCallback.verify();
});
});
});
describe('fail', function () {
it('invokes the failure callback', function () {
// create a mock callback
var failureCallback = sinon.mock();
// set expectations
failureCallback.once().withArgs({code: 1001});
// create a service call
var callId = straw.core.serviceCall('serviceX', 'methodY', {}, null, failureCallback);
// succeed the service call
straw.core.fail(callId, {code: 1001}, true);
// verify the expectations
failureCallback.verify();
});
it('does nothing without error if callback is not a function', function () {
// create a service call
var callId = straw.core.serviceCall('serviceX', 'methodY', {}, null, 'bogus callback');
// fail the service call
straw.core.fail(callId, {value: 1}, true);
});
});
});