|
| 1 | +/* eslint-env mocha */ |
| 2 | +import React from "react" |
| 3 | +import expect, { createSpy } from "expect" |
| 4 | +import { Select } from "components/layout-utils" |
| 5 | +import { render } from "enzyme" |
| 6 | +import * as JsonSchemaComponents from "core/json-schema-components" |
| 7 | +import { JsonSchemaForm } from "core/json-schema-components" |
| 8 | + |
| 9 | +const components = {...JsonSchemaComponents, Select} |
| 10 | + |
| 11 | +const getComponentStub = (name) => { |
| 12 | + return components[name] || (() => { |
| 13 | + console.error(`Couldn't find "${name}"`) |
| 14 | + return null |
| 15 | + }) |
| 16 | +} |
| 17 | + |
| 18 | +describe("<JsonSchemaForm/>", function(){ |
| 19 | + describe("strings", function() { |
| 20 | + it("should render the correct options for a string enum parameter", function(){ |
| 21 | + |
| 22 | + let props = { |
| 23 | + getComponent: getComponentStub, |
| 24 | + value: "", |
| 25 | + onChange: () => {}, |
| 26 | + keyName: "", |
| 27 | + fn: {}, |
| 28 | + schema: { |
| 29 | + type: "string", |
| 30 | + enum: ["one", "two"] |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + let wrapper = render(<JsonSchemaForm {...props}/>) |
| 35 | + |
| 36 | + expect(wrapper.find("select").length).toEqual(1) |
| 37 | + expect(wrapper.find("select option").length).toEqual(3) |
| 38 | + expect(wrapper.find("select option").eq(0).text()).toEqual("--") |
| 39 | + expect(wrapper.find("select option").eq(1).text()).toEqual("one") |
| 40 | + expect(wrapper.find("select option").eq(2).text()).toEqual("two") |
| 41 | + }) |
| 42 | + |
| 43 | + it("should render the correct options for a required string enum parameter", function(){ |
| 44 | + |
| 45 | + let props = { |
| 46 | + getComponent: getComponentStub, |
| 47 | + value: "", |
| 48 | + onChange: () => {}, |
| 49 | + keyName: "", |
| 50 | + fn: {}, |
| 51 | + required: true, |
| 52 | + schema: { |
| 53 | + type: "string", |
| 54 | + enum: ["one", "two"] |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + let wrapper = render(<JsonSchemaForm {...props}/>) |
| 59 | + |
| 60 | + expect(wrapper.find("select").length).toEqual(1) |
| 61 | + expect(wrapper.find("select option").length).toEqual(2) |
| 62 | + expect(wrapper.find("select option").eq(0).text()).toEqual("one") |
| 63 | + expect(wrapper.find("select option").eq(1).text()).toEqual("two") |
| 64 | + }) |
| 65 | + }) |
| 66 | + describe("booleans", function() { |
| 67 | + it("should render the correct options for a boolean parameter", function(){ |
| 68 | + |
| 69 | + let props = { |
| 70 | + getComponent: getComponentStub, |
| 71 | + value: "", |
| 72 | + onChange: () => {}, |
| 73 | + keyName: "", |
| 74 | + fn: {}, |
| 75 | + schema: { |
| 76 | + type: "boolean" |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + let wrapper = render(<JsonSchemaForm {...props}/>) |
| 81 | + |
| 82 | + expect(wrapper.find("select").length).toEqual(1) |
| 83 | + expect(wrapper.find("select option").length).toEqual(3) |
| 84 | + expect(wrapper.find("select option").eq(0).text()).toEqual("--") |
| 85 | + expect(wrapper.find("select option").eq(1).text()).toEqual("true") |
| 86 | + expect(wrapper.find("select option").eq(2).text()).toEqual("false") |
| 87 | + }) |
| 88 | + |
| 89 | + it("should render the correct options for a required enum boolean parameter", function(){ |
| 90 | + |
| 91 | + let props = { |
| 92 | + getComponent: getComponentStub, |
| 93 | + value: "", |
| 94 | + onChange: () => {}, |
| 95 | + keyName: "", |
| 96 | + fn: {}, |
| 97 | + required: true, |
| 98 | + schema: { |
| 99 | + type: "boolean", |
| 100 | + enum: ["true"] |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + let wrapper = render(<JsonSchemaForm {...props}/>) |
| 105 | + |
| 106 | + expect(wrapper.find("select").length).toEqual(1) |
| 107 | + expect(wrapper.find("select option").length).toEqual(1) |
| 108 | + expect(wrapper.find("select option").eq(1).text()).toEqual("true") |
| 109 | + }) |
| 110 | + }) |
| 111 | +}) |
0 commit comments