123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- import React from "react"
- import Immutable, { List } from "immutable"
- import { Select, Input, TextArea } from "components/layout-utils"
- import { mount, render } from "enzyme"
- import * as JsonSchemaComponents from "core/json-schema-components"
- import { JsonSchemaForm } from "core/json-schema-components"
- const components = {...JsonSchemaComponents, Select, Input, TextArea}
- const getComponentStub = (name) => {
- if(components[name]) return components[name]
- return null
- }
- describe("<JsonSchemaForm/>", function(){
- describe("strings", function() {
- it("should render the correct options for a string enum parameter", function(){
- let props = {
- getComponent: getComponentStub,
- value: "",
- onChange: () => {},
- keyName: "",
- fn: {},
- schema: Immutable.fromJS({
- type: "string",
- enum: ["one", "two"]
- })
- }
- let wrapper = render(<JsonSchemaForm {...props}/>)
- expect(wrapper.find("select").length).toEqual(1)
- expect(wrapper.find("select option").length).toEqual(3)
- expect(wrapper.find("select option").eq(0).text()).toEqual("--")
- expect(wrapper.find("select option").eq(1).text()).toEqual("one")
- expect(wrapper.find("select option").eq(2).text()).toEqual("two")
- })
- it("should render a string enum as disabled when JsonSchemaForm is disabled", function(){
- let props = {
- getComponent: getComponentStub,
- value: "",
- onChange: () => {},
- keyName: "",
- fn: {},
- schema: Immutable.fromJS({
- type: "string",
- enum: ["one", "two"]
- }),
- disabled: true
- }
- let wrapper = render(<JsonSchemaForm {...props}/>)
- expect(wrapper.find("select").attr("disabled")).toEqual("disabled")
- })
- it("should render the correct options for a required string enum parameter", function(){
- let props = {
- getComponent: getComponentStub,
- value: "",
- onChange: () => {},
- keyName: "",
- fn: {},
- required: true,
- schema: Immutable.fromJS({
- type: "string",
- enum: ["one", "two"]
- })
- }
- let wrapper = render(<JsonSchemaForm {...props}/>)
- expect(wrapper.find("select").length).toEqual(1)
- expect(wrapper.find("select option").length).toEqual(2)
- expect(wrapper.find("select option").eq(0).text()).toEqual("one")
- expect(wrapper.find("select option").eq(1).text()).toEqual("two")
- })
- })
- describe("booleans", function() {
- it("should render the correct options for a boolean parameter", function(){
- let props = {
- getComponent: getComponentStub,
- value: "",
- onChange: () => {},
- keyName: "",
- fn: {},
- schema: Immutable.fromJS({
- type: "boolean"
- })
- }
- let wrapper = render(<JsonSchemaForm {...props}/>)
- expect(wrapper.find("select").length).toEqual(1)
- expect(wrapper.find("select option").length).toEqual(3)
- expect(wrapper.find("select option").eq(0).text()).toEqual("--")
- expect(wrapper.find("select option").eq(1).text()).toEqual("true")
- expect(wrapper.find("select option").eq(2).text()).toEqual("false")
- })
- it("should render the correct options for an enum boolean parameter", function(){
- let props = {
- getComponent: getComponentStub,
- value: "",
- onChange: () => {},
- keyName: "",
- fn: {},
- schema: Immutable.fromJS({
- type: "boolean",
- enum: ["true"]
- })
- }
- let wrapper = render(<JsonSchemaForm {...props}/>)
- expect(wrapper.find("select").length).toEqual(1)
- expect(wrapper.find("select option").length).toEqual(2)
- expect(wrapper.find("select option").eq(0).text()).toEqual("--")
- expect(wrapper.find("select option").eq(1).text()).toEqual("true")
- expect(wrapper.find("select option:checked").first().text()).toEqual("--")
- })
- it("should render the correct options for a required boolean parameter", function(){
- let props = {
- getComponent: getComponentStub,
- value: "",
- onChange: () => {},
- keyName: "",
- fn: {},
- schema: Immutable.fromJS({
- type: "boolean",
- required: true
- })
- }
- let wrapper = render(<JsonSchemaForm {...props}/>)
- expect(wrapper.find("select").length).toEqual(1)
- expect(wrapper.find("select option").length).toEqual(3)
- expect(wrapper.find("select option").eq(0).text()).toEqual("--")
- expect(wrapper.find("select option").eq(1).text()).toEqual("true")
- expect(wrapper.find("select option").eq(2).text()).toEqual("false")
- expect(wrapper.find("select option:checked").first().text()).toEqual("--")
- })
- it("should render the correct options for a required enum boolean parameter", function(){
- let props = {
- getComponent: getComponentStub,
- value: "",
- onChange: () => {},
- keyName: "",
- fn: {},
- required: true,
- schema: Immutable.fromJS({
- type: "boolean",
- enum: ["true"]
- })
- }
- let wrapper = render(<JsonSchemaForm {...props}/>)
- expect(wrapper.find("select").length).toEqual(1)
- expect(wrapper.find("select option").length).toEqual(1)
- expect(wrapper.find("select option").eq(0).text()).toEqual("true")
- expect(wrapper.find("select option:checked").first().text()).toEqual("true")
- })
- })
- describe("objects", function() {
- it("should render the correct editor for an OAS3 object parameter", function(){
- let updateQueue = []
- let props = {
- getComponent: getComponentStub,
- value: `{\n "id": "abc123"\n}`,
- onChange: (value) => {
- updateQueue.push({ value })
- },
- keyName: "",
- fn: {},
- errors: List(),
- schema: Immutable.fromJS({
- type: "object",
- properties: {
- id: {
- type: "string",
- example: "abc123"
- }
- }
- })
- }
- let wrapper = mount(<JsonSchemaForm {...props}/>)
- updateQueue.forEach(newProps => wrapper.setProps(newProps))
- expect(wrapper.find("textarea").length).toEqual(1)
- expect(wrapper.find("textarea").text()).toEqual(`{\n "id": "abc123"\n}`)
- })
- })
- describe("unknown types", function() {
- it("should render unknown types as strings", function(){
- let props = {
- getComponent: getComponentStub,
- value: "yo",
- onChange: () => {},
- keyName: "",
- fn: {},
- schema: Immutable.fromJS({
- type: "NotARealType"
- })
- }
- let wrapper = render(<JsonSchemaForm {...props}/>)
- expect(wrapper.find("input").length).toEqual(1)
- // expect(wrapper.find("select input").length).toEqual(1)
- // expect(wrapper.find("select option").first().text()).toEqual("true")
- })
- it("should render unknown types as strings when a format is passed", function(){
- let props = {
- getComponent: getComponentStub,
- value: "yo",
- onChange: () => {},
- keyName: "",
- fn: {},
- schema: Immutable.fromJS({
- type: "NotARealType",
- format: "NotARealFormat"
- })
- }
- let wrapper = render(<JsonSchemaForm {...props}/>)
- expect(wrapper.find("input").length).toEqual(1)
- // expect(wrapper.find("select input").length).toEqual(1)
- // expect(wrapper.find("select option").first().text()).toEqual("true")
- })
- })
- })
|