Skip to content

Commit b43c8f0

Browse files
Update tests
1 parent 27894b0 commit b43c8f0

12 files changed

+149
-71
lines changed

src/__tests__/browserSuites/impressions-listener.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,21 @@ export default function (assert) {
5757
keyName: 'marcio@split.io',
5858
treatment: 'no',
5959
bucketingKey: 'impr_bucketing_2',
60-
label: 'default rule'
60+
label: 'default rule',
61+
pt: undefined
6162
};
6263

6364
assert.equal(listener.logImpression.callCount, 4, 'Impression listener logImpression method should be called after we call client.getTreatment, once per each impression generated.');
64-
assert.true(listener.logImpression.getCall(0).calledWithMatch({
65+
assert.true(listener.logImpression.getCall(0).calledWithExactly({
6566
impression: {
6667
feature: 'hierarchical_splits_test',
6768
keyName: 'nicolas@split.io',
6869
treatment: 'on',
70+
time: listener.logImpression.getCall(0).args[0].impression.time,
6971
bucketingKey: undefined,
7072
label: 'expected label',
73+
changeNumber: 2828282828,
74+
pt: undefined
7175
},
7276
attributes: undefined,
7377
...metaData

src/__tests__/browserSuites/impressions.debug.spec.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import splitChangesMock1 from '../mocks/splitchanges.since.-1.json';
44
import splitChangesMock2 from '../mocks/splitchanges.since.1457552620999.json';
55
import membershipsFacundo from '../mocks/memberships.facundo@split.io.json';
66
import { DEBUG } from '@splitsoftware/splitio-commons/src/utils/constants';
7+
import { truncateTimeFrame } from '@splitsoftware/splitio-commons/src/utils/time';
78
import { url } from '../testUtils';
89

910
const baseUrls = {
@@ -19,6 +20,8 @@ const settings = settingsFactory({
1920
streamingEnabled: false
2021
});
2122

23+
let truncatedTimeFrame;
24+
2225
export default function (fetchMock, assert) {
2326
// Mocking this specific route to make sure we only get the items we want to test from the handlers.
2427
fetchMock.getOnce(url(settings, '/splitChanges?s=1.2&since=-1'), { status: 200, body: splitChangesMock1 });
@@ -47,41 +50,21 @@ export default function (fetchMock, assert) {
4750
});
4851

4952
const client = splitio.client();
50-
const assertPayload = req => {
51-
const resp = JSON.parse(req.body);
52-
53-
assert.equal(resp.length, 1, 'We performed three evaluations so we should have 1 impressions type');
54-
55-
const alwaysOnWithConfigImpr = resp.filter(e => e.f === 'split_with_config')[0];
56-
57-
assert.equal(alwaysOnWithConfigImpr.i.length, 3);
58-
59-
function validateImpressionData(output, expected) {
60-
assert.equal(output.k, expected.keyName, 'Present impressions should have the correct key.');
61-
assert.equal(output.b, expected.bucketingKey, 'Present impressions should have the correct bucketingKey.');
62-
assert.equal(output.t, expected.treatment, 'Present impressions should have the correct treatment.');
63-
assert.equal(output.r, expected.label, 'Present impressions should have the correct label.');
64-
assert.equal(output.c, expected.changeNumber, 'Present impressions should have the correct changeNumber.');
65-
assert.equal(output.pt, expected.pt, 'Present impressions should have the correct previousTime.');
66-
}
67-
68-
validateImpressionData(alwaysOnWithConfigImpr.i[0], {
69-
keyName: 'facundo@split.io', label: 'another expected label', treatment: 'o.n',
70-
bucketingKey: undefined, changeNumber: 828282828282, pt: undefined
71-
});
72-
validateImpressionData(alwaysOnWithConfigImpr.i[1], {
73-
keyName: 'facundo@split.io', label: 'another expected label', treatment: 'o.n',
74-
bucketingKey: undefined, changeNumber: 828282828282, pt: alwaysOnWithConfigImpr.i[0].m
75-
});
76-
validateImpressionData(alwaysOnWithConfigImpr.i[2], {
77-
keyName: 'facundo@split.io', label: 'another expected label', treatment: 'o.n',
78-
bucketingKey: undefined, changeNumber: 828282828282, pt: alwaysOnWithConfigImpr.i[1].m
79-
});
80-
};
8153

8254
fetchMock.postOnce(url(settings, '/testImpressions/bulk'), (url, req) => {
8355
assert.equal(req.headers.SplitSDKImpressionsMode, DEBUG);
84-
assertPayload(req);
56+
const data = JSON.parse(req.body);
57+
58+
assert.deepEqual(data, [{
59+
f: 'split_with_config',
60+
i: [{
61+
k: 'facundo@split.io', t: 'o.n', m: data[0].i[0].m, c: 828282828282, r: 'another expected label'
62+
}, {
63+
k: 'facundo@split.io', t: 'o.n', m: data[0].i[1].m, c: 828282828282, r: 'another expected label', pt: data[0].i[0].m,
64+
}, {
65+
k: 'facundo@split.io', t: 'o.n', m: data[0].i[2].m, c: 828282828282, r: 'another expected label', pt: data[0].i[1].m
66+
}]
67+
}]);
8568

8669
client.destroy().then(() => {
8770
assert.end();
@@ -90,9 +73,28 @@ export default function (fetchMock, assert) {
9073
return 200;
9174
});
9275

76+
fetchMock.postOnce(url(settings, '/testImpressions/count'), (url, opts) => {
77+
assert.deepEqual(JSON.parse(opts.body), {
78+
pf: [{ f: 'always_on_track_impressions_false', m: truncatedTimeFrame, rc: 1 }]
79+
}, 'We should generate impression count for the feature with track impressions disabled.');
80+
81+
return 200;
82+
});
83+
84+
fetchMock.postOnce(url(settings, '/v1/keys/cs'), (url, opts) => {
85+
assert.deepEqual(JSON.parse(opts.body), {
86+
keys: [{ fs: ['always_on_track_impressions_false'], k: 'facundo@split.io' }]
87+
}, 'We should track unique keys for the feature with track impressions disabled.');
88+
89+
return 200;
90+
});
91+
9392
client.ready().then(() => {
93+
truncatedTimeFrame = truncateTimeFrame(Date.now());
94+
9495
client.getTreatment('split_with_config');
9596
client.getTreatment('split_with_config');
9697
client.getTreatment('split_with_config');
98+
assert.equal(client.getTreatment('always_on_track_impressions_false'), 'on');
9799
});
98100
}

src/__tests__/browserSuites/impressions.spec.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,19 @@ export default function (fetchMock, assert) {
4949
const assertPayload = req => {
5050
const resp = JSON.parse(req.body);
5151

52-
assert.equal(resp.length, 2, 'We performed three evaluations so we should have 2 impressions type');
52+
assert.equal(resp.length, 2, 'We performed evaluations for 3 features, but one with `impressionsDisabled` true, so we should have 2 items total');
5353

5454
const dependencyChildImpr = resp.filter(e => e.f === 'hierarchical_splits_test')[0];
55-
const alwaysOnWithConfigImpr = resp.filter(e => e.f === 'split_with_config')[0];
55+
const splitWithConfigImpr = resp.filter(e => e.f === 'split_with_config')[0];
56+
const alwaysOnWithTrackImpressionsFalse = resp.filter(e => e.f === 'always_on_track_impressions_false');
5657

5758
assert.true(dependencyChildImpr, 'Split we wanted to evaluate should be present on the impressions.');
5859
assert.false(resp.some(e => e.f === 'hierarchical_dep_always_on'), 'Parent split evaluations should not result in impressions.');
5960
assert.false(resp.some(e => e.f === 'hierarchical_dep_hierarchical'), 'No matter how deep is the chain.');
60-
assert.true(alwaysOnWithConfigImpr, 'Split evaluated with config should have generated an impression too.');
61-
assert.false(Object.prototype.hasOwnProperty.call(alwaysOnWithConfigImpr.i[0], 'configuration'), 'Impressions do not change with configuration evaluations.');
62-
assert.false(Object.prototype.hasOwnProperty.call(alwaysOnWithConfigImpr.i[0], 'config'), 'Impressions do not change with configuration evaluations.');
61+
assert.true(splitWithConfigImpr, 'Split evaluated with config should have generated an impression too.');
62+
assert.false(Object.prototype.hasOwnProperty.call(splitWithConfigImpr.i[0], 'configuration'), 'Impressions do not change with configuration evaluations.');
63+
assert.false(Object.prototype.hasOwnProperty.call(splitWithConfigImpr.i[0], 'config'), 'Impressions do not change with configuration evaluations.');
64+
assert.equal(alwaysOnWithTrackImpressionsFalse.length, 0);
6365

6466
const {
6567
k,
@@ -94,18 +96,26 @@ export default function (fetchMock, assert) {
9496
fetchMock.postOnce(url(settings, '/testImpressions/count'), (url, opts) => {
9597
const data = JSON.parse(opts.body);
9698

97-
assert.equal(data.pf.length, 1, 'We should generate impressions count for one feature.');
99+
assert.equal(data.pf.length, 2, 'We should generate impressions count for 2 features.');
98100

99101
// finding these validate the feature names collection too
100-
const dependencyChildImpr = data.pf.filter(e => e.f === 'hierarchical_splits_test')[0];
101-
const alwaysOnWithConfigImpr = data.pf.filter(e => e.f === 'split_with_config')[0];
102+
const splitWithConfigImpr = data.pf.filter(e => e.f === 'split_with_config')[0];
103+
const alwaysOnWithTrackImpressionsFalse = data.pf.filter(e => e.f === 'always_on_track_impressions_false')[0];
102104

103-
assert.equal(dependencyChildImpr.rc, 1);
104-
assert.equal(typeof dependencyChildImpr.m, 'number');
105-
assert.equal(dependencyChildImpr.m, truncatedTimeFrame);
106-
assert.equal(alwaysOnWithConfigImpr.rc, 3);
107-
assert.equal(typeof alwaysOnWithConfigImpr.m, 'number');
108-
assert.equal(alwaysOnWithConfigImpr.m, truncatedTimeFrame);
105+
assert.equal(splitWithConfigImpr.rc, 2);
106+
assert.equal(typeof splitWithConfigImpr.m, 'number');
107+
assert.equal(splitWithConfigImpr.m, truncatedTimeFrame);
108+
assert.equal(alwaysOnWithTrackImpressionsFalse.rc, 1);
109+
assert.equal(typeof alwaysOnWithTrackImpressionsFalse.m, 'number');
110+
assert.equal(alwaysOnWithTrackImpressionsFalse.m, truncatedTimeFrame);
111+
112+
return 200;
113+
});
114+
115+
fetchMock.postOnce(url(settings, '/v1/keys/cs'), (url, opts) => {
116+
assert.deepEqual(JSON.parse(opts.body), {
117+
keys: [{ fs: [ 'always_on_track_impressions_false' ], k: 'facundo@split.io' }]
118+
}, 'We should only track unique keys for features flags with track impressions disabled.');
109119

110120
return 200;
111121
});
@@ -120,5 +130,8 @@ export default function (fetchMock, assert) {
120130
}, 'We should get an evaluation as always.');
121131
client.getTreatmentWithConfig('split_with_config');
122132
client.getTreatmentWithConfig('split_with_config');
133+
134+
// Impression should not be tracked
135+
assert.equal(client.getTreatment('always_on_track_impressions_false'), 'on');
123136
});
124137
}

src/__tests__/browserSuites/manager.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export default async function (settings, fetchMock, assert) {
4141
'treatments': map(mockSplits.splits[index].conditions[0].partitions, partition => partition.treatment),
4242
'configs': mockSplits.splits[index].configurations || {},
4343
'sets': mockSplits.splits[index].sets,
44-
'defaultTreatment': mockSplits.splits[index].defaultTreatment
44+
'defaultTreatment': mockSplits.splits[index].defaultTreatment,
45+
'impressionsDisabled': false
4546
});
4647

4748
assert.equal(manager.split('non_existent'), null, 'Trying to get a manager.split() of a Split that does not exist returns null.');

src/__tests__/browserSuites/telemetry.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default async function telemetryBrowserSuite(fetchMock, assert) {
7171

7272
// @TODO check if iDe value is correct
7373
assert.deepEqual(data, {
74-
mE: {}, hE: { sp: { 500: 1 }, ms: { 500: 1 } }, tR: 0, aR: 0, iQ: 4, iDe: 1, iDr: 0, spC: 32, seC: 1, skC: 1, eQ: 1, eD: 0, sE: [], t: [], ufs: {}
74+
mE: {}, hE: { sp: { 500: 1 }, ms: { 500: 1 } }, tR: 0, aR: 0, iQ: 4, iDe: 1, iDr: 0, spC: 33, seC: 1, skC: 1, eQ: 1, eD: 0, sE: [], t: [], ufs: {}
7575
}, 'metrics/usage JSON payload should be the expected');
7676

7777
finish.next();
@@ -91,7 +91,7 @@ export default async function telemetryBrowserSuite(fetchMock, assert) {
9191
// @TODO check if iDe value is correct
9292
assert.deepEqual(data, {
9393
mL: {}, mE: {}, hE: {}, hL: {}, // errors and latencies were popped
94-
tR: 0, aR: 0, iQ: 4, iDe: 1, iDr: 0, spC: 32, seC: 1, skC: 1, eQ: 1, eD: 0, sE: [], t: [], ufs: {}
94+
tR: 0, aR: 0, iQ: 4, iDe: 1, iDr: 0, spC: 33, seC: 1, skC: 1, eQ: 1, eD: 0, sE: [], t: [], ufs: {}
9595
}, '2nd metrics/usage JSON payload should be the expected');
9696
return 200;
9797
});

src/__tests__/consumer/browser_consumer.spec.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { version } from '../../../package.json';
1010
import { SplitFactory, PluggableStorage } from '../../';
1111

1212
const expectedSplitName = 'hierarchical_splits_testing_on';
13-
const expectedSplitView = { name: 'hierarchical_splits_testing_on', trafficType: 'user', killed: false, changeNumber: 1487277320548, treatments: ['on', 'off'], configs: {}, sets: [], defaultTreatment: 'off' };
13+
const expectedSplitView = { name: 'hierarchical_splits_testing_on', trafficType: 'user', killed: false, changeNumber: 1487277320548, treatments: ['on', 'off'], configs: {}, sets: [], defaultTreatment: 'off', impressionsDisabled: false };
1414

1515
const wrapperPrefix = 'PLUGGABLE_STORAGE_UT';
1616
const wrapperInstance = inMemoryWrapperFactory();
@@ -111,11 +111,12 @@ tape('Browser Consumer mode with pluggable storage', function (t) {
111111

112112
assert.equal(await client.getTreatment('always-on'), 'on', 'Evaluations using pluggable storage should be correct.');
113113

114-
// Below splits were added manually.
114+
// Below feature flags were added manually.
115115
// They are all_keys (always evaluate to on) which depend from always-on split. the _on/off is what treatment they are expecting there.
116116
assert.equal(await client.getTreatment('hierarchical_splits_testing_on'), 'on', 'Evaluations using pluggable storage should be correct.');
117117
assert.equal(await client.getTreatment('hierarchical_splits_testing_off'), 'off', 'Evaluations using pluggable storage should be correct.');
118118
assert.equal(await client.getTreatment('hierarchical_splits_testing_on_negated'), 'off', 'Evaluations using pluggable storage should be correct.');
119+
assert.equal(await client.getTreatment('always-on-impressions-disabled-true'), 'on', 'Evaluations using pluggable storage should be correct.');
119120

120121
assert.equal(typeof client.track('user', 'test.event', 18).then, 'function', 'Track calls should always return a promise on Consumer mode.');
121122
assert.equal(typeof client.track().then, 'function', 'Track calls should always return a promise on Consumer mode, even when parameters are incorrect.');
@@ -125,11 +126,11 @@ tape('Browser Consumer mode with pluggable storage', function (t) {
125126

126127
// Manager methods
127128
const splitNames = await manager.names();
128-
assert.equal(splitNames.length, 25, 'manager `names` method returns the list of split names asynchronously');
129+
assert.equal(splitNames.length, 26, 'manager `names` method returns the list of split names asynchronously');
129130
assert.equal(splitNames.indexOf(expectedSplitName) > -1, true, 'list of split names should contain expected splits');
130131
assert.deepEqual(await manager.split(expectedSplitName), expectedSplitView, 'manager `split` method returns the split view of the given split name asynchronously');
131132
const splitViews = await manager.splits();
132-
assert.equal(splitViews.length, 25, 'manager `splits` method returns the list of split views asynchronously');
133+
assert.equal(splitViews.length, 26, 'manager `splits` method returns the list of split views asynchronously');
133134
assert.deepEqual(splitViews.find(splitView => splitView.name === expectedSplitName), expectedSplitView, 'manager `split` method returns the split view of the given split name asynchronously');
134135

135136
// New shared client created
@@ -164,7 +165,7 @@ tape('Browser Consumer mode with pluggable storage', function (t) {
164165

165166
// Assert impressionsListener
166167
setTimeout(() => {
167-
assert.equal(impressions.length, TOTAL_RAW_IMPRESSIONS, 'Each evaluation has its corresponding impression');
168+
assert.equal(impressions.length, TOTAL_RAW_IMPRESSIONS + 1 /* One evaluation with impressionsDisabled true */, 'Each evaluation has its corresponding impression');
168169
assert.equal(impressions[0].impression.label, SDK_NOT_READY, 'The first impression is control with label "sdk not ready"');
169170

170171
assert.end();
@@ -251,7 +252,7 @@ tape('Browser Consumer mode with pluggable storage', function (t) {
251252

252253
assert.equal(await client.getTreatment('always-on'), 'on', 'Evaluations using pluggable storage should be correct.');
253254

254-
// Below splits were added manually.
255+
// Below feature flags were added manually.
255256
// They are all_keys (always evaluate to on) which depend from always-on split. the _on/off is what treatment they are expecting there.
256257
assert.equal(await client.getTreatment('hierarchical_splits_testing_on'), 'on', 'Evaluations using pluggable storage should be correct.');
257258
assert.equal(await client.getTreatment('hierarchical_splits_testing_off'), 'off', 'Evaluations using pluggable storage should be correct.');
@@ -383,7 +384,7 @@ tape('Browser Consumer mode with pluggable storage', function (t) {
383384

384385
assert.equal(await client.getTreatment('always-on'), 'on', 'Evaluations using pluggable storage should be correct.');
385386

386-
// Below splits were added manually.
387+
// Below feature flags were added manually.
387388
// They are all_keys (always evaluate to on) which depend from always-on split. the _on/off is what treatment they are expecting there.
388389
assert.equal(await client.getTreatment('hierarchical_splits_testing_on'), 'on', 'Evaluations using pluggable storage should be correct.');
389390
assert.equal(await client.getTreatment('hierarchical_splits_testing_off'), 'off', 'Evaluations using pluggable storage should be correct.');

0 commit comments

Comments
 (0)