Skip to content

Commit

Permalink
#11 some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mavarazy committed Sep 4, 2017
1 parent 40861ec commit b026cf0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/conditionsMeet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import checkField from "./checkField";
import { OR, AND, NOT } from "./constants";
import selectn from "selectn";

export default function conditionsMeet(conditions, formData) {
if (!isObject(conditions) || !isObject(formData)) {
export default function conditionsMeet(condition, formData) {
if (!isObject(condition) || !isObject(formData)) {
toError(
`Rule ${JSON.stringify(conditions)} with ${formData} can't be processed`
`Rule ${JSON.stringify(condition)} with ${formData} can't be processed`
);
return false;
}
return Object.keys(conditions).every(ref => {
let refCondition = conditions[ref];
return Object.keys(condition).every(ref => {
let refCondition = condition[ref];
if (ref === OR) {
return refCondition.some(rule => conditionsMeet(rule, formData));
} else if (ref === AND) {
Expand Down
8 changes: 8 additions & 0 deletions test/conditionsMeet.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import conditionsMeet from "../src/conditionsMeet";
import { testInProd } from "./utils";

test("sanity checkField", function() {
expect(() => conditionsMeet("empty", {})).toThrow();
Expand Down Expand Up @@ -53,3 +54,10 @@ test("NOT condition", () => {
conditionsMeet(condition, { firstName: "Will", lastName: "Smith" })
).toBeFalsy();
});

test("invalid condition", () => {
expect(() => conditionsMeet("empty", {})).toThrow();
expect(() => conditionsMeet({}, "empty")).toThrow();
expect(testInProd(() => conditionsMeet("empty", {}))).toBeFalsy();
expect(testInProd(() => conditionsMeet({}, "empty"))).toBeFalsy();
});
4 changes: 2 additions & 2 deletions test/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ test("invalid field NOT or", () => {
{
conditions: {
not: {
firstName: "or",
firstName: "bad",
},
},
event: { type: "remove" },
},
]);

expect(listInvalidPredicates(invalidFieldNotWithOr, defSchema)).toEqual([
"or",
"bad",
]);
expect(() => validatePredicates(invalidFieldNotWithOr, defSchema)).toThrow();
});
Expand Down

0 comments on commit b026cf0

Please sign in to comment.