This repository was archived by the owner on Jun 21, 2023. It is now read-only.
forked from optimizely/javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.react_native.tests.js
344 lines (306 loc) · 12.4 KB
/
index.react_native.tests.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* Copyright 2019, Optimizely
*
* 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.
*/
var logging = require('@optimizely/js-sdk-logging');
var configValidator = require('./utils/config_validator');
var eventProcessor = require('@optimizely/js-sdk-event-processor');
var Optimizely = require('./optimizely');
var optimizelyFactory = require('./index.react_native');
var packageJSON = require('../package.json');
var testData = require('./tests/test_data');
var eventProcessor = require('@optimizely/js-sdk-event-processor');
var eventProcessorConfigValidator = require('./utils/event_processor_config_validator');
var defaultEventDispatcher = require('./plugins/event_dispatcher/index.browser');
var chai = require('chai');
var assert = chai.assert;
var sinon = require('sinon');
describe('javascript-sdk/react-native', function() {
describe('APIs', function() {
var xhr;
var requests;
it('should expose logger, errorHandler, eventDispatcher and enums', function() {
assert.isDefined(optimizelyFactory.logging);
assert.isDefined(optimizelyFactory.logging.createLogger);
assert.isDefined(optimizelyFactory.logging.createNoOpLogger);
assert.isDefined(optimizelyFactory.errorHandler);
assert.isDefined(optimizelyFactory.eventDispatcher);
assert.isDefined(optimizelyFactory.enums);
});
describe('createInstance', function() {
var fakeErrorHandler = { handleError: function() {} };
var fakeEventDispatcher = { dispatchEvent: function() {} };
var silentLogger;
beforeEach(function() {
silentLogger = optimizelyFactory.logging.createLogger({
logLevel: optimizelyFactory.enums.LOG_LEVEL.INFO,
logToConsole: false,
});
sinon.spy(console, 'error');
sinon.stub(configValidator, 'validate');
xhr = sinon.useFakeXMLHttpRequest();
global.XMLHttpRequest = xhr;
requests = [];
xhr.onCreate = function(req) {
requests.push(req);
};
});
afterEach(function() {
console.error.restore();
configValidator.validate.restore();
xhr.restore();
});
it('should not throw if the provided config is not valid', function() {
configValidator.validate.throws(new Error('Invalid config or something'));
assert.doesNotThrow(function() {
var optlyInstance = optimizelyFactory.createInstance({
datafile: {},
logger: silentLogger,
});
// Invalid datafile causes onReady Promise rejection - catch this error
optlyInstance.onReady().catch(function() {});
});
});
it('should create an instance of optimizely', function() {
var optlyInstance = optimizelyFactory.createInstance({
datafile: {},
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
});
// Invalid datafile causes onReady Promise rejection - catch this error
optlyInstance.onReady().catch(function() {});
assert.instanceOf(optlyInstance, Optimizely);
assert.equal(optlyInstance.clientVersion, '4.0.0-alpha.1');
});
it('should set the React Native JS client engine and javascript SDK version', function() {
var optlyInstance = optimizelyFactory.createInstance({
datafile: {},
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
});
// Invalid datafile causes onReady Promise rejection - catch this error
optlyInstance.onReady().catch(function() {});
assert.equal('react-native-js-sdk', optlyInstance.clientEngine);
assert.equal(packageJSON.version, optlyInstance.clientVersion);
});
it('should allow passing of "react-sdk" as the clientEngine and convert it to "react-native-sdk"', function() {
var optlyInstance = optimizelyFactory.createInstance({
clientEngine: 'react-sdk',
datafile: {},
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
});
// Invalid datafile causes onReady Promise rejection - catch this error
optlyInstance.onReady().catch(function() {});
assert.equal('react-native-sdk', optlyInstance.clientEngine);
});
it('should activate with provided event dispatcher', function() {
var optlyInstance = optimizelyFactory.createInstance({
datafile: testData.getTestProjectConfig(),
errorHandler: fakeErrorHandler,
eventDispatcher: optimizelyFactory.eventDispatcher,
logger: silentLogger,
});
var activate = optlyInstance.activate('testExperiment', 'testUser');
assert.strictEqual(activate, 'control');
});
describe('when no event dispatcher passed to createInstance', function() {
beforeEach(function() {
sinon.stub(defaultEventDispatcher, 'dispatchEvent', function(evt, cb) {
cb();
});
});
afterEach(function() {
defaultEventDispatcher.dispatchEvent.restore();
});
it('uses the default event dispatcher', function() {
var optlyInstance = optimizelyFactory.createInstance({
datafile: testData.getTestProjectConfig(),
errorHandler: fakeErrorHandler,
logger: silentLogger,
});
optlyInstance.activate('testExperiment', 'testUser');
return optlyInstance.close().then(function() {
sinon.assert.calledOnce(defaultEventDispatcher.dispatchEvent);
});
});
});
describe('when passing in logLevel', function() {
beforeEach(function() {
sinon.stub(logging, 'setLogLevel');
});
afterEach(function() {
logging.setLogLevel.restore();
});
it('should call logging.setLogLevel', function() {
optimizelyFactory.createInstance({
datafile: testData.getTestProjectConfig(),
logLevel: optimizelyFactory.enums.LOG_LEVEL.ERROR,
});
sinon.assert.calledOnce(logging.setLogLevel);
sinon.assert.calledWithExactly(logging.setLogLevel, optimizelyFactory.enums.LOG_LEVEL.ERROR);
});
});
describe('when passing in logger', function() {
beforeEach(function() {
sinon.stub(logging, 'setLogHandler');
});
afterEach(function() {
logging.setLogHandler.restore();
});
it('should call logging.setLogHandler with the supplied logger', function() {
var fakeLogger = { log: function() {} };
optimizelyFactory.createInstance({
datafile: testData.getTestProjectConfig(),
logger: fakeLogger,
});
sinon.assert.calledOnce(logging.setLogHandler);
sinon.assert.calledWithExactly(logging.setLogHandler, fakeLogger);
});
});
describe('event processor configuration', function() {
var eventProcessorSpy;
beforeEach(function() {
eventProcessorSpy = sinon.stub(eventProcessor, 'LogTierV1EventProcessor').callThrough();
});
afterEach(function() {
eventProcessor.LogTierV1EventProcessor.restore();
});
it('should use default event flush interval when none is provided', function() {
optimizelyFactory.createInstance({
datafile: testData.getTestProjectConfigWithFeatures(),
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
});
sinon.assert.calledWithExactly(
eventProcessorSpy,
sinon.match({
flushInterval: 1000,
})
);
});
describe('with an invalid flush interval', function() {
beforeEach(function() {
sinon.stub(eventProcessorConfigValidator, 'validateEventFlushInterval').returns(false);
});
afterEach(function() {
eventProcessorConfigValidator.validateEventFlushInterval.restore();
});
it('should ignore the event flush interval and use the default instead', function() {
optimizelyFactory.createInstance({
datafile: testData.getTestProjectConfigWithFeatures(),
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
eventFlushInterval: ['invalid', 'flush', 'interval'],
});
sinon.assert.calledWithExactly(
eventProcessorSpy,
sinon.match({
flushInterval: 1000,
})
);
});
});
describe('with a valid flush interval', function() {
beforeEach(function() {
sinon.stub(eventProcessorConfigValidator, 'validateEventFlushInterval').returns(true);
});
afterEach(function() {
eventProcessorConfigValidator.validateEventFlushInterval.restore();
});
it('should use the provided event flush interval', function() {
optimizelyFactory.createInstance({
datafile: testData.getTestProjectConfigWithFeatures(),
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
eventFlushInterval: 9000,
});
sinon.assert.calledWithExactly(
eventProcessorSpy,
sinon.match({
flushInterval: 9000,
})
);
});
});
it('should use default event batch size when none is provided', function() {
optimizelyFactory.createInstance({
datafile: testData.getTestProjectConfigWithFeatures(),
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
});
sinon.assert.calledWithExactly(
eventProcessorSpy,
sinon.match({
maxQueueSize: 10,
})
);
});
describe('with an invalid event batch size', function() {
beforeEach(function() {
sinon.stub(eventProcessorConfigValidator, 'validateEventBatchSize').returns(false);
});
afterEach(function() {
eventProcessorConfigValidator.validateEventBatchSize.restore();
});
it('should ignore the event batch size and use the default instead', function() {
optimizelyFactory.createInstance({
datafile: testData.getTestProjectConfigWithFeatures(),
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
eventBatchSize: null,
});
sinon.assert.calledWithExactly(
eventProcessorSpy,
sinon.match({
maxQueueSize: 10,
})
);
});
});
describe('with a valid event batch size', function() {
beforeEach(function() {
sinon.stub(eventProcessorConfigValidator, 'validateEventBatchSize').returns(true);
});
afterEach(function() {
eventProcessorConfigValidator.validateEventBatchSize.restore();
});
it('should use the provided event batch size', function() {
optimizelyFactory.createInstance({
datafile: testData.getTestProjectConfigWithFeatures(),
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
eventBatchSize: 300,
});
sinon.assert.calledWithExactly(
eventProcessorSpy,
sinon.match({
maxQueueSize: 300,
})
);
});
});
});
});
});
});