Skip to content

Commit 2bdc298

Browse files
committed
Testing progress.
1 parent 60ea015 commit 2bdc298

File tree

3 files changed

+493
-2
lines changed

3 files changed

+493
-2
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import {
2+
ApplicationTags,
3+
Configuration,
4+
Context,
5+
FlagManager,
6+
internal,
7+
LDEmitter,
8+
LDHeaders,
9+
LDIdentifyOptions,
10+
LDLogger,
11+
Platform,
12+
Response,
13+
ServiceEndpoints,
14+
} from '@launchdarkly/js-client-sdk-common';
15+
16+
import BrowserDataManager from '../src/BrowserDataManager';
17+
import { ValidatedOptions } from '../src/options';
18+
19+
function mockResponse(value: string, statusCode: number) {
20+
const response: Response = {
21+
headers: {
22+
get: jest.fn(),
23+
keys: jest.fn(),
24+
values: jest.fn(),
25+
entries: jest.fn(),
26+
has: jest.fn(),
27+
},
28+
status: statusCode,
29+
text: () => Promise.resolve(value),
30+
json: () => Promise.resolve(JSON.parse(value)),
31+
};
32+
return Promise.resolve(response);
33+
}
34+
35+
function mockFetch(value: string, statusCode: number = 200) {
36+
const f = jest.fn();
37+
f.mockResolvedValue(mockResponse(value, statusCode));
38+
return f;
39+
}
40+
41+
describe('given a BrowserDataManager with mocked dependencies', () => {
42+
let platform: jest.Mocked<Platform>;
43+
let flagManager: jest.Mocked<FlagManager>;
44+
let config: Configuration;
45+
let browserConfig: ValidatedOptions;
46+
let baseHeaders: LDHeaders;
47+
let emitter: jest.Mocked<LDEmitter>;
48+
let diagnosticsManager: jest.Mocked<internal.DiagnosticsManager>;
49+
let browserDataManager: BrowserDataManager;
50+
let logger: LDLogger;
51+
52+
beforeEach(() => {
53+
logger = {
54+
error: jest.fn(),
55+
warn: jest.fn(),
56+
info: jest.fn(),
57+
debug: jest.fn(),
58+
};
59+
config = {
60+
logger,
61+
baseUri: 'string',
62+
eventsUri: 'string',
63+
streamUri: 'string',
64+
maxCachedContexts: 5,
65+
capacity: 100,
66+
diagnosticRecordingInterval: 1000,
67+
flushInterval: 1000,
68+
streamInitialReconnectDelay: 1000,
69+
allAttributesPrivate: false,
70+
debug: true,
71+
diagnosticOptOut: false,
72+
sendEvents: false,
73+
sendLDHeaders: true,
74+
useReport: false,
75+
withReasons: true,
76+
privateAttributes: [],
77+
tags: new ApplicationTags({}),
78+
serviceEndpoints: new ServiceEndpoints('', ''),
79+
pollInterval: 1000,
80+
userAgentHeaderName: 'user-agent',
81+
trackEventModifier: (event) => event,
82+
}
83+
const mockedFetch = mockFetch('{"flagA": true}', 200);
84+
platform = {
85+
requests: {
86+
fetch: mockedFetch,
87+
createEventSource: jest.fn(),
88+
getEventSourceCapabilities: jest.fn(),
89+
},
90+
} as unknown as jest.Mocked<Platform>;
91+
92+
flagManager = {
93+
loadCached: jest.fn(),
94+
} as unknown as jest.Mocked<FlagManager>;
95+
96+
browserConfig = { stream: true } as ValidatedOptions;
97+
baseHeaders = {};
98+
emitter = {
99+
emit: jest.fn(),
100+
} as unknown as jest.Mocked<LDEmitter>;
101+
diagnosticsManager = {} as unknown as jest.Mocked<internal.DiagnosticsManager>;
102+
103+
browserDataManager = new BrowserDataManager(
104+
platform,
105+
flagManager,
106+
'test-credential',
107+
config,
108+
browserConfig,
109+
() => ({
110+
pathGet: jest.fn(),
111+
pathReport: jest.fn(),
112+
}),
113+
() => ({
114+
pathGet: jest.fn(),
115+
pathReport: jest.fn(),
116+
}),
117+
baseHeaders,
118+
emitter,
119+
diagnosticsManager,
120+
);
121+
});
122+
123+
afterEach(() => {
124+
jest.resetAllMocks();
125+
});
126+
127+
it('should load cached flags and continue to initialize via a poll', async () => {
128+
const context = Context.fromLDContext({ kind: 'user', key: 'test-user' });
129+
const identifyOptions: LDIdentifyOptions = {};
130+
const identifyResolve = jest.fn();
131+
const identifyReject = jest.fn();
132+
133+
flagManager.loadCached.mockResolvedValue(true);
134+
135+
await browserDataManager.identify(identifyResolve, identifyReject, context, identifyOptions);
136+
137+
expect(logger.debug).toHaveBeenCalledWith(
138+
'Identify - Flags loaded from cache. Continuing to initialize via a poll.',
139+
);
140+
expect(flagManager.loadCached).toHaveBeenCalledWith(context);
141+
expect(platform.requests.fetch).toHaveBeenCalled();
142+
});
143+
144+
it('should set up streaming connection if stream is enabled', async () => {
145+
const context = Context.fromLDContext({ kind: 'user', key: 'test-user' });
146+
const identifyOptions: LDIdentifyOptions = {};
147+
const identifyResolve = jest.fn();
148+
const identifyReject = jest.fn();
149+
150+
await browserDataManager.identify(identifyResolve, identifyReject, context, identifyOptions);
151+
152+
expect(platform.requests.createEventSource).toHaveBeenCalled();
153+
});
154+
155+
it('should not set up streaming connection if stream is disabled', async () => {
156+
browserConfig.stream = false;
157+
const context = Context.fromLDContext({ kind: 'user', key: 'test-user' });
158+
const identifyOptions: LDIdentifyOptions = {};
159+
const identifyResolve = jest.fn();
160+
const identifyReject = jest.fn();
161+
162+
await browserDataManager.identify(identifyResolve, identifyReject, context, identifyOptions);
163+
164+
expect(platform.requests.createEventSource).not.toHaveBeenCalled();
165+
});
166+
167+
// it('should stop the data source', () => {
168+
// const mockClose = jest.fn();
169+
// browserDataManager.updateProcessor = { close: mockClose } as any;
170+
171+
// browserDataManager.stopDataSource();
172+
173+
// expect(mockClose).toHaveBeenCalled();
174+
// expect(browserDataManager.updateProcessor).toBeUndefined();
175+
// });
176+
177+
// it('should start the data source if context exists', () => {
178+
// const mockSetupConnection = jest.spyOn(browserDataManager as any, 'setupConnection');
179+
// browserDataManager.context = Context.fromLDContext({ kind: 'user', key: 'test-user' });
180+
181+
// browserDataManager.startDataSource();
182+
183+
// expect(mockSetupConnection).toHaveBeenCalled();
184+
// });
185+
186+
// it('should not start the data source if context does not exist', () => {
187+
// const mockSetupConnection = jest.spyOn(browserDataManager as any, 'setupConnection');
188+
// browserDataManager.context = undefined;
189+
190+
// browserDataManager.startDataSource();
191+
192+
// expect(mockSetupConnection).not.toHaveBeenCalled();
193+
// });
194+
});

0 commit comments

Comments
 (0)