diff --git a/test/NumberField_test.js b/test/NumberField_test.js index b265e8e642..456dcd2bd9 100644 --- a/test/NumberField_test.js +++ b/test/NumberField_test.js @@ -290,5 +290,82 @@ describe("NumberField", () => { expect(node.querySelector("select").id).eql("root"); }); + + it("should render a select element if set the enum.", () => { + const schema = { + type: "object", + properties: { + foo: { + type: "number", + enum: [0], + }, + }, + }; + + const { node } = createFormComponent({ + schema, + }); + + const selects = node.querySelectorAll("select"); + expect(selects).to.have.length.of(1); + }); + + it("should render a select element and it's value is empty, if set the enum and the default value is undefined.", () => { + const schema = { + type: "object", + properties: { + foo: { + type: "number", + enum: [0], + }, + }, + }; + + const { node } = createFormComponent({ + schema, + }); + + const selects = node.querySelectorAll("select"); + expect(selects[0].value).eql(""); + }); + + it("should render a select element and it's first option has an empty innerHTML, if set the enum and the default value is undefined.", () => { + const schema = { + type: "object", + properties: { + foo: { + type: "number", + enum: [0], + }, + }, + }; + + const { node } = createFormComponent({ + schema, + }); + + const options = node.querySelectorAll("option"); + expect(options[0].innerHTML).eql(""); + }); + + it("should render a select element and it's first option is '0', if set the enum and the default value is 0.", () => { + const schema = { + type: "object", + properties: { + foo: { + type: "number", + enum: [0], + default: 0, + }, + }, + }; + + const { node } = createFormComponent({ + schema, + }); + + const options = node.querySelectorAll("option"); + expect(options[0].innerHTML).eql("0"); + }); }); }); diff --git a/test/StringField_test.js b/test/StringField_test.js index 2846031cfd..4382247b39 100644 --- a/test/StringField_test.js +++ b/test/StringField_test.js @@ -350,6 +350,86 @@ describe("StringField", () => { expect(node.querySelector("#custom")).to.exist; }); + + it("should render a select element and it's first option is 'false', if set the enum and the default value is false.", () => { + const schema = { + type: "object", + properties: { + foo: { + type: "string", + enum: [false, true], + default: false, + }, + }, + }; + + const { node } = createFormComponent({ + schema, + }); + + const options = node.querySelectorAll("option"); + expect(options[0].innerHTML).eql("false"); + }); + + it("should render a select element and the option's length is equal the enum's length, if set the enum.", () => { + const schema = { + type: "object", + properties: { + foo: { + type: "string", + enum: [false, true], + default: false, + }, + }, + }; + + const { node } = createFormComponent({ + schema, + }); + + const options = node.querySelectorAll("option"); + expect(options.length).eql(2); + }); + + it("should render a select element and the option's length is equal the enum's length, if set the enum and the default value is empty.", () => { + const schema = { + type: "object", + properties: { + foo: { + type: "string", + enum: ["", "1"], + default: "", + }, + }, + }; + + const { node } = createFormComponent({ + schema, + }); + + const options = node.querySelectorAll("option"); + expect(options.length).eql(2); + }); + + it("shouldn't render two empty options, when the default value is empty.", () => { + const schema = { + type: "object", + properties: { + foo: { + type: "string", + enum: [""], + default: "", + }, + }, + }; + + const { node } = createFormComponent({ + schema, + }); + + const options = node.querySelectorAll("option"); + expect(options.length).eql(1); + }); }); describe("TextareaWidget", () => { diff --git a/test/enum_test.js b/test/enum_test.js deleted file mode 100644 index a9f335f6f7..0000000000 --- a/test/enum_test.js +++ /dev/null @@ -1,132 +0,0 @@ -import { expect } from "chai"; - -import { createFormComponent, createSandbox } from "./test_utils"; - -describe("enum", () => { - let sandbox; - - beforeEach(() => { - sandbox = createSandbox(); - }); - - afterEach(() => { - sandbox.restore(); - }); - - it("should render a select element if set the enum.", () => { - const schema = { - type: "object", - properties: { - foo: { - type: "string", - enum: [0], - }, - }, - }; - - const { node } = createFormComponent({ - schema, - }); - - const selects = node.querySelectorAll("select"); - expect(selects).to.have.length.of(1); - }); - - it("should render a select element and it's value is empty, if set the enum and the default value is undefined.", () => { - const schema = { - type: "object", - properties: { - foo: { - type: "string", - enum: [0], - }, - }, - }; - - const { node } = createFormComponent({ - schema, - }); - - const selects = node.querySelectorAll("select"); - expect(selects[0].value).eql(""); - }); - - it("should render a select element and it's first option has an empty innerHTML, if set the enum and the default value is undefined.", () => { - const schema = { - type: "object", - properties: { - foo: { - type: "string", - enum: [0], - }, - }, - }; - - const { node } = createFormComponent({ - schema, - }); - - const options = node.querySelectorAll("option"); - expect(options[0].innerHTML).eql(""); - }); - - it("should render a select element and it's first option is '0', if set the enum and the default value is 0.", () => { - const schema = { - type: "object", - properties: { - foo: { - type: "string", - enum: [0], - default: 0, - }, - }, - }; - - const { node } = createFormComponent({ - schema, - }); - - const options = node.querySelectorAll("option"); - expect(options[0].innerHTML).eql("0"); - }); - - it("should render a select element and it's first option is 'false', if set the enum and the default value is false.", () => { - const schema = { - type: "object", - properties: { - foo: { - type: "string", - enum: [false, true], - default: false, - }, - }, - }; - - const { node } = createFormComponent({ - schema, - }); - - const options = node.querySelectorAll("option"); - expect(options[0].innerHTML).eql("false"); - }); - - it("should render a select element and the option's length is equal the enum's length, if set the enum.", () => { - const schema = { - type: "object", - properties: { - foo: { - type: "string", - enum: [false, true], - default: false, - }, - }, - }; - - const { node } = createFormComponent({ - schema, - }); - - const options = node.querySelectorAll("option"); - expect(options.length).eql(2); - }); -});