Skip to content

Commit 59de6b6

Browse files
committed
Finish decide api with empty default decide options unit tests
1 parent 6c03853 commit 59de6b6

File tree

1 file changed

+175
-1
lines changed

1 file changed

+175
-1
lines changed

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

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4490,7 +4490,7 @@ describe('lib/optimizely', function() {
44904490
optimizely: optlyInstance,
44914491
userId,
44924492
});
4493-
var decision = user.decide(flagKey);
4493+
var decision = optlyInstance.decide(user, flagKey);
44944494
var expectedDecision = {
44954495
variationKey: 'variation_with_traffic',
44964496
enabled: true,
@@ -4557,6 +4557,180 @@ describe('lib/optimizely', function() {
45574557
var callArgs = eventDispatcher.dispatchEvent.getCalls()[0].args;
45584558
assert.deepEqual(callArgs[0], expectedImpressionEvent);
45594559
});
4560+
4561+
it('should make a decision for rollout and dispatch an event when sendFlagDecisions is set to true', function() {
4562+
var flagKey = 'feature_1';
4563+
var variablesExpected = optlyInstance.getAllFeatureVariables(flagKey, userId);
4564+
var user = new OptimizelyUserContext({
4565+
optimizely: optlyInstance,
4566+
userId,
4567+
});
4568+
var decision = optlyInstance.decide(user, flagKey);
4569+
var expectedDecision = {
4570+
variationKey: '18257766532',
4571+
enabled: true,
4572+
variables: variablesExpected,
4573+
ruleKey: '18322080788',
4574+
flagKey: flagKey,
4575+
userContext: user,
4576+
reasons: [],
4577+
}
4578+
assert.deepEqual(decision, expectedDecision);
4579+
sinon.assert.calledOnce(optlyInstance.eventDispatcher.dispatchEvent);
4580+
var expectedImpressionEvent = {
4581+
httpVerb: 'POST',
4582+
url: 'https://logx.optimizely.com/v1/events',
4583+
params: {
4584+
account_id: '10367498574',
4585+
project_id: '10431130345',
4586+
visitors: [
4587+
{
4588+
snapshots: [
4589+
{
4590+
decisions: [
4591+
{
4592+
campaign_id: '18263344648',
4593+
experiment_id: '18322080788',
4594+
variation_id: '18257766532',
4595+
metadata: {
4596+
flag_key: 'feature_1',
4597+
rule_key: '18322080788',
4598+
rule_type: 'rollout',
4599+
variation_key: '18257766532',
4600+
enabled: true,
4601+
},
4602+
},
4603+
],
4604+
events: [
4605+
{
4606+
entity_id: '18263344648',
4607+
timestamp: Math.round(new Date().getTime()),
4608+
key: 'campaign_activated',
4609+
uuid: 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
4610+
},
4611+
],
4612+
},
4613+
],
4614+
visitor_id: 'tester',
4615+
attributes: [
4616+
{
4617+
entity_id: '$opt_bot_filtering',
4618+
key: '$opt_bot_filtering',
4619+
type: 'custom',
4620+
value: true,
4621+
},
4622+
],
4623+
},
4624+
],
4625+
revision: '241',
4626+
client_name: 'node-sdk',
4627+
client_version: enums.NODE_CLIENT_VERSION,
4628+
anonymize_ip: true,
4629+
enrich_decisions: true,
4630+
},
4631+
};
4632+
var callArgs = eventDispatcher.dispatchEvent.getCalls()[0].args;
4633+
assert.deepEqual(callArgs[0], expectedImpressionEvent);
4634+
});
4635+
4636+
it('should make a decision for rollout and do not dispatch an event when sendFlagDecisions is set to false', function() {
4637+
var newConfig = optlyInstance.projectConfigManager.getConfig();
4638+
newConfig.sendFlagDecisions = false;
4639+
optlyInstance.projectConfigManager.getConfig.returns(newConfig);
4640+
var flagKey = 'feature_1';
4641+
var variablesExpected = optlyInstance.getAllFeatureVariables(flagKey, userId);
4642+
var user = new OptimizelyUserContext({
4643+
optimizely: optlyInstance,
4644+
userId,
4645+
});
4646+
var decision = optlyInstance.decide(user, flagKey);
4647+
var expectedDecision = {
4648+
variationKey: '18257766532',
4649+
enabled: true,
4650+
variables: variablesExpected,
4651+
ruleKey: '18322080788',
4652+
flagKey: flagKey,
4653+
userContext: user,
4654+
reasons: [],
4655+
}
4656+
assert.deepEqual(decision, expectedDecision);
4657+
sinon.assert.notCalled(optlyInstance.eventDispatcher.dispatchEvent);
4658+
});
4659+
4660+
it('should make a decision when variation is null and dispatch an event', function() {
4661+
var flagKey = 'feature_3';
4662+
var variablesExpected = optlyInstance.getAllFeatureVariables(flagKey, userId);
4663+
var user = new OptimizelyUserContext({
4664+
optimizely: optlyInstance,
4665+
userId,
4666+
});
4667+
var decision = optlyInstance.decide(user, flagKey);
4668+
var expectedDecision = {
4669+
variationKey: '',
4670+
enabled: false,
4671+
variables: variablesExpected,
4672+
ruleKey: '',
4673+
flagKey: flagKey,
4674+
userContext: user,
4675+
reasons: [],
4676+
}
4677+
assert.deepEqual(decision, expectedDecision);
4678+
sinon.assert.calledOnce(optlyInstance.eventDispatcher.dispatchEvent);
4679+
var expectedImpressionEvent = {
4680+
httpVerb: 'POST',
4681+
url: 'https://logx.optimizely.com/v1/events',
4682+
params: {
4683+
account_id: '10367498574',
4684+
project_id: '10431130345',
4685+
visitors: [
4686+
{
4687+
snapshots: [
4688+
{
4689+
decisions: [
4690+
{
4691+
campaign_id: null,
4692+
experiment_id: null,
4693+
variation_id: null,
4694+
metadata: {
4695+
flag_key: 'feature_3',
4696+
rule_key: '',
4697+
rule_type: 'rollout',
4698+
variation_key: '',
4699+
enabled: false,
4700+
},
4701+
},
4702+
],
4703+
events: [
4704+
{
4705+
entity_id: null,
4706+
timestamp: Math.round(new Date().getTime()),
4707+
key: 'campaign_activated',
4708+
uuid: 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
4709+
},
4710+
],
4711+
},
4712+
],
4713+
visitor_id: 'tester',
4714+
attributes: [
4715+
{
4716+
entity_id: '$opt_bot_filtering',
4717+
key: '$opt_bot_filtering',
4718+
type: 'custom',
4719+
value: true,
4720+
},
4721+
],
4722+
},
4723+
],
4724+
revision: '241',
4725+
client_name: 'node-sdk',
4726+
client_version: enums.NODE_CLIENT_VERSION,
4727+
anonymize_ip: true,
4728+
enrich_decisions: true,
4729+
},
4730+
};
4731+
var callArgs = eventDispatcher.dispatchEvent.getCalls()[0].args;
4732+
assert.deepEqual(callArgs[0], expectedImpressionEvent);
4733+
});
45604734
});
45614735
});
45624736
});

0 commit comments

Comments
 (0)