Skip to content

Commit 46846d8

Browse files
committed
fixed some more unit tsts
1 parent 2da6a9f commit 46846d8

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

packages/optimizely-sdk/lib/core/bucketer/index.tests.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { createLogger } from '../../plugins/logger';
2828
import projectConfig from '../project_config';
2929
import { getTestProjectConfig } from '../../tests/test_data';
3030

31+
var buildLogMessageFromArgs = args => sprintf(args[1], ...args.splice(2));
3132
var testData = getTestProjectConfig();
3233

3334
describe('lib/core/bucketer', function() {
@@ -75,7 +76,7 @@ describe('lib/core/bucketer', function() {
7576
var decisionResponse = bucketer.bucket(bucketerParamsTest1);
7677
expect(decisionResponse.result).to.equal('111128');
7778

78-
var bucketedUser_log1 = createdLogger.log.args[0][1];
79+
var bucketedUser_log1 = buildLogMessageFromArgs(createdLogger.log.args[0]);
7980
expect(bucketedUser_log1).to.equal(
8081
sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, 'BUCKETER', '50', 'ppid1')
8182
);
@@ -84,7 +85,7 @@ describe('lib/core/bucketer', function() {
8485
bucketerParamsTest2.userId = 'ppid2';
8586
expect(bucketer.bucket(bucketerParamsTest2).result).to.equal(null);
8687

87-
var notBucketedUser_log1 = createdLogger.log.args[1][1];
88+
var notBucketedUser_log1 = buildLogMessageFromArgs(createdLogger.log.args[1]);
8889

8990
expect(notBucketedUser_log1).to.equal(
9091
sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, 'BUCKETER', '50000', 'ppid2')
@@ -137,12 +138,12 @@ describe('lib/core/bucketer', function() {
137138
sinon.assert.calledTwice(bucketerStub);
138139
sinon.assert.callCount(createdLogger.log, 3);
139140

140-
var log1 = createdLogger.log.args[0][1];
141+
var log1 = buildLogMessageFromArgs(createdLogger.log.args[0]);
141142
expect(log1).to.equal(
142143
sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, 'BUCKETER', '50', 'testUser')
143144
);
144145

145-
var log2 = createdLogger.log.args[1][1];
146+
var log2 = buildLogMessageFromArgs(createdLogger.log.args[1]);
146147
expect(log2).to.equal(
147148
sprintf(
148149
LOG_MESSAGES.USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP,
@@ -153,7 +154,7 @@ describe('lib/core/bucketer', function() {
153154
)
154155
);
155156

156-
var log3 = createdLogger.log.args[2][1];
157+
var log3 = buildLogMessageFromArgs(createdLogger.log.args[2]);
157158
expect(log3).to.equal(
158159
sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, 'BUCKETER', '50', 'testUser')
159160
);
@@ -168,11 +169,11 @@ describe('lib/core/bucketer', function() {
168169
sinon.assert.calledOnce(bucketerStub);
169170
sinon.assert.calledTwice(createdLogger.log);
170171

171-
var log1 = createdLogger.log.args[0][1];
172+
var log1 = buildLogMessageFromArgs(createdLogger.log.args[0]);
172173
expect(log1).to.equal(
173174
sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, 'BUCKETER', '5000', 'testUser')
174175
);
175-
var log2 = createdLogger.log.args[1][1];
176+
var log2 = buildLogMessageFromArgs(createdLogger.log.args[1]);
176177
expect(log2).to.equal(
177178
sprintf(
178179
LOG_MESSAGES.USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP,
@@ -193,11 +194,11 @@ describe('lib/core/bucketer', function() {
193194
sinon.assert.calledOnce(bucketerStub);
194195
sinon.assert.calledTwice(createdLogger.log);
195196

196-
var log1 = createdLogger.log.args[0][1];
197+
var log1 = buildLogMessageFromArgs(createdLogger.log.args[0]);
197198
expect(log1).to.equal(
198199
sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, 'BUCKETER', '50000', 'testUser')
199200
);
200-
var log2 = createdLogger.log.args[1][1];
201+
var log2 = buildLogMessageFromArgs(createdLogger.log.args[1]);
201202
expect(log2).to.equal(sprintf(LOG_MESSAGES.USER_NOT_IN_ANY_EXPERIMENT, 'BUCKETER', 'testUser', '666'));
202203
});
203204

@@ -210,11 +211,11 @@ describe('lib/core/bucketer', function() {
210211
sinon.assert.calledOnce(bucketerStub);
211212
sinon.assert.calledTwice(createdLogger.log);
212213

213-
var log1 = createdLogger.log.args[0][1];
214+
var log1 = buildLogMessageFromArgs(createdLogger.log.args[0]);
214215
expect(log1).to.equal(
215216
sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, 'BUCKETER', '9000', 'testUser')
216217
);
217-
var log2 = createdLogger.log.args[1][1];
218+
var log2 = buildLogMessageFromArgs(createdLogger.log.args[1]);
218219
expect(log2).to.equal(sprintf(LOG_MESSAGES.USER_NOT_IN_ANY_EXPERIMENT, 'BUCKETER', 'testUser', '666'));
219220
});
220221

@@ -252,7 +253,7 @@ describe('lib/core/bucketer', function() {
252253
sinon.assert.calledOnce(bucketerStub);
253254
sinon.assert.calledOnce(createdLogger.log);
254255

255-
var log1 = createdLogger.log.args[0][1];
256+
var log1 = buildLogMessageFromArgs(createdLogger.log.args[0]);
256257
expect(log1).to.equal(sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_EXPERIMENT_BUCKET, 'BUCKETER', '0', 'testUser'));
257258
});
258259

packages/optimizely-sdk/lib/core/project_config/index.tests.js

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import * as loggerPlugin from '../../plugins/logger';
3030
import testDatafile from '../../tests/test_data';
3131
import configValidator from '../../utils/config_validator';
3232

33+
var buildLogMessageFromArgs = args => sprintf(args[1], ...args.splice(2));
3334
var logger = getLogger();
3435

3536
describe('lib/core/project_config', function() {
@@ -301,9 +302,8 @@ describe('lib/core/project_config', function() {
301302

302303
it('should return null for invalid attribute key in getAttributeId', function() {
303304
assert.isNull(projectConfig.getAttributeId(configObj, 'invalidAttributeKey', createdLogger));
304-
sinon.assert.calledWithExactly(
305-
createdLogger.log,
306-
LOG_LEVEL.DEBUG,
305+
assert.strictEqual(
306+
buildLogMessageFromArgs(createdLogger.log.lastCall.args),
307307
'PROJECT_CONFIG: Unrecognized attribute invalidAttributeKey provided. Pruning before sending event to Optimizely.'
308308
);
309309
});
@@ -315,9 +315,8 @@ describe('lib/core/project_config', function() {
315315
key: '$opt_some_reserved_attribute',
316316
};
317317
assert.strictEqual(projectConfig.getAttributeId(configObj, '$opt_some_reserved_attribute', createdLogger), '42');
318-
sinon.assert.calledWithExactly(
319-
createdLogger.log,
320-
LOG_LEVEL.WARNING,
318+
assert.strictEqual(
319+
buildLogMessageFromArgs(createdLogger.log.lastCall.args),
321320
'Attribute $opt_some_reserved_attribute unexpectedly has reserved prefix $opt_; using attribute ID instead of reserved attribute name.'
322321
);
323322
});
@@ -461,9 +460,8 @@ describe('lib/core/project_config', function() {
461460
var result = projectConfig.getVariableForFeature(configObj, featureKey, variableKey, featureManagementLogger);
462461
assert.strictEqual(result, null);
463462
sinon.assert.calledOnce(featureManagementLogger.log);
464-
sinon.assert.calledWithExactly(
465-
featureManagementLogger.log,
466-
LOG_LEVEL.ERROR,
463+
assert.strictEqual(
464+
buildLogMessageFromArgs(featureManagementLogger.log.lastCall.args),
467465
'PROJECT_CONFIG: Variable with key "notARealVariable____" associated with feature with key "test_feature_for_experiment" is not in datafile.'
468466
);
469467
});
@@ -474,9 +472,8 @@ describe('lib/core/project_config', function() {
474472
var result = projectConfig.getVariableForFeature(configObj, featureKey, variableKey, featureManagementLogger);
475473
assert.strictEqual(result, null);
476474
sinon.assert.calledOnce(featureManagementLogger.log);
477-
sinon.assert.calledWithExactly(
478-
featureManagementLogger.log,
479-
LOG_LEVEL.ERROR,
475+
assert.strictEqual(
476+
buildLogMessageFromArgs(featureManagementLogger.log.lastCall.args),
480477
'PROJECT_CONFIG: Feature key notARealFeature_____ is not in datafile.'
481478
);
482479
});
@@ -487,9 +484,8 @@ describe('lib/core/project_config', function() {
487484
var result = projectConfig.getVariableForFeature(configObj, featureKey, variableKey, featureManagementLogger);
488485
assert.strictEqual(result, null);
489486
sinon.assert.calledOnce(featureManagementLogger.log);
490-
sinon.assert.calledWithExactly(
491-
featureManagementLogger.log,
492-
LOG_LEVEL.ERROR,
487+
assert.strictEqual(
488+
buildLogMessageFromArgs(featureManagementLogger.log.lastCall.args),
493489
'PROJECT_CONFIG: Feature key notARealFeature_____ is not in datafile.'
494490
);
495491
});
@@ -633,9 +629,8 @@ describe('lib/core/project_config', function() {
633629
featureManagementLogger
634630
);
635631
assert.strictEqual(result, null);
636-
sinon.assert.calledWithExactly(
637-
featureManagementLogger.log,
638-
LOG_LEVEL.ERROR,
632+
assert.strictEqual(
633+
buildLogMessageFromArgs(featureManagementLogger.log.lastCall.args),
639634
'PROJECT_CONFIG: Unable to cast value notabool to type boolean, returning null.'
640635
);
641636
});
@@ -647,9 +642,8 @@ describe('lib/core/project_config', function() {
647642
featureManagementLogger
648643
);
649644
assert.strictEqual(result, null);
650-
sinon.assert.calledWithExactly(
651-
featureManagementLogger.log,
652-
LOG_LEVEL.ERROR,
645+
assert.strictEqual(
646+
buildLogMessageFromArgs(featureManagementLogger.log.lastCall.args),
653647
'PROJECT_CONFIG: Unable to cast value notanint to type integer, returning null.'
654648
);
655649
});
@@ -661,9 +655,8 @@ describe('lib/core/project_config', function() {
661655
featureManagementLogger
662656
);
663657
assert.strictEqual(result, null);
664-
sinon.assert.calledWithExactly(
665-
featureManagementLogger.log,
666-
LOG_LEVEL.ERROR,
658+
assert.strictEqual(
659+
buildLogMessageFromArgs(featureManagementLogger.log.lastCall.args),
667660
'PROJECT_CONFIG: Unable to cast value notadouble to type double, returning null.'
668661
);
669662
});

0 commit comments

Comments
 (0)