Skip to content

Commit 87a4d89

Browse files
committed
fix #6
1 parent 7be1d2e commit 87a4d89

6 files changed

+131
-2
lines changed

src/applicableActions.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { flatMap } from "./utils";
1+
import { flatMap, toArray } from "./utils";
22
import conditionsMeet from "./conditionsMeet";
33

44
export default function applicableActions(rules, formData) {
55
return flatMap(rules, ({ conditions, event }) => {
66
if (conditionsMeet(conditions, formData)) {
7-
return [event];
7+
return toArray(event);
88
} else {
99
return [];
1010
}

src/utils.js

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ export function isDevelopment() {
66
return process.env.NODE_ENV !== "production";
77
}
88

9+
export function toArray(event) {
10+
if (Array.isArray(event)) {
11+
return event;
12+
} else {
13+
return [event];
14+
}
15+
}
16+
917
export function toError(message) {
1018
if (isDevelopment()) {
1119
throw new ReferenceError(message);
@@ -34,6 +42,7 @@ function fetchSchema(ref, schema) {
3442
toError(
3543
"Only local references supported at this point use json-schema-deref"
3644
);
45+
return undefined;
3746
}
3847
}
3948

@@ -43,6 +52,7 @@ export function extractRefSchema(field, schema) {
4352
} else if (schema.properties[field] && schema.properties[field]["$ref"]) {
4453
return fetchSchema(schema.properties[field]["$ref"], schema);
4554
} else {
55+
toError(`${field} has no $ref field ref schema extraction is impossible`);
4656
return undefined;
4757
}
4858
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import applicableActions from "../src/applicableActions";
2+
3+
let rules = [
4+
{
5+
conditions: {
6+
address: "empty",
7+
},
8+
event: [{ type: "remove" }, { type: "add" }],
9+
},
10+
];
11+
12+
test("check nested fields work", function() {
13+
expect(applicableActions(rules, {})).toEqual([
14+
{ type: "remove" },
15+
{ type: "add" },
16+
]);
17+
expect(applicableActions(rules, { address: { line: "some" } })).toEqual([]);
18+
});

test/utils.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
toError,
66
extractRefSchema,
77
isRefArray,
8+
toArray,
89
} from "../src/utils";
910
import { testInProd } from "./utils";
1011

@@ -57,6 +58,9 @@ test("extract referenced schema", () => {
5758
primaryMedication: {
5859
$ref: "#/definitions/medication",
5960
},
61+
externalConfig: {
62+
$ref: "http://example.com/oneschema.json",
63+
},
6064
},
6165
};
6266

@@ -67,4 +71,19 @@ test("extract referenced schema", () => {
6771
expect(extractRefSchema("primaryMedication", schema)).toEqual(
6872
schema.definitions.medication
6973
);
74+
75+
expect(() => extractRefSchema("externalConfig", schema)).toThrow();
76+
expect(testInProd(() => extractRefSchema("externalConfig", schema))).toEqual(
77+
undefined
78+
);
79+
80+
expect(() => extractRefSchema("lastName", schema)).toThrow();
81+
expect(testInProd(() => extractRefSchema("lastName", schema))).toEqual(
82+
undefined
83+
);
84+
});
85+
86+
test("array transformation", () => {
87+
expect(toArray("Yes")).toEqual(["Yes"]);
88+
expect(toArray(["Yes", "No"])).toEqual(["Yes", "No"]);
7089
});

test/validation.ref.test.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { predicatesFromCondition } from "../src/validation";
2+
import { testInProd } from "./utils";
3+
4+
let schema = {
5+
definitions: {
6+
medication: {
7+
type: "object",
8+
properties: {
9+
type: { type: "string" },
10+
isLiquid: { type: "boolean" },
11+
},
12+
},
13+
},
14+
type: "object",
15+
required: ["medications", "firstName", "lastName"],
16+
properties: {
17+
firstName: {
18+
type: "string",
19+
},
20+
lastName: {
21+
type: "string",
22+
},
23+
medications: {
24+
type: "array",
25+
items: { $ref: "#/definitions/medication" },
26+
},
27+
primaryMedication: {
28+
$ref: "#/definitions/medication",
29+
},
30+
externalConfig: {
31+
$ref: "http://example.com/oneschema.json",
32+
},
33+
},
34+
};
35+
36+
test("condition with external ref", () => {
37+
expect(() =>
38+
predicatesFromCondition({ "externalConfig.name": "empty" }, schema)
39+
).toThrow();
40+
expect(
41+
testInProd(() =>
42+
predicatesFromCondition({ "externalConfig.name": "empty" }, schema)
43+
)
44+
).toEqual([]);
45+
});
46+
47+
test("condition with fake ref field", () => {
48+
expect(() =>
49+
predicatesFromCondition({ "fakeRef.name": "empty" }, schema)
50+
).toThrow();
51+
expect(
52+
testInProd(() =>
53+
predicatesFromCondition({ "fakeRef.name": "empty" }, schema)
54+
)
55+
).toEqual([]);
56+
});
57+
58+
test("condition with fake field", () => {
59+
expect(() => predicatesFromCondition({ fakeRef: "empty" }, schema)).toThrow();
60+
expect(
61+
testInProd(() => predicatesFromCondition({ fakeRef: "empty" }, schema))
62+
).toEqual([]);
63+
});

test/validation.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,19 @@ test("invalid field", () => {
122122
{
123123
conditions: {
124124
or: [{ lastName: "empty" }, { firstName: "empty" }],
125+
and: [{ otherName: "empty" }],
125126
},
126127
},
127128
]);
128129

129130
expect(listAllFields(invalidFieldConditions)).toEqual([
130131
"lastName",
131132
"firstName",
133+
"otherName",
132134
]);
133135
expect(listInvalidFields(invalidFieldConditions, defSchema)).toEqual([
134136
"lastName",
137+
"otherName",
135138
]);
136139
expect(() =>
137140
validateConditionFields(invalidFieldConditions, defSchema)
@@ -257,3 +260,19 @@ test("extract predicates from condition when with or & and", () => {
257260
)
258261
).toEqual(["is", "less"]);
259262
});
263+
264+
test("invalid or in rule", () => {
265+
expect(() => predicatesFromRule({ or: { is: 1 } })).toThrow();
266+
expect(() => predicatesFromRule({ and: { is: 1 } })).toThrow();
267+
268+
expect(testInProd(() => predicatesFromRule({ or: { is: 1 } }))).toEqual([]);
269+
expect(testInProd(() => predicatesFromRule({ and: { is: 1 } }))).toEqual([]);
270+
});
271+
272+
test("invalid or in condition", () => {
273+
expect(() => predicatesFromCondition({ or: {} })).toThrow();
274+
expect(() => predicatesFromCondition({ and: {} })).toThrow();
275+
276+
expect(testInProd(() => predicatesFromCondition({ or: {} }))).toEqual([]);
277+
expect(testInProd(() => predicatesFromCondition({ and: {} }))).toEqual([]);
278+
});

0 commit comments

Comments
 (0)