Skip to content

Commit e05f419

Browse files
committed
Add tests for isConstant and toConstant
1 parent 8fc594b commit e05f419

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

test/utils_test.js

+45
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
deepEquals,
77
getDefaultFormState,
88
isFilesArray,
9+
isConstant,
10+
toConstant,
911
isMultiSelect,
1012
mergeObjects,
1113
pad,
@@ -269,6 +271,49 @@ describe("utils", () => {
269271
});
270272
});
271273

274+
describe("isConstant", () => {
275+
it("should return false when neither enum nor const is defined", () => {
276+
const schema = {};
277+
expect(isConstant(schema)).to.be.false;
278+
});
279+
280+
it("should return true when schema enum is an array of one item", () => {
281+
const schema = { enum: ["foo"] };
282+
expect(isConstant(schema)).to.be.true;
283+
});
284+
285+
it("should return false when schema enum contains several items", () => {
286+
const schema = { enum: ["foo", "bar", "baz"] };
287+
expect(isConstant(schema)).to.be.false;
288+
});
289+
290+
it("should return true when schema const is defined", () => {
291+
const schema = { const: "foo" };
292+
expect(isConstant(schema)).to.be.true;
293+
});
294+
});
295+
296+
describe("toConstant()", () => {
297+
describe("schema contains an enum array", () => {
298+
it("should return its first value when it contains a unique element", () => {
299+
const schema = { enum: ["foo"] };
300+
expect(toConstant(schema)).eql("foo");
301+
});
302+
303+
it("should return schema const value when it exists", () => {
304+
const schema = { const: "bar" };
305+
expect(toConstant(schema)).eql("bar");
306+
});
307+
308+
it("should throw when it contains more than one element", () => {
309+
const schema = { enum: ["foo", "bar"] };
310+
expect(() => {
311+
toConstant(schema);
312+
}).to.Throw(Error, "cannot be inferred");
313+
});
314+
});
315+
});
316+
272317
describe("isMultiSelect()", () => {
273318
describe("uniqueItems is true", () => {
274319
describe("schema items enum is an array", () => {

0 commit comments

Comments
 (0)