-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathLDClientImpl.variation.test.ts
More file actions
123 lines (103 loc) · 3.41 KB
/
LDClientImpl.variation.test.ts
File metadata and controls
123 lines (103 loc) · 3.41 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
import {
AutoEnvAttributes,
clone,
Context,
LDContext,
LDLogger,
} from '@launchdarkly/js-sdk-common';
import LDClientImpl from '../src/LDClientImpl';
import { Flags } from '../src/types';
import { createBasicPlatform } from './createBasicPlatform';
import * as mockResponseJson from './evaluation/mockResponse.json';
import { MockEventSource } from './streaming/LDClientImpl.mocks';
import { makeTestDataManagerFactory } from './TestDataManager';
let mockPlatform: ReturnType<typeof createBasicPlatform>;
let logger: LDLogger;
beforeEach(() => {
mockPlatform = createBasicPlatform();
logger = {
error: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
};
});
const testSdkKey = 'test-sdk-key';
const context: LDContext = { kind: 'org', key: 'Testy Pizza' };
let ldc: LDClientImpl;
let mockEventSource: MockEventSource;
let simulatedEvents: { data?: any }[] = [];
let defaultPutResponse: Flags;
describe('sdk-client object', () => {
beforeEach(() => {
defaultPutResponse = clone<Flags>(mockResponseJson);
simulatedEvents = [{ data: JSON.stringify(defaultPutResponse) }];
mockPlatform.requests.createEventSource.mockImplementation(
(streamUri: string = '', options: any = {}) => {
mockEventSource = new MockEventSource(streamUri, options);
mockEventSource.simulateEvents('put', simulatedEvents);
return mockEventSource;
},
);
ldc = new LDClientImpl(
testSdkKey,
AutoEnvAttributes.Disabled,
mockPlatform,
{
logger,
sendEvents: false,
},
makeTestDataManagerFactory(testSdkKey, mockPlatform),
);
});
afterEach(() => {
jest.resetAllMocks();
});
test('variation', async () => {
await ldc.identify(context);
const devTestFlag = ldc.variation('dev-test-flag');
expect(devTestFlag).toBe(true);
});
test('variation flag not found should give a warning message', async () => {
await ldc.identify({ kind: 'user', key: 'test-user' });
const errorListener = jest.fn().mockName('errorListener');
ldc.on('error', errorListener);
const p = ldc.identify(context);
const flagValue = ldc.variation('does-not-exist', 'not-found');
await expect(p).resolves.toEqual({ status: 'completed' });
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Unknown feature'));
expect(flagValue).toBe('not-found');
});
test('variationDetail flag not found should return an error detail', async () => {
await ldc.identify(context);
const flag = ldc.variationDetail('does-not-exist', 'not-found');
expect(flag).toEqual({
reason: { errorKind: 'FLAG_NOT_FOUND', kind: 'ERROR' },
value: 'not-found',
variationIndex: null,
});
});
test('variationDetail deleted flag not found', async () => {
await ldc.identify(context);
const checkedContext = Context.fromLDContext(context);
// @ts-ignore
// eslint-disable-next-line no-underscore-dangle
await ldc._flagManager.upsert(checkedContext, 'dev-test-flag', {
version: 999,
flag: {
deleted: true,
version: 0,
flagVersion: 0,
value: undefined,
variation: 0,
trackEvents: false,
},
});
const flag = ldc.variationDetail('dev-test-flag', 'deleted');
expect(flag).toEqual({
reason: { errorKind: 'FLAG_NOT_FOUND', kind: 'ERROR' },
value: 'deleted',
variationIndex: null,
});
});
});