Skip to content

Commit da3c276

Browse files
author
Matt Carroll
committed
audience_evaluator: Continue with evaluation even when no user
attributes are passed in
1 parent e41f5c9 commit da3c276

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

packages/optimizely-sdk/lib/core/audience_evaluator/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ module.exports = {
2020
* Determine if the given user attributes satisfy the given audience conditions
2121
* @param {Object[]} audiences Audiences to match the user attributes against
2222
* @param {Object[]} audiences.conditions Audience conditions to match the user attributes against
23-
* @param {Object} userAttributes Hash representing user attributes which will be used in determining if
24-
* the audience conditions are met
23+
* @param {Object} [userAttributes] Hash representing user attributes which will be used in
24+
* determining if the audience conditions are met. If not
25+
* provided, defaults to an empty object.
2526
* @return {Boolean} True if the user attributes match the given audience conditions
2627
*/
2728
evaluate: function(audiences, userAttributes) {
@@ -30,9 +31,8 @@ module.exports = {
3031
return true;
3132
}
3233

33-
// if no user attributes specified, return false
3434
if (!userAttributes) {
35-
return false;
35+
userAttributes = {};
3636
}
3737

3838
for (var i = 0; i < audiences.length; i++) {

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ var chai = require('chai');
1818
var assert = chai.assert;
1919

2020
var chromeUserAudience = {
21-
conditions: ['and', {'name': 'browser_type', 'value': 'chrome'}],
21+
conditions: ['and', {
22+
name: 'browser_type',
23+
value: 'chrome',
24+
type: 'custom_attribute',
25+
}],
2226
};
2327
var iphoneUserAudience = {
24-
conditions: ['and', {'name': 'device_model', 'value': 'iphone'}],
28+
conditions: ['and', {
29+
name: 'device_model',
30+
value: 'iphone',
31+
type: 'custom_attribute',
32+
}],
2533
};
2634

2735
describe('lib/core/audience_evaluator', function() {
@@ -72,6 +80,18 @@ describe('lib/core/audience_evaluator', function() {
7280
assert.isFalse(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], safariUsers));
7381
assert.isFalse(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], nexusSafariUsers));
7482
});
83+
84+
it('should return true if no attributes are passed and the audience conditions evaluate to true in the absence of attributes', function() {
85+
var conditionsPassingWithNoAttrs = ['not', {
86+
match: 'exists',
87+
name: 'input_value',
88+
type: 'custom_attribute',
89+
}];
90+
var audience = {
91+
conditions: conditionsPassingWithNoAttrs,
92+
};
93+
assert.isTrue(audienceEvaluator.evaluate([audience]));
94+
});
7595
});
7696
});
7797
});

0 commit comments

Comments
 (0)