Skip to content

Commit 5174cb9

Browse files
Merge pull request #65 from splitio/fme-7166
[FME-7166] Add impressions properties
2 parents 35d103c + baba000 commit 5174cb9

File tree

4 files changed

+200
-72
lines changed

4 files changed

+200
-72
lines changed

projects/splitio/src/lib/__tests__/splitio.service.spec.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ describe('SplitService', () => {
144144

145145
service.getTreatment('test_split');
146146
expect(clientSpy.getTreatment.mock.calls[0]).toEqual(['test_split', undefined]);
147-
service.getTreatmentWithConfig('test_split', {attr: true});
148-
expect(clientSpy.getTreatmentWithConfig.mock.calls[0]).toEqual(['test_split', {attr: true}]);
149-
service.getTreatments(['test_split','test_split2'], {attr: true});
150-
expect(clientSpy.getTreatments.mock.calls[0]).toEqual([['test_split','test_split2'], {attr: true}]);
147+
service.getTreatmentWithConfig('test_split', {attr: true}, {properties: { enabled: true }});
148+
expect(clientSpy.getTreatmentWithConfig.mock.calls[0]).toEqual(['test_split', {attr: true}, {properties: { enabled: true }}]);
149+
service.getTreatments(['test_split','test_split2'], {attr: true}, {properties: { enabled: true }});
150+
expect(clientSpy.getTreatments.mock.calls[0]).toEqual([['test_split','test_split2'], {attr: true}, {properties: { enabled: true }}]);
151151
service.getTreatmentsWithConfig(['test_split','test_split2']);
152-
expect(clientSpy.getTreatmentsWithConfig.mock.calls[0]).toEqual([['test_split','test_split2'], undefined]);
152+
expect(clientSpy.getTreatmentsWithConfig.mock.calls[0]).toEqual([['test_split','test_split2']]);
153153

154154
const sharedClientKey = 'myKey2';
155155
// initialize shared client and wait for ready
@@ -208,28 +208,28 @@ describe('SplitService', () => {
208208
expect(sharedClientSpy.getTreatmentsWithConfig).toHaveBeenCalledTimes(0);
209209

210210
// verify that main client is not evaluating when evaluates for shared client
211-
expect(service.getTreatment(sharedClientKey, 'test_split', {attr: true})).toEqual('on');
212-
expect(sharedClientSpy.getTreatment.mock.calls[0]).toEqual(['test_split', {attr: true}]);
211+
expect(service.getTreatment(sharedClientKey, 'test_split', {attr: true}, { properties: { enabled: true }})).toEqual('on');
212+
expect(sharedClientSpy.getTreatment.mock.calls[0]).toEqual(['test_split', {attr: true}, { properties: { enabled: true }}]);
213213
expect(clientSpy.getTreatment).toHaveBeenCalledTimes(1);
214-
215-
expect(service.getTreatmentWithConfig(sharedClientKey, 'test_split', {attr: true})).toEqual({treatment: 'on', config: null});
216-
expect(sharedClientSpy.getTreatmentWithConfig.mock.calls[0]).toEqual(['test_split', {attr: true}]);
214+
215+
expect(service.getTreatmentWithConfig(sharedClientKey, 'test_split', {attr: true}, { properties: { enabled: true }})).toEqual({treatment: 'on', config: null});
216+
expect(sharedClientSpy.getTreatmentWithConfig.mock.calls[0]).toEqual(['test_split', {attr: true}, { properties: { enabled: true }}]);
217217
expect(clientSpy.getTreatmentWithConfig).toHaveBeenCalledTimes(1);
218218

219-
expect(service.getTreatments(sharedClientKey, ['test_split','test_split2'], {attr: true}))
219+
expect(service.getTreatments(sharedClientKey, ['test_split','test_split2'], {attr: true}, { properties: { enabled: true }}))
220220
.toEqual({
221221
'test_split': 'on',
222222
'test_split2': 'off'
223223
});
224-
expect(sharedClientSpy.getTreatments.mock.calls[0]).toEqual([['test_split','test_split2'], {attr: true}]);
224+
expect(sharedClientSpy.getTreatments.mock.calls[0]).toEqual([['test_split','test_split2'], {attr: true}, { properties: { enabled: true }}]);
225225
expect(clientSpy.getTreatments).toHaveBeenCalledTimes(1);
226226

227-
expect(service.getTreatmentsWithConfig(sharedClientKey, ['test_split','test_split2'], {attr: true}))
227+
expect(service.getTreatmentsWithConfig(sharedClientKey, ['test_split','test_split2'], {attr: true}, { properties: { enabled: true }}))
228228
.toEqual({
229229
test_split: { treatment: 'on', config: null },
230230
test_split2: { treatment: 'off', config: '{"bannerText":"Click here."}' }
231231
});
232-
expect(sharedClientSpy.getTreatmentsWithConfig.mock.calls[0]).toEqual([['test_split','test_split2'], {attr: true}]);
232+
expect(sharedClientSpy.getTreatmentsWithConfig.mock.calls[0]).toEqual([['test_split','test_split2'], {attr: true}, { properties: { enabled: true }}]);
233233
expect(clientSpy.getTreatmentsWithConfig).toHaveBeenCalledTimes(1);
234234

235235
// input validation
@@ -281,10 +281,10 @@ describe('SplitService', () => {
281281

282282
service.getTreatmentsByFlagSet('set_a');
283283
expect(clientSpy.getTreatmentsByFlagSet.mock.calls[0]).toEqual(['set_a', undefined]);
284-
service.getTreatmentsWithConfigByFlagSet('set_a', {attr: true});
285-
expect(clientSpy.getTreatmentsWithConfigByFlagSet.mock.calls[0]).toEqual(['set_a', {attr: true}]);
286-
service.getTreatmentsByFlagSets(['set_a','set_b'], {attr: true});
287-
expect(clientSpy.getTreatmentsByFlagSets.mock.calls[0]).toEqual([['set_a','set_b'], {attr: true}]);
284+
service.getTreatmentsWithConfigByFlagSet('set_a', {attr: true}, { properties: {enabled: true}});
285+
expect(clientSpy.getTreatmentsWithConfigByFlagSet.mock.calls[0]).toEqual(['set_a', {attr: true}, { properties: {enabled: true}}]);
286+
service.getTreatmentsByFlagSets(['set_a','set_b'], {attr: true}, { properties: {enabled: true}});
287+
expect(clientSpy.getTreatmentsByFlagSets.mock.calls[0]).toEqual([['set_a','set_b'], {attr: true}, { properties: {enabled: true}}]);
288288
service.getTreatmentsWithConfigByFlagSets(['set_a','set_b']);
289289
expect(clientSpy.getTreatmentsWithConfigByFlagSets.mock.calls[0]).toEqual([['set_a','set_b'], undefined]);
290290

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { buildInstance, parseTreatmentParams, parseFlagSetParams, parseTrackParams, isString } from '../utils/utils';
2+
3+
describe('utils', () => {
4+
describe('buildInstance', () => {
5+
it('returns key if no bucketingKey', () => {
6+
expect(buildInstance('user1')).toBe('user1');
7+
});
8+
it('returns formatted string for SplitKey object', () => {
9+
// @ts-ignore
10+
expect(buildInstance({ matchingKey: 'm', bucketingKey: 'b' })).toBe('m-b-');
11+
});
12+
it('handles missing matchingKey', () => {
13+
// @ts-ignore
14+
expect(buildInstance({ bucketingKey: 'b' })).toBe('b-b-');
15+
});
16+
it('handles missing bucketingKey', () => {
17+
// @ts-ignore
18+
expect(buildInstance({ matchingKey: 'm' })).toBe('m-m-');
19+
});
20+
});
21+
22+
describe('parseTreatmentParams', () => {
23+
it('parses with key and featureFlagNames', () => {
24+
const result = parseTreatmentParams('user1', 'flag1', { attr: 1 }, { properties: { enabled: true } });
25+
expect(result).toEqual({ key: 'user1', featureFlagNames: 'flag1', attributes: { attr: 1 }, options: { properties: { enabled: true } } });
26+
});
27+
it('parses with key and featureFlagNames as array', () => {
28+
const result = parseTreatmentParams('user1', ['flag1', 'flag2'], { attr: 1 }, { properties: { enabled: true } });
29+
expect(result).toEqual({ key: 'user1', featureFlagNames: ['flag1', 'flag2'], attributes: { attr: 1 }, options: { properties: { enabled: true } } });
30+
});
31+
it('parses with featureFlagNames only', () => {
32+
const result = parseTreatmentParams('flag1', { attr: 1 }, { properties: { enabled: true } });
33+
expect(result).toEqual({ key: undefined, featureFlagNames: 'flag1', attributes: { attr: 1 }, options: { properties: { enabled: true } } });
34+
});
35+
it('parses with featureFlagNames as array only', () => {
36+
const result = parseTreatmentParams(['flag1', 'flag2'], { attr: 1 }, { properties: { enabled: true } });
37+
expect(result).toEqual({ key: undefined, featureFlagNames: ['flag1', 'flag2'], attributes: { attr: 1 }, options: { properties: { enabled: true } } });
38+
});
39+
});
40+
41+
describe('parseFlagSetParams', () => {
42+
it('parses with key and flagSetNames', () => {
43+
const result = parseFlagSetParams('user1', 'flagSet1', { attr: 1 }, { properties: { enabled: true } });
44+
expect(result).toEqual({ key: 'user1', flagSetNames: 'flagSet1', attributes: { attr: 1 }, options: { properties: { enabled: true } } });
45+
});
46+
it('parses with key and flagSetNames as array', () => {
47+
const result = parseFlagSetParams('user1', ['flagSet1', 'flagSet2'], { attr: 1 }, { properties: { enabled: true } });
48+
expect(result).toEqual({ key: 'user1', flagSetNames: ['flagSet1', 'flagSet2'], attributes: { attr: 1 }, options: { properties: { enabled: true } } });
49+
});
50+
it('parses with flagSetNames only', () => {
51+
const result = parseFlagSetParams('flagSet1', { attr: 1 }, { properties: { enabled: true } });
52+
expect(result).toEqual({ key: undefined, flagSetNames: 'flagSet1', attributes: { attr: 1 }, options: { properties: { enabled: true } } });
53+
});
54+
it('parses with flagSetNames as array only', () => {
55+
const result = parseFlagSetParams(['flagSet1', 'flagSet2'], { attr: 1 }, { properties: { enabled: true } });
56+
expect(result).toEqual({ key: undefined, flagSetNames: ['flagSet1', 'flagSet2'], attributes: { attr: 1 }, options: { properties: { enabled: true } } });
57+
});
58+
});
59+
60+
describe('parseTrackParams', () => {
61+
it('parses with key, trafficType, eventType (string), value, properties', () => {
62+
const result = parseTrackParams('user1', 'traffic', 'event', 123, { foo: 'bar' });
63+
expect(result).toEqual({ key: 'user1', trafficType: 'traffic', eventType: 'event', value: 123, properties: { foo: 'bar' } });
64+
});
65+
it('parses with trafficType, eventType, value, properties (eventType as not string)', () => {
66+
const result = parseTrackParams('traffic', 'event', 123, { foo: 'bar' });
67+
expect(result).toEqual({ key: undefined, trafficType: 'traffic', eventType: 'event', value: 123, properties: { foo: 'bar' } });
68+
});
69+
});
70+
71+
describe('isString', () => {
72+
it('returns true for string', () => {
73+
expect(isString('abc')).toBe(true);
74+
});
75+
it('returns true for String object', () => {
76+
expect(isString(new String('abc'))).toBe(true);
77+
});
78+
it('returns false for non-string', () => {
79+
expect(isString(123)).toBe(false);
80+
expect(isString({})).toBe(false);
81+
expect(isString([])).toBe(false);
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)