-
Notifications
You must be signed in to change notification settings - Fork 83
[FSSDK-11095] rewrite condition_tree_evaluator tests in Typescript #994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
/**************************************************************************** | ||
* Copyright 2018, 2020-2021, Optimizely, Inc. and contributors * | ||
* * | ||
* Licensed under the Apache License, Version 2.0 (the "License"); * | ||
* you may not use this file except in compliance with the License. * | ||
* You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, software * | ||
* distributed under the License is distributed on an "AS IS" BASIS, * | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
* See the License for the specific language governing permissions and * | ||
* limitations under the License. * | ||
***************************************************************************/ | ||
import { describe, it, vi, expect } from 'vitest'; | ||
|
||
import * as conditionTreeEvaluator from '.'; | ||
|
||
const conditionA = { | ||
name: 'browser_type', | ||
value: 'safari', | ||
type: 'custom_attribute', | ||
}; | ||
const conditionB = { | ||
name: 'device_model', | ||
value: 'iphone6', | ||
type: 'custom_attribute', | ||
}; | ||
const conditionC = { | ||
name: 'location', | ||
match: 'exact', | ||
type: 'custom_attribute', | ||
value: 'CA', | ||
}; | ||
|
||
describe('lib/core/condition_tree_evaluator', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's get rid of this and the next describe blocks, these are not necessary. |
||
describe('APIs', function() { | ||
describe('evaluate', function() { | ||
it('should return true for a leaf condition when the leaf condition evaluator returns true', function() { | ||
expect( | ||
conditionTreeEvaluator.evaluate(conditionA, function() { | ||
return true; | ||
}) | ||
).toBe(true); | ||
}); | ||
|
||
it('should return false for a leaf condition when the leaf condition evaluator returns false', function() { | ||
expect( | ||
conditionTreeEvaluator.evaluate(conditionA, function() { | ||
return false; | ||
}) | ||
).toBe(false) | ||
}); | ||
|
||
describe('and evaluation', function() { | ||
it('should return true when ALL conditions evaluate to true', function() { | ||
expect( | ||
conditionTreeEvaluator.evaluate(['and', conditionA, conditionB], function() { | ||
return true; | ||
}) | ||
).toBe(true); | ||
}); | ||
|
||
it('should return false if one condition evaluates to false', function() { | ||
const leafEvaluator = vi.fn(); | ||
leafEvaluator.mockImplementationOnce(() => true) | ||
.mockImplementationOnce(() => false); | ||
expect(conditionTreeEvaluator.evaluate(['and', conditionA, conditionB], leafEvaluator)).toBe(false); | ||
}); | ||
|
||
describe('null handling', function() { | ||
it('should return null when all operands evaluate to null', function() { | ||
expect( | ||
conditionTreeEvaluator.evaluate(['and', conditionA, conditionB], function() { | ||
return null; | ||
}) | ||
).toBeNull(); | ||
}); | ||
|
||
it('should return null when operands evaluate to trues and nulls', function() { | ||
const leafEvaluator = vi.fn(); | ||
leafEvaluator.mockImplementationOnce(() => true) | ||
.mockImplementationOnce(() => null); | ||
expect(conditionTreeEvaluator.evaluate(['and', conditionA, conditionB], leafEvaluator)).toBeNull(); | ||
}); | ||
|
||
it('should return false when operands evaluate to falses and nulls', function() { | ||
const leafEvaluator = vi.fn(); | ||
leafEvaluator.mockImplementationOnce(() => false) | ||
.mockImplementationOnce(() => null); | ||
expect(conditionTreeEvaluator.evaluate(['and', conditionA, conditionB], leafEvaluator)).toBe(false); | ||
|
||
leafEvaluator.mockReset(); | ||
leafEvaluator.mockImplementationOnce(() => null) | ||
.mockImplementationOnce(() => false); | ||
expect(conditionTreeEvaluator.evaluate(['and', conditionA, conditionB], leafEvaluator)).toBe(false); | ||
}); | ||
|
||
it('should return false when operands evaluate to trues, falses, and nulls', function() { | ||
const leafEvaluator = vi.fn(); | ||
leafEvaluator.mockImplementationOnce(() => true) | ||
.mockImplementationOnce(() => false) | ||
.mockImplementationOnce(() => null); | ||
expect(conditionTreeEvaluator.evaluate(['and', conditionA, conditionB, conditionC], leafEvaluator)).toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('or evaluation', function() { | ||
it('should return true if any condition evaluates to true', function() { | ||
const leafEvaluator = vi.fn(); | ||
leafEvaluator.mockImplementationOnce(() => false) | ||
.mockImplementationOnce(() => true); | ||
expect(conditionTreeEvaluator.evaluate(['or', conditionA, conditionB], leafEvaluator)).toBe(true); | ||
}); | ||
|
||
it('should return false if all conditions evaluate to false', function() { | ||
expect( | ||
conditionTreeEvaluator.evaluate(['or', conditionA, conditionB], function() { | ||
return false; | ||
}) | ||
).toBe(false); | ||
}); | ||
|
||
describe('null handling', function() { | ||
it('should return null when all operands evaluate to null', function() { | ||
expect( | ||
conditionTreeEvaluator.evaluate(['or', conditionA, conditionB], function() { | ||
return null; | ||
}) | ||
).toBeNull(); | ||
}); | ||
|
||
it('should return true when operands evaluate to trues and nulls', function() { | ||
const leafEvaluator = vi.fn(); | ||
leafEvaluator.mockImplementationOnce(() => true) | ||
.mockImplementationOnce(() => null); | ||
expect(conditionTreeEvaluator.evaluate(['or', conditionA, conditionB], leafEvaluator)).toBe(true); | ||
}); | ||
|
||
it('should return null when operands evaluate to falses and nulls', function() { | ||
const leafEvaluator = vi.fn(); | ||
leafEvaluator.mockImplementationOnce(() => null) | ||
.mockImplementationOnce(() => false); | ||
expect(conditionTreeEvaluator.evaluate(['or', conditionA, conditionB], leafEvaluator)).toBeNull(); | ||
|
||
leafEvaluator.mockReset(); | ||
leafEvaluator.mockImplementationOnce(() => false) | ||
.mockImplementationOnce(() => null); | ||
expect(conditionTreeEvaluator.evaluate(['or', conditionA, conditionB], leafEvaluator)).toBeNull(); | ||
}); | ||
|
||
it('should return true when operands evaluate to trues, falses, and nulls', function() { | ||
const leafEvaluator = vi.fn(); | ||
leafEvaluator.mockImplementationOnce(() => true) | ||
.mockImplementationOnce(() => null) | ||
.mockImplementationOnce(() => false); | ||
expect(conditionTreeEvaluator.evaluate(['or', conditionA, conditionB, conditionC], leafEvaluator)).toBe(true); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('not evaluation', function() { | ||
it('should return true if the condition evaluates to false', function() { | ||
expect( | ||
conditionTreeEvaluator.evaluate(['not', conditionA], function() { | ||
return false; | ||
}) | ||
).toBe(true); | ||
}); | ||
|
||
it('should return false if the condition evaluates to true', function() { | ||
expect( | ||
conditionTreeEvaluator.evaluate(['not', conditionB], function() { | ||
return true; | ||
}) | ||
).toBe(false); | ||
}); | ||
|
||
it('should return the result of negating the first condition, and ignore any additional conditions', function() { | ||
let result = conditionTreeEvaluator.evaluate(['not', '1', '2', '1'], function(id) { | ||
return String(id) === '1'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of converting id to string here, maybe we can add (id: string) in the function signature? |
||
}); | ||
expect(result).toBe(false); | ||
result = conditionTreeEvaluator.evaluate(['not', '1', '2', '1'], function(id) { | ||
return String(id) === '2'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar here |
||
}); | ||
expect(result).toBe(true); | ||
result = conditionTreeEvaluator.evaluate(['not', '1', '2', '3'], function(id) { | ||
return String(id) === '1' ? null : String(id) === '3'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here |
||
}); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
describe('null handling', function() { | ||
it('should return null when operand evaluates to null', function() { | ||
expect( | ||
conditionTreeEvaluator.evaluate(['not', conditionA], function() { | ||
return null; | ||
}) | ||
).toBeNull(); | ||
}); | ||
|
||
it('should return null when there are no operands', function() { | ||
expect( | ||
conditionTreeEvaluator.evaluate(['not'], function() { | ||
return null; | ||
}) | ||
).toBeNull(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('implicit operator', function() { | ||
it('should behave like an "or" operator when the first item in the array is not a recognized operator', function() { | ||
const leafEvaluator = vi.fn(); | ||
leafEvaluator.mockImplementationOnce(() => true) | ||
.mockImplementationOnce(() => false); | ||
expect(conditionTreeEvaluator.evaluate([conditionA, conditionB], leafEvaluator)).toBe(true); | ||
expect( | ||
conditionTreeEvaluator.evaluate([conditionA, conditionB], function() { | ||
return false; | ||
}) | ||
).toBe(false); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use this copyright
/**
*/