Skip to content

Commit 2a71a33

Browse files
authored
test: Akamai base and Akamai edgekv tests (#151)
Adding tests for Akamai base and Akamai edgeKV index files Ran into this error when running the tests so I've added `moduleNameMapper` to the `jest.config.json` files <img width="742" alt="Screenshot 2023-06-08 at 10 26 12 AM" src="https://github.com/launchdarkly/js-core/assets/104030863/a42a1eeb-08f7-4b4b-8a6b-af8bb732f375">
2 parents 8a138c1 + 87aa85f commit 2a71a33

File tree

7 files changed

+386
-12
lines changed

7 files changed

+386
-12
lines changed

packages/sdk/akamai-base/jest.config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
"modulePathIgnorePatterns": ["dist"],
66
"testEnvironment": "node",
77
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
8-
"collectCoverageFrom": ["src/**/*.ts"]
8+
"collectCoverageFrom": ["src/**/*.ts"],
9+
"moduleNameMapper": {
10+
"@launchdarkly/akamai-edgeworker-sdk-common": "<rootDir>../../shared/akamai-edgeworker-sdk/src"
11+
}
912
}
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(): 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.

packages/sdk/akamai-edgekv/jest.config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
"modulePathIgnorePatterns": ["dist"],
66
"testEnvironment": "node",
77
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
8-
"collectCoverageFrom": ["src/**/*.ts"]
8+
"collectCoverageFrom": ["src/**/*.ts"],
9+
"moduleNameMapper": {
10+
"@launchdarkly/akamai-edgeworker-sdk-common": "<rootDir>../../shared/akamai-edgeworker-sdk/src"
11+
}
912
}

packages/sdk/akamai-edgekv/src/__tests__/edgeKVProvider.test.ts renamed to packages/sdk/akamai-edgekv/src/__tests__/edgekv/edgeKVProvider.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import EdgeKVProvider from '../edgekv/edgeKVProvider';
2-
import { EdgeKV } from '../edgekv/edgekv';
1+
import EdgeKVProvider from '../../edgekv/edgeKVProvider';
2+
import { EdgeKV } from '../../edgekv/edgekv';
33

4-
jest.mock('../edgekv/edgekv', () => ({
4+
jest.mock('../../edgekv/edgekv', () => ({
55
EdgeKV: jest.fn(),
66
}));
77

0 commit comments

Comments
 (0)