diff --git a/test/test_data-test.js b/test/test_data-test.js index 6779663..8f428a6 100644 --- a/test/test_data-test.js +++ b/test/test_data-test.js @@ -222,7 +222,7 @@ describe('TestData', function() { { "attribute": "name", "negate": false, - "operator": "in", + "op": "in", "values": [ "ben", "christian", @@ -231,7 +231,7 @@ describe('TestData', function() { { "attribute": "country", "negate": true, - "operator": "in", + "op": "in", "values": [ "fr", ], @@ -243,4 +243,33 @@ describe('TestData', function() { const clearedRulesFlag = flag.clearRules(); expect(clearedRulesFlag.build(0)).not.toHaveProperty('rules'); }); + + it('can add evaluate a rule', async function() { + const td = TestData(); + td.update(td.flag('test-flag') + .fallthroughVariation(false) + .ifMatch('name', 'ben', 'christian') + .andNotMatch('country', 'fr') + .thenReturn(true)); + + const store = InMemoryFeatureStore(); + const client = LDClient.init('sdk_key', { featureStore: store, updateProcessor: td, sendEvents: false }); + + + // User1 should pass because matching name and not matching country + const user1 = { 'key': 'user1', 'name': 'christian', 'country': 'us' }; + const eval1 = await client.variationDetail('test-flag', user1, 'default' ); + + expect(eval1.value).toEqual(true); + expect(eval1.variationIndex).toEqual(0); + expect(eval1.reason.kind).toEqual('RULE_MATCH'); + + // User2 should NOT pass because matching name but incorrectly matching country + const user2 = { 'key': 'user2', 'name': 'ben', 'country': 'fr' }; + const eval2 = await client.variationDetail('test-flag', user2, 'default' ); + + expect(eval2.value).toEqual(false); + expect(eval2.variationIndex).toEqual(1); + expect(eval2.reason.kind).toEqual('FALLTHROUGH'); + }); }); diff --git a/test_data.js b/test_data.js index 50d72fc..d39aa67 100644 --- a/test_data.js +++ b/test_data.js @@ -97,7 +97,7 @@ TestDataFlagBuilder.prototype.copy = function () { to._on = this._on; to._fallthroughVariation = this._fallthroughVariation; to._targets = !this._targets ? null : new Map(this._targets); - to._rules = !this._rules ? null : JSON.parse(JSON.stringify(this._rules)); + to._rules = !this._rules ? null : this._rules.map(r => r.copy(this)); return to; }; @@ -259,7 +259,7 @@ function TestDataRuleBuilder(flagBuilder) { TestDataRuleBuilder.prototype.andMatch = function (attribute, ...values) { this._clauses.push({ attribute: attribute, - operator: 'in', + op: 'in', values: values, negate: false, }); @@ -269,7 +269,7 @@ TestDataRuleBuilder.prototype.andMatch = function (attribute, ...values) { TestDataRuleBuilder.prototype.andNotMatch = function (attribute, ...values) { this._clauses.push({ attribute: attribute, - operator: 'in', + op: 'in', values: values, negate: true, }); @@ -295,4 +295,11 @@ TestDataRuleBuilder.prototype.build = function (id) { }; }; +TestDataRuleBuilder.prototype.copy = function (flagBuilder) { + const flagRuleBuilder = new TestDataRuleBuilder(flagBuilder); + flagRuleBuilder._clauses = JSON.parse(JSON.stringify(this._clauses)); + flagRuleBuilder._variation = JSON.parse(JSON.stringify(this._variation)); + return flagRuleBuilder; +}; + module.exports = TestData;