-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrowser.spec.js
More file actions
124 lines (93 loc) · 3.9 KB
/
browser.spec.js
File metadata and controls
124 lines (93 loc) · 3.9 KB
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
// Here we are testing exceptions and the handler should be ours, we need to avoid tape-catch
import { initialLogLevel } from './setLocalStorage';
import tape from 'tape';
import sinon from 'sinon';
import fetchMock from '../testUtils/fetchMock';
import { SplitFactory, ErrorLogger, DebugLogger } from '../../';
// Don't care about SDK readiness
fetchMock.get('*', { throws: new TypeError('Network error') });
fetchMock.post('*', 200);
const minConfig = {
core: {
authorizationKey: '<fake-token-1>',
key: 'nico@split.io'
}
};
tape('## E2E Logger Tests ##', assert => {
const logSpy = sinon.spy(console, 'log');
assert.test('debug settings: false', async (t) => {
const factory = SplitFactory({ ...minConfig, debug: false });
await factory.client().destroy();
t.false(logSpy.calledWithMatch('splitio => '), 'shouldn\'t log split messages');
logSpy.resetHistory();
t.end();
});
assert.test('debug settings: Logger object', async (t) => {
const factory = SplitFactory({ ...minConfig, debug: ErrorLogger() });
await Promise.resolve();
await factory.client().destroy();
t.false(logSpy.calledWithMatch('[WARN]'), 'shouldn\'t log messages with level WARN');
t.true(logSpy.calledWithMatch('[ERROR]'), 'should log messages with level ERROR');
logSpy.resetHistory();
localStorage.clear();
t.end();
});
assert.test('debug settings: log level', async (t) => {
const factory = SplitFactory({ ...minConfig, debug: 'INFO' });
await Promise.resolve();
await factory.client().destroy();
t.false(logSpy.calledWithMatch('[DEBUG]'), 'shouldn\'t log messages with level DEBUG');
t.true(logSpy.calledWithMatch('[INFO]'), 'should log messages with level INFO');
logSpy.resetHistory();
t.end();
});
assert.test('debug settings: custom logger', async (t) => {
const customLogger = {
debug: sinon.spy(),
info: sinon.spy(),
warn: sinon.spy(),
error: sinon.spy()
};
const factory = SplitFactory({ ...minConfig, debug: 'INFO', logger: customLogger });
t.equal(factory.settings.log.options.logLevel, 'DEBUG', 'When combined with the `logger` option, any log level other than `NONE` (false) will be set to `DEBUG` (true)');
await factory.client().destroy();
t.true(customLogger.debug.calledWithMatch('splitio => '), 'should log messages with level DEBUG');
t.true(customLogger.info.calledWithMatch('splitio => '), 'should log messages with level INFO');
logSpy.resetHistory();
t.end();
});
assert.test('debug settings: localStorage.splitio_debug = "enable"', async (t) => {
const factory = SplitFactory(minConfig);
await Promise.resolve();
await factory.client().destroy();
t.true(logSpy.calledWithMatch('[' + initialLogLevel + ']'), 'should log messages with level' + initialLogLevel);
logSpy.resetHistory();
t.end();
});
assert.test('Logger API', (t) => {
function assertLoggerApi(factory) {
factory.Logger.disable();
t.equal(factory.settings.log.options.logLevel, 'NONE');
factory.Logger.enable();
t.equal(factory.settings.log.options.logLevel, 'DEBUG');
factory.Logger.setLogLevel('WARN');
t.equal(factory.settings.log.options.logLevel, 'WARN');
factory.Logger.setLogLevel('invalid');
t.equal(factory.settings.log.options.logLevel, 'WARN');
// attempt to set invalid logger
factory.Logger.setLogger('invalid logger');
t.equal(factory.settings.log.logger, undefined);
// set logger
factory.Logger.setLogger(console);
t.equal(factory.settings.log.logger, console);
// unset logger
factory.Logger.setLogger(undefined);
t.equal(factory.settings.log.logger, undefined);
factory.client().destroy();
}
assertLoggerApi(SplitFactory({ ...minConfig, debug: DebugLogger() }));
assertLoggerApi(SplitFactory({ ...minConfig, debug: 'ERROR' }));
t.end();
});
assert.end();
});