|
| 1 | +/** |
| 2 | + * Copyright 2016, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +var audienceEvaluator = require('./'); |
| 17 | +var chai = require('chai'); |
| 18 | +var assert = chai.assert; |
| 19 | + |
| 20 | +var chromeUserAudience = { |
| 21 | + conditions: ['and', {'name': 'browser_type', 'value': 'chrome'}], |
| 22 | +}; |
| 23 | +var iphoneUserAudience = { |
| 24 | + conditions: ['and', {'name': 'device_model', 'value': 'iphone'}], |
| 25 | +}; |
| 26 | + |
| 27 | +describe('lib/core/audience_evaluator', function() { |
| 28 | + describe('APIs', function() { |
| 29 | + describe('evaluate', function() { |
| 30 | + it('should return true if there are no audiences', function() { |
| 31 | + assert.isTrue(audienceEvaluator.evaluate([], {})); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should return false if there are audiences but no attributes', function() { |
| 35 | + assert.isFalse(audienceEvaluator.evaluate([chromeUserAudience], {})); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should return true if any of the audience conditions are met', function() { |
| 39 | + var iphoneUsers = { |
| 40 | + 'device_model': 'iphone', |
| 41 | + }; |
| 42 | + |
| 43 | + var chromeUsers = { |
| 44 | + 'browser_type': 'chrome', |
| 45 | + }; |
| 46 | + |
| 47 | + var iphoneChromeUsers = { |
| 48 | + 'browser_type': 'chrome', |
| 49 | + 'device_model': 'iphone', |
| 50 | + }; |
| 51 | + |
| 52 | + assert.isTrue(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], iphoneUsers)); |
| 53 | + assert.isTrue(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], chromeUsers)); |
| 54 | + assert.isTrue(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], iphoneChromeUsers)); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return false if none of the audience conditions are met', function() { |
| 58 | + var nexusUsers = { |
| 59 | + 'device_model': 'nexus5', |
| 60 | + }; |
| 61 | + |
| 62 | + var safariUsers = { |
| 63 | + 'browser_type': 'safari', |
| 64 | + }; |
| 65 | + |
| 66 | + var nexusSafariUsers = { |
| 67 | + 'browser_type': 'safari', |
| 68 | + 'device_model': 'nexus5', |
| 69 | + }; |
| 70 | + |
| 71 | + assert.isFalse(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], nexusUsers)); |
| 72 | + assert.isFalse(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], safariUsers)); |
| 73 | + assert.isFalse(audienceEvaluator.evaluate([chromeUserAudience, iphoneUserAudience], nexusSafariUsers)); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments