Skip to content

Commit cbdcb5f

Browse files
committed
Adding tests for index files
1 parent 9c379fa commit cbdcb5f

File tree

4 files changed

+375
-7
lines changed

4 files changed

+375
-7
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { EdgeProvider, init } from '../index';
2+
import * as testData from './testData.json';
3+
4+
const sdkKey = 'test-sdk-key';
5+
const flagKey1 = 'testFlag1';
6+
const flagKey2 = 'testFlag2';
7+
const flagKey3 = 'testFlag3';
8+
const context: any = { kind: 'user', key: 'test-user-key-1' };
9+
10+
class FeatureStore implements EdgeProvider {
11+
async get(rootKey: string): Promise<string> {
12+
return Promise.resolve(JSON.stringify(testData));
13+
}
14+
}
15+
16+
describe('init', () => {
17+
let ldClient: any;
18+
const mockFeatureStore = new FeatureStore();
19+
20+
describe('init with own feature store', () => {
21+
beforeAll(async () => {
22+
ldClient = init({ sdkKey, featureStoreProvider: mockFeatureStore });
23+
await ldClient.waitForInitialization();
24+
});
25+
26+
afterAll(() => {
27+
ldClient.close();
28+
});
29+
30+
describe('flags', () => {
31+
it('variation default', async () => {
32+
const value = await ldClient.variation(flagKey1, context, false);
33+
expect(value).toBeTruthy();
34+
});
35+
36+
it('variation default rollout', async () => {
37+
const contextWithEmail = { ...context, email: '[email protected]' };
38+
const value = await ldClient.variation(flagKey2, contextWithEmail, false);
39+
const detail = await ldClient.variationDetail(flagKey2, contextWithEmail, false);
40+
41+
expect(detail).toEqual({ reason: { kind: 'FALLTHROUGH' }, value: true, variationIndex: 0 });
42+
expect(value).toBeTruthy();
43+
});
44+
45+
it('rule match', async () => {
46+
const contextWithEmail = { ...context, email: '[email protected]' };
47+
const value = await ldClient.variation(flagKey1, contextWithEmail, false);
48+
const detail = await ldClient.variationDetail(flagKey1, contextWithEmail, false);
49+
50+
expect(detail).toEqual({
51+
reason: { kind: 'RULE_MATCH', ruleId: 'rule1', ruleIndex: 0 },
52+
value: false,
53+
variationIndex: 1,
54+
});
55+
expect(value).toBeFalsy();
56+
});
57+
58+
it('fallthrough', async () => {
59+
const contextWithEmail = { ...context, email: '[email protected]' };
60+
const value = await ldClient.variation(flagKey1, contextWithEmail, false);
61+
const detail = await ldClient.variationDetail(flagKey1, contextWithEmail, false);
62+
63+
expect(detail).toEqual({ reason: { kind: 'FALLTHROUGH' }, value: true, variationIndex: 0 });
64+
expect(value).toBeTruthy();
65+
});
66+
67+
it('allFlags fallthrough', async () => {
68+
const allFlags = await ldClient.allFlagsState(context);
69+
70+
expect(allFlags).toBeDefined();
71+
expect(allFlags.toJSON()).toEqual({
72+
$flagsState: {
73+
testFlag1: { debugEventsUntilDate: 2000, variation: 0, version: 2 },
74+
testFlag2: { debugEventsUntilDate: 2000, variation: 0, version: 2 },
75+
testFlag3: { debugEventsUntilDate: 2000, variation: 0, version: 2 },
76+
},
77+
$valid: true,
78+
testFlag1: true,
79+
testFlag2: true,
80+
testFlag3: true,
81+
});
82+
});
83+
});
84+
85+
describe('segments', () => {
86+
it('segment by country', async () => {
87+
const contextWithCountry = { ...context, country: 'australia' };
88+
const value = await ldClient.variation(flagKey3, contextWithCountry, false);
89+
const detail = await ldClient.variationDetail(flagKey3, contextWithCountry, false);
90+
91+
expect(detail).toEqual({
92+
reason: { kind: 'RULE_MATCH', ruleId: 'rule1', ruleIndex: 0 },
93+
value: false,
94+
variationIndex: 1,
95+
});
96+
expect(value).toBeFalsy();
97+
});
98+
});
99+
});
100+
});
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
{
2+
"flags": {
3+
"testFlag1": {
4+
"key": "testFlag1",
5+
"on": true,
6+
"prerequisites": [],
7+
"targets": [],
8+
"rules": [
9+
{
10+
"variation": 1,
11+
"id": "rule1",
12+
"clauses": [
13+
{
14+
"contextKind": "user",
15+
"attribute": "/email",
16+
"op": "contains",
17+
"values": ["gmail"],
18+
"negate": false
19+
}
20+
],
21+
"trackEvents": false,
22+
"rollout": {
23+
"bucketBy": "bucket",
24+
"variations": [{ "variation": 1, "weight": 100 }]
25+
}
26+
}
27+
],
28+
"fallthrough": {
29+
"variation": 0
30+
},
31+
"offVariation": 1,
32+
"variations": [true, false],
33+
"clientSideAvailability": {
34+
"usingMobileKey": true,
35+
"usingEnvironmentId": true
36+
},
37+
"clientSide": true,
38+
"salt": "aef830243d6640d0a973be89988e008d",
39+
"trackEvents": false,
40+
"trackEventsFallthrough": false,
41+
"debugEventsUntilDate": 2000,
42+
"version": 2,
43+
"deleted": false
44+
},
45+
"testFlag2": {
46+
"key": "testFlag2",
47+
"on": true,
48+
"prerequisites": [],
49+
"targets": [],
50+
"rules": [],
51+
"fallthrough": {
52+
"variation": 0,
53+
"rollout": {
54+
"bucketBy": "bucket",
55+
"variations": [{ "variation": 1, "weight": 100 }],
56+
"contextKind:": "user",
57+
"attribute": "/email"
58+
}
59+
},
60+
"offVariation": 1,
61+
"variations": [true, false],
62+
"clientSideAvailability": {
63+
"usingMobileKey": true,
64+
"usingEnvironmentId": true
65+
},
66+
"clientSide": true,
67+
"salt": "aef830243d6640d0a973be89988e008d",
68+
"trackEvents": false,
69+
"trackEventsFallthrough": false,
70+
"debugEventsUntilDate": 2000,
71+
"version": 2,
72+
"deleted": false
73+
},
74+
"testFlag3": {
75+
"key": "testFlag3",
76+
"on": true,
77+
"prerequisites": [],
78+
"targets": [],
79+
"rules": [
80+
{
81+
"variation": 1,
82+
"id": "rule1",
83+
"clauses": [
84+
{
85+
"op": "segmentMatch",
86+
"values": ["testSegment1"],
87+
"negate": false
88+
}
89+
],
90+
"trackEvents": false
91+
}
92+
],
93+
"fallthrough": {
94+
"variation": 0
95+
},
96+
"offVariation": 1,
97+
"variations": [true, false],
98+
"clientSideAvailability": {
99+
"usingMobileKey": true,
100+
"usingEnvironmentId": true
101+
},
102+
"clientSide": true,
103+
"salt": "aef830243d6640d0a973be89988e008d",
104+
"trackEvents": false,
105+
"trackEventsFallthrough": false,
106+
"debugEventsUntilDate": 2000,
107+
"version": 2,
108+
"deleted": false
109+
}
110+
},
111+
"segments": {
112+
"testSegment1": {
113+
"name": "testSegment1",
114+
"tags": [],
115+
"creationDate": 1676063792158,
116+
"key": "testSegment1",
117+
"included": [],
118+
"excluded": [],
119+
"includedContexts": [],
120+
"excludedContexts": [],
121+
"_links": {
122+
"parent": { "href": "/api/v2/segments/default/test", "type": "application/json" },
123+
"self": {
124+
"href": "/api/v2/segments/default/test/beta-users-1",
125+
"type": "application/json"
126+
},
127+
"site": { "href": "/default/test/segments/beta-users-1", "type": "text/html" }
128+
},
129+
"rules": [
130+
{
131+
"id": "rule-country",
132+
"clauses": [
133+
{
134+
"attribute": "country",
135+
"op": "in",
136+
"values": ["australia"],
137+
"negate": false
138+
}
139+
]
140+
}
141+
],
142+
"version": 1,
143+
"deleted": false,
144+
"_access": { "denied": [], "allowed": [] },
145+
"generation": 1
146+
},
147+
"testSegment2": {
148+
"name": "testSegment2",
149+
"tags": [],
150+
"creationDate": 1676063792158,
151+
"key": "testSegment2",
152+
"included": [],
153+
"excluded": [],
154+
"includedContexts": [],
155+
"excludedContexts": [],
156+
"_links": {
157+
"parent": { "href": "/api/v2/segments/default/test", "type": "application/json" },
158+
"self": {
159+
"href": "/api/v2/segments/default/test/beta-users-1",
160+
"type": "application/json"
161+
},
162+
"site": { "href": "/default/test/segments/beta-users-1", "type": "text/html" }
163+
},
164+
"rules": [],
165+
"version": 1,
166+
"deleted": false,
167+
"_access": { "denied": [], "allowed": [] },
168+
"generation": 1
169+
}
170+
}
171+
}

packages/sdk/akamai-base/src/index.test.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { init as initWithEdgeKV, LDClient, LDContext } from '../index';
2+
import * as testData from './testData.json';
3+
import EdgeKVProvider from '../edgekv/edgeKVProvider';
4+
5+
jest.mock('../edgekv/edgekv', () => ({
6+
EdgeKV: jest.fn(),
7+
}));
8+
9+
const sdkKey = 'test-sdk-key';
10+
const flagKey1 = 'testFlag1';
11+
const flagKey2 = 'testFlag2';
12+
const flagKey3 = 'testFlag3';
13+
const context: LDContext = { kind: 'user', key: 'test-user-key-1' };
14+
15+
describe('init', () => {
16+
let ldClient: LDClient;
17+
18+
describe('init with Edge KV', () => {
19+
beforeAll(async () => {
20+
ldClient = initWithEdgeKV({ namespace: 'akamai-test', group: 'Akamai', sdkKey });
21+
await ldClient.waitForInitialization();
22+
});
23+
24+
beforeEach(() => {
25+
jest
26+
.spyOn(EdgeKVProvider.prototype, 'get')
27+
.mockImplementation(() => Promise.resolve(JSON.stringify(testData)));
28+
});
29+
30+
afterAll(() => {
31+
ldClient.close();
32+
});
33+
34+
describe('flags', () => {
35+
it('variation default', async () => {
36+
const value = await ldClient.variation(flagKey1, context, false);
37+
expect(value).toBeTruthy();
38+
});
39+
40+
it('variation default rollout', async () => {
41+
const contextWithEmail = { ...context, email: '[email protected]' };
42+
const value = await ldClient.variation(flagKey2, contextWithEmail, false);
43+
const detail = await ldClient.variationDetail(flagKey2, contextWithEmail, false);
44+
45+
expect(detail).toEqual({ reason: { kind: 'FALLTHROUGH' }, value: true, variationIndex: 0 });
46+
expect(value).toBeTruthy();
47+
});
48+
49+
it('rule match', async () => {
50+
const contextWithEmail = { ...context, email: '[email protected]' };
51+
const value = await ldClient.variation(flagKey1, contextWithEmail, false);
52+
const detail = await ldClient.variationDetail(flagKey1, contextWithEmail, false);
53+
54+
expect(detail).toEqual({
55+
reason: { kind: 'RULE_MATCH', ruleId: 'rule1', ruleIndex: 0 },
56+
value: false,
57+
variationIndex: 1,
58+
});
59+
expect(value).toBeFalsy();
60+
});
61+
62+
it('fallthrough', async () => {
63+
const contextWithEmail = { ...context, email: '[email protected]' };
64+
const value = await ldClient.variation(flagKey1, contextWithEmail, false);
65+
const detail = await ldClient.variationDetail(flagKey1, contextWithEmail, false);
66+
67+
expect(detail).toEqual({ reason: { kind: 'FALLTHROUGH' }, value: true, variationIndex: 0 });
68+
expect(value).toBeTruthy();
69+
});
70+
71+
it('allFlags fallthrough', async () => {
72+
const allFlags = await ldClient.allFlagsState(context);
73+
74+
expect(allFlags).toBeDefined();
75+
expect(allFlags.toJSON()).toEqual({
76+
$flagsState: {
77+
testFlag1: { debugEventsUntilDate: 2000, variation: 0, version: 2 },
78+
testFlag2: { debugEventsUntilDate: 2000, variation: 0, version: 2 },
79+
testFlag3: { debugEventsUntilDate: 2000, variation: 0, version: 2 },
80+
},
81+
$valid: true,
82+
testFlag1: true,
83+
testFlag2: true,
84+
testFlag3: true,
85+
});
86+
});
87+
});
88+
89+
describe('segments', () => {
90+
it('segment by country', async () => {
91+
const contextWithCountry = { ...context, country: 'australia' };
92+
const value = await ldClient.variation(flagKey3, contextWithCountry, false);
93+
const detail = await ldClient.variationDetail(flagKey3, contextWithCountry, false);
94+
95+
expect(detail).toEqual({
96+
reason: { kind: 'RULE_MATCH', ruleId: 'rule1', ruleIndex: 0 },
97+
value: false,
98+
variationIndex: 1,
99+
});
100+
expect(value).toBeFalsy();
101+
});
102+
});
103+
});
104+
});

0 commit comments

Comments
 (0)