Skip to content

Commit

Permalink
Add tests for isConstant and toConstant
Browse files Browse the repository at this point in the history
  • Loading branch information
fbessou committed Jun 12, 2017
1 parent cc6d17d commit 1f9ca07
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
deepEquals,
getDefaultFormState,
isFilesArray,
isConstant,
toConstant,
isMultiSelect,
mergeObjects,
pad,
Expand Down Expand Up @@ -269,6 +271,49 @@ describe("utils", () => {
});
});

describe("isConstant", () => {
it("should return false when neither enum nor const is defined", () => {
const schema = {};
expect(isConstant(schema)).to.be.false;
});

it("should return true when schema enum is an array of one item", () => {
const schema = { enum: ["foo"] };
expect(isConstant(schema)).to.be.true;
});

it("should return false when schema enum contains several items", () => {
const schema = { enum: ["foo", "bar", "baz"] };
expect(isConstant(schema)).to.be.false;
});

it("should return true when schema const is defined", () => {
const schema = { const: "foo" };
expect(isConstant(schema)).to.be.true;
});
});

describe("toConstant()", () => {
describe("schema contains an enum array", () => {
it("should return its first value when it contains a unique element", () => {
const schema = { enum: ["foo"] };
expect(toConstant(schema)).eql("foo");
});

it("should return schema const value when it exists", () => {
const schema = { const: "bar" };
expect(toConstant(schema)).eql("bar");
});

it("should throw when it contains more than one element", () => {
const schema = { enum: ["foo", "bar"] };
expect(() => {
toConstant(schema);
}).to.Throw(Error, "cannot be inferred");
});
});
});

describe("isMultiSelect()", () => {
describe("uniqueItems is true", () => {
describe("schema items enum is an array", () => {
Expand Down

0 comments on commit 1f9ca07

Please sign in to comment.