From aa862abdbb4fd014b584c0cb267d46eec8473939 Mon Sep 17 00:00:00 2001 From: "kevinq.qk" Date: Tue, 12 Feb 2019 23:12:25 +0800 Subject: [PATCH] test: add enum test --- test/enum_test.js | 132 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 test/enum_test.js diff --git a/test/enum_test.js b/test/enum_test.js new file mode 100644 index 0000000000..a9f335f6f7 --- /dev/null +++ b/test/enum_test.js @@ -0,0 +1,132 @@ +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); + }); +});