Update AdvancedFilter constructor to throw Error if logical operator is not And and there is only single condition. Also fixes bug which performs validation on conditions after they are extracted from the array.

This commit is contained in:
Matt Mazzola 2016-08-18 13:30:58 -07:00
Родитель cf5045298e
Коммит 97761f0b33
2 изменённых файлов: 28 добавлений и 9 удалений

Просмотреть файл

@ -260,24 +260,31 @@ export class AdvancedFilter extends Filter {
this.logicalOperator = logicalOperator;
if(conditions.length === 0) {
throw new Error(`conditions must be a non-empty array. You passed: ${conditions}`);
}
if(conditions.length > 2) {
throw new Error(`AdvancedFilters may not have more than two conditions. You passed: ${conditions.length}`);
}
let extractedConditions: IAdvancedFilterCondition[];
/**
* Accept conditions as array instead of as individual arguments
* new AdvancedFilter('a', 'b', "And", { value: 1, operator: "Equals" }, { value: 2, operator: "IsGreaterThan" });
* new AdvancedFilter('a', 'b', "And", [{ value: 1, operator: "Equals" }, { value: 2, operator: "IsGreaterThan" }]);
*/
if(Array.isArray(conditions[0])) {
this.conditions = <IAdvancedFilterCondition[]>conditions[0];
extractedConditions = <IAdvancedFilterCondition[]>conditions[0];
}
else {
this.conditions = <IAdvancedFilterCondition[]>conditions;
extractedConditions = <IAdvancedFilterCondition[]>conditions;
}
if(extractedConditions.length === 0) {
throw new Error(`conditions must be a non-empty array. You passed: ${conditions}`);
}
if(extractedConditions.length > 2) {
throw new Error(`AdvancedFilters may not have more than two conditions. You passed: ${conditions.length}`);
}
if(extractedConditions.length === 1 && logicalOperator !== "And") {
throw new Error(`Logical Operator must be "And" when there is only one condition provided`);
}
this.conditions = extractedConditions;
}
toJSON(): IAdvancedFilter {

Просмотреть файл

@ -406,6 +406,18 @@ describe("Unit | Filters", function () {
expect(attemptToCreateFilter).toThrowError();
});
it("should throw an error if logical operator is not And when only 1 condition is provided", function () {
// Arrange
// Act
const attemptToCreateFilter = () => {
return new models.AdvancedFilter({ table: "Table", column: "c" }, "Or", { value: "a", operator: "Contains" });
};
// Assert
expect(attemptToCreateFilter).toThrowError();
});
it("should output the correct json when toJSON is called", function () {
// Arrange
const expectedFilter: models.IAdvancedFilter = {
@ -445,7 +457,7 @@ describe("Unit | Filters", function () {
table: "a",
column: "b"
},
logicalOperator: <any>"x",
logicalOperator: "Or",
conditions: [
{
value: "v1",