-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathindex.lite.tests.js
83 lines (75 loc) · 3.2 KB
/
index.lite.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
/**
* Copyright 2021-2024 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.
*/
import { assert } from 'chai';
import sinon from 'sinon';
import * as enums from './utils/enums';
import Optimizely from './optimizely';
import * as loggerPlugin from './plugins/logger';
import optimizelyFactory from './index.lite';
import configValidator from './utils/config_validator';
describe('optimizelyFactory', function() {
describe('APIs', function() {
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 fakeLogger;
beforeEach(function() {
fakeLogger = { log: sinon.spy(), setLogLevel: sinon.spy() };
sinon.stub(loggerPlugin, 'createLogger').returns(fakeLogger);
sinon.stub(configValidator, 'validate');
sinon.stub(console, 'error');
});
afterEach(function() {
loggerPlugin.createLogger.restore();
configValidator.validate.restore();
console.error.restore();
});
it('should not throw if the provided config is not valid and log an error if logger is passed in', function() {
configValidator.validate.throws(new Error('Invalid config or something'));
var localLogger = loggerPlugin.createLogger({ logLevel: enums.LOG_LEVEL.INFO });
assert.doesNotThrow(function() {
var optlyInstance = optimizelyFactory.createInstance({
datafile: {},
logger: localLogger,
});
// Invalid datafile causes onReady Promise rejection - catch this
optlyInstance.onReady().catch(function() {});
});
sinon.assert.calledWith(localLogger.log, enums.LOG_LEVEL.ERROR);
});
it('should create an instance of optimizely', function() {
var optlyInstance = optimizelyFactory.createInstance({
datafile: {},
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: fakeLogger,
});
// Invalid datafile causes onReady Promise rejection - catch this
optlyInstance.onReady().catch(function() {});
assert.instanceOf(optlyInstance, Optimizely);
assert.equal(optlyInstance.clientVersion, '5.3.5');
});
});
});
});