Skip to content

Commit 7f26541

Browse files
authored
Split getJSONAssignment into getJSONStringAssignment and getParsedJSONAssignment by bumping common library version [FF-899] (#27)
* Update tests * Commit pre-commit file * Increment common library version and help jest find in node_modules * increment common version to use IEppoClient with getParsedJSONAssignment
1 parent d0f2ab0 commit 7f26541

File tree

8 files changed

+159
-23
lines changed

8 files changed

+159
-23
lines changed

.eslintrc.pre-commit.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
node: true,
5+
es6: true,
6+
jest: true,
7+
},
8+
extends: [
9+
'eslint:recommended',
10+
'plugin:@typescript-eslint/eslint-recommended',
11+
'plugin:@typescript-eslint/recommended',
12+
'plugin:promise/recommended',
13+
'plugin:import/recommended',
14+
],
15+
plugins: ['@typescript-eslint', 'import', 'promise'],
16+
rules: {
17+
'@typescript-eslint/ban-ts-comment': 'warn',
18+
'import/namespace': 'off',
19+
'import/named': 'off',
20+
'import/no-unresolved': 'off',
21+
'import/order': [
22+
'warn',
23+
{
24+
pathGroups: [
25+
{
26+
pattern: 'src/**',
27+
group: 'parent',
28+
position: 'before',
29+
},
30+
],
31+
groups: ['builtin', 'external', 'parent', 'sibling', 'index'],
32+
'newlines-between': 'always',
33+
alphabetize: {
34+
order: 'asc' /* sort in ascending order. Options: ['ignore', 'asc', 'desc'] */,
35+
caseInsensitive: true /* ignore case. Options: [true, false] */,
36+
},
37+
},
38+
],
39+
},
40+
};

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ temp
88
yarn-error.log
99

1010
test/data
11+
12+
.eslintcache

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
moduleNameMapper: {
55
'^src/(.*)': ['<rootDir>/src/$1'],
66
'^test/(.*)': ['<rootDir>/test/$1'],
7+
'@eppo(.*)': '<rootDir>/node_modules/@eppo/$1',
78
},
89
testRegex: '.*\\..*spec\\.ts$',
910
transform: {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"xhr-mock": "^2.5.1"
5858
},
5959
"dependencies": {
60-
"@eppo/js-client-sdk-common": "^1.4.1",
60+
"@eppo/js-client-sdk-common": "^1.5.1",
6161
"axios": "^0.27.2",
6262
"md5": "^2.3.0"
6363
}

src/index.spec.ts

Lines changed: 102 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
* @jest-environment jsdom
33
*/
44

5+
import { EppoValue } from '@eppo/js-client-sdk-common/dist/eppo_value';
56
import * as td from 'testdouble';
67
import mock from 'xhr-mock';
78

89
import {
910
IAssignmentTestCase,
11+
ValueTestType,
1012
readAssignmentTestData,
1113
readMockRacResponse,
1214
} from '../test/testHelpers';
@@ -207,22 +209,37 @@ describe('EppoJSClient E2E test', () => {
207209
'test variation assignment splits',
208210
({
209211
experiment,
210-
valueType = 'string',
212+
valueType = ValueTestType.StringType,
211213
subjects,
212214
subjectsWithAttributes,
213215
expectedAssignments,
214216
}: IAssignmentTestCase) => {
215-
`---- Test Case for ${experiment} Experiment ----`;
216-
217-
if (valueType === 'string') {
218-
const assignments = subjectsWithAttributes
219-
? getAssignmentsWithSubjectAttributes(subjectsWithAttributes, experiment)
220-
: getAssignments(subjects, experiment);
221-
expect(assignments).toEqual(expectedAssignments);
222-
expect(assignments.length).toBeGreaterThan(0);
223-
} else {
224-
// skip for now
225-
expect(true).toBe(true);
217+
console.log(`---- Test Case for ${experiment} Experiment ----`);
218+
const assignments = subjectsWithAttributes
219+
? getAssignmentsWithSubjectAttributes(subjectsWithAttributes, experiment, valueType)
220+
: getAssignments(subjects, experiment, valueType);
221+
222+
switch (valueType) {
223+
case ValueTestType.BoolType: {
224+
const boolAssignments = assignments.map((a) => a?.boolValue ?? null);
225+
expect(boolAssignments).toEqual(expectedAssignments);
226+
break;
227+
}
228+
case ValueTestType.NumericType: {
229+
const numericAssignments = assignments.map((a) => a?.numericValue ?? null);
230+
expect(numericAssignments).toEqual(expectedAssignments);
231+
break;
232+
}
233+
case ValueTestType.StringType: {
234+
const stringAssignments = assignments.map((a) => a?.stringValue ?? null);
235+
expect(stringAssignments).toEqual(expectedAssignments);
236+
break;
237+
}
238+
case ValueTestType.JSONType: {
239+
const jsonStringAssignments = assignments.map((a) => a?.stringValue ?? null);
240+
expect(jsonStringAssignments).toEqual(expectedAssignments);
241+
break;
242+
}
226243
}
227244
},
228245
);
@@ -232,9 +249,35 @@ describe('EppoJSClient E2E test', () => {
232249
});
233250
});
234251

235-
function getAssignments(subjects: string[], experiment: string): (string | null)[] {
252+
function getAssignments(
253+
subjects: string[],
254+
experiment: string,
255+
valueTestType: ValueTestType = ValueTestType.StringType,
256+
): (EppoValue | null)[] {
236257
return subjects.map((subjectKey) => {
237-
return globalClient.getAssignment(subjectKey, experiment);
258+
switch (valueTestType) {
259+
case ValueTestType.BoolType: {
260+
const ba = globalClient.getBoolAssignment(subjectKey, experiment);
261+
if (ba === null) return null;
262+
return EppoValue.Bool(ba);
263+
}
264+
case ValueTestType.NumericType: {
265+
const na = globalClient.getNumericAssignment(subjectKey, experiment);
266+
if (na === null) return null;
267+
return EppoValue.Numeric(na);
268+
}
269+
case ValueTestType.StringType: {
270+
const sa = globalClient.getStringAssignment(subjectKey, experiment);
271+
if (sa === null) return null;
272+
return EppoValue.String(sa);
273+
}
274+
case ValueTestType.JSONType: {
275+
const sa = globalClient.getJSONStringAssignment(subjectKey, experiment);
276+
const oa = globalClient.getParsedJSONAssignment(subjectKey, experiment);
277+
if (oa == null || sa === null) return null;
278+
return EppoValue.JSON(sa, oa);
279+
}
280+
}
238281
});
239282
}
240283

@@ -245,9 +288,52 @@ describe('EppoJSClient E2E test', () => {
245288
subjectAttributes: Record<string, any>;
246289
}[],
247290
experiment: string,
248-
): (string | null)[] {
291+
valueTestType: ValueTestType = ValueTestType.StringType,
292+
): (EppoValue | null)[] {
249293
return subjectsWithAttributes.map((subject) => {
250-
return globalClient.getAssignment(subject.subjectKey, experiment, subject.subjectAttributes);
294+
switch (valueTestType) {
295+
case ValueTestType.BoolType: {
296+
const ba = globalClient.getBoolAssignment(
297+
subject.subjectKey,
298+
experiment,
299+
subject.subjectAttributes,
300+
);
301+
if (ba === null) return null;
302+
return EppoValue.Bool(ba);
303+
}
304+
case ValueTestType.NumericType: {
305+
const na = globalClient.getNumericAssignment(
306+
subject.subjectKey,
307+
experiment,
308+
subject.subjectAttributes,
309+
);
310+
if (na === null) return null;
311+
return EppoValue.Numeric(na);
312+
}
313+
case ValueTestType.StringType: {
314+
const sa = globalClient.getStringAssignment(
315+
subject.subjectKey,
316+
experiment,
317+
subject.subjectAttributes,
318+
);
319+
if (sa === null) return null;
320+
return EppoValue.String(sa);
321+
}
322+
case ValueTestType.JSONType: {
323+
const sa = globalClient.getJSONStringAssignment(
324+
subject.subjectKey,
325+
experiment,
326+
subject.subjectAttributes,
327+
);
328+
const oa = globalClient.getParsedJSONAssignment(
329+
subject.subjectKey,
330+
experiment,
331+
subject.subjectAttributes,
332+
);
333+
if (oa == null || sa === null) return null;
334+
return EppoValue.JSON(sa, oa);
335+
}
336+
}
251337
});
252338
}
253339
});

test/testHelpers.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ export const TEST_DATA_DIR = './test/data/';
77
export const ASSIGNMENT_TEST_DATA_DIR = TEST_DATA_DIR + 'assignment-v2/';
88
export const MOCK_RAC_RESPONSE_FILE = 'rac-experiments-v3.json';
99

10+
export enum ValueTestType {
11+
BoolType = 'boolean',
12+
NumericType = 'numeric',
13+
StringType = 'string',
14+
JSONType = 'json',
15+
}
16+
1017
export interface IAssignmentTestCase {
1118
experiment: string;
12-
valueType: string;
19+
valueType: ValueTestType;
1320
percentExposure: number;
1421
variations: IVariation[];
1522
subjects: string[];

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
"src/**/*.spec.ts",
1616
"test/**/*"
1717
]
18-
}
18+
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@
289289
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.7.tgz#1d572bfbbe14b7704e0ba0f39b74815b84870d70"
290290
integrity sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==
291291

292-
"@eppo/js-client-sdk-common@^1.4.1":
293-
version "1.4.1"
294-
resolved "https://registry.yarnpkg.com/@eppo/js-client-sdk-common/-/js-client-sdk-common-1.4.1.tgz#b75bb34c9004ea34070be38250532e04d2683e83"
295-
integrity sha512-1dPursZSgtJb7ufLbePI1qIryydFTP6Y+EJYiV+I1njhBEdIUjc/E1+Qe3kBDSXoyowrk8VjUZGV4RD4YnPhcA==
292+
"@eppo/js-client-sdk-common@^1.5.1":
293+
version "1.5.1"
294+
resolved "https://registry.yarnpkg.com/@eppo/js-client-sdk-common/-/js-client-sdk-common-1.5.1.tgz#f4f86c17aaf8da565613a85ffc6b5f133a382a6a"
295+
integrity sha512-cO7lkqO1I0d3aOu43+jeiUTNFn4Rkrg95Fc6ADhLFwYjV3b0hI7KRUPE66YadjgP3Db3J2XgBWtQps2XFbfe5Q==
296296
dependencies:
297297
axios "^0.27.2"
298298
md5 "^2.3.0"

0 commit comments

Comments
 (0)