This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathstubs.js
107 lines (96 loc) · 2.5 KB
/
stubs.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
const { withCloseable } = require('launchdarkly-js-test-helpers');
var InMemoryFeatureStore = require('../feature_store');
var LDClient = require('../index.js');
var dataKind = require('../versioned_data_kind');
import { AsyncQueue } from 'launchdarkly-js-test-helpers';
import { format } from 'util';
function stubEventProcessor() {
var eventProcessor = {
events: [],
sendEvent: function(event) {
eventProcessor.events.push(event);
},
flush: function(callback) {
if (callback) {
setImmediate(callback);
} else {
return Promise.resolve(null);
}
},
close: function() {}
};
return eventProcessor;
}
function stubLogger() {
return {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn()
};
}
function asyncLogCapture() {
const logCapture = {all: new AsyncQueue()};
const logger = {};
for (const level of ['debug', 'info', 'warn', 'error']) {
logCapture[level] = new AsyncQueue();
logger[level] = function(fmt, ...args) {
const message = format(fmt, ...args);
logCapture[level].add(message);
logCapture.all.add({ level: level, message: message });
}
}
logCapture.logger = logger;
return logCapture;
}
function stubUpdateProcessor() {
var updateProcessor = {
start: function(callback) {
if (updateProcessor.shouldInitialize) {
setImmediate(callback, updateProcessor.error);
}
},
shouldInitialize: true
};
return updateProcessor;
}
function createClient(overrideOptions) {
var defaults = {
eventProcessor: stubEventProcessor(),
updateProcessor: stubUpdateProcessor(),
logger: stubLogger()
};
return LDClient.init('secret', Object.assign({}, defaults, overrideOptions));
}
async function withClient(overrideOptions, callback) {
return withCloseable(createClient(overrideOptions), callback);
}
function initializedStoreWithFlags(...flags) {
const flagsMap = {};
for (const f of flags) {
flagsMap[f.key] = f;
}
const store = InMemoryFeatureStore();
store.init({
[dataKind.features.namespace]: flagsMap,
[dataKind.segments.namespace]: {}
});
return store;
}
function uninitializedStoreWithFlags(...flags) {
const store = InMemoryFeatureStore();
for (const f of flags) {
store.upsert(dataKind.features, f);
}
return store;
}
module.exports = {
asyncLogCapture,
createClient,
initializedStoreWithFlags,
stubEventProcessor,
stubLogger,
stubUpdateProcessor,
uninitializedStoreWithFlags,
withClient,
};