-
-
Notifications
You must be signed in to change notification settings - Fork 656
/
global-frontend-api-cache.test.ts
169 lines (150 loc) · 4.56 KB
/
global-frontend-api-cache.test.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
GlobalFrontendApiCache,
GlobalFrontendApiCacheState,
} from './global-frontend-api-cache';
import noLogger from '../../test/fixtures/no-logger';
import { FakeSegmentReadModel } from '../features/segment/fake-segment-read-model';
import FakeClientFeatureToggleReadModel from './fake-client-feature-toggle-read-model';
import EventEmitter from 'events';
import { IApiUser, IFeatureToggleClient, ISegment } from '../types';
import { UPDATE_REVISION } from '../features/feature-toggle/configuration-revision-service';
const state = async (
cache: GlobalFrontendApiCache,
state: GlobalFrontendApiCacheState,
) => {
await new Promise((resolve) => {
cache.on(state, () => {
resolve('done');
});
});
};
const defaultFeature: IFeatureToggleClient = {
name: 'featureA',
enabled: true,
strategies: [],
variants: [],
project: 'projectA',
dependencies: [],
type: 'release',
stale: false,
description: '',
};
const defaultSegment = { name: 'segment', id: 1 } as ISegment;
const createCache = (
segment: ISegment = defaultSegment,
features: Record<string, IFeatureToggleClient[]> = {},
) => {
const config = { getLogger: noLogger };
const segmentReadModel = new FakeSegmentReadModel([segment as ISegment]);
const clientFeatureToggleReadModel = new FakeClientFeatureToggleReadModel(
features,
);
const configurationRevisionService = new EventEmitter();
const cache = new GlobalFrontendApiCache(
config,
segmentReadModel,
clientFeatureToggleReadModel,
configurationRevisionService,
);
return {
cache,
configurationRevisionService,
clientFeatureToggleReadModel,
};
};
test('Can read initial segment', async () => {
const { cache } = createCache({ name: 'segment', id: 1 } as ISegment);
const segmentBeforeRead = cache.getSegment(1);
expect(segmentBeforeRead).toEqual(undefined);
await state(cache, 'ready');
const segment = cache.getSegment(1);
expect(segment).toEqual({ name: 'segment', id: 1 });
});
test('Can read initial features', async () => {
const { cache } = createCache(defaultSegment, {
development: [
{
...defaultFeature,
name: 'featureA',
enabled: true,
project: 'projectA',
},
{
...defaultFeature,
name: 'featureB',
enabled: true,
project: 'projectB',
},
],
production: [
{
...defaultFeature,
name: 'featureA',
enabled: false,
project: 'projectA',
},
],
});
const featuresBeforeRead = cache.getToggles({
environment: 'development',
projects: ['projectA'],
} as IApiUser);
expect(featuresBeforeRead).toEqual([]);
await state(cache, 'ready');
const features = cache.getToggles({
environment: 'development',
projects: ['projectA'],
} as IApiUser);
expect(features).toEqual([
{
...defaultFeature,
name: 'featureA',
enabled: true,
impressionData: false,
},
]);
const allProjectFeatures = cache.getToggles({
environment: 'development',
projects: ['*'],
} as IApiUser);
expect(allProjectFeatures.length).toBe(2);
const defaultProjectFeatures = cache.getToggles({
environment: '*',
projects: ['*'],
} as IApiUser);
expect(defaultProjectFeatures.length).toBe(0);
});
test('Can refresh data on revision update', async () => {
const {
cache,
configurationRevisionService,
clientFeatureToggleReadModel,
} = createCache();
await state(cache, 'ready');
clientFeatureToggleReadModel.setValue({
development: [
{
...defaultFeature,
name: 'featureA',
enabled: false,
strategies: [{ name: 'default' }],
project: 'projectA',
},
],
});
configurationRevisionService.emit(UPDATE_REVISION);
await state(cache, 'updated');
const features = cache.getToggles({
environment: 'development',
projects: ['projectA'],
} as IApiUser);
expect(features).toMatchObject([
{
...defaultFeature,
name: 'featureA',
enabled: false,
strategies: [{ name: 'default' }],
impressionData: false,
},
]);
});