json-schema-form.jsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import React from "react"
  2. import Immutable, { List } from "immutable"
  3. import { Select, Input, TextArea } from "components/layout-utils"
  4. import { mount, render } from "enzyme"
  5. import * as JsonSchemaComponents from "core/json-schema-components"
  6. import { JsonSchemaForm } from "core/json-schema-components"
  7. const components = {...JsonSchemaComponents, Select, Input, TextArea}
  8. const getComponentStub = (name) => {
  9. if(components[name]) return components[name]
  10. return null
  11. }
  12. describe("<JsonSchemaForm/>", function(){
  13. describe("strings", function() {
  14. it("should render the correct options for a string enum parameter", function(){
  15. let props = {
  16. getComponent: getComponentStub,
  17. value: "",
  18. onChange: () => {},
  19. keyName: "",
  20. fn: {},
  21. schema: Immutable.fromJS({
  22. type: "string",
  23. enum: ["one", "two"]
  24. })
  25. }
  26. let wrapper = render(<JsonSchemaForm {...props}/>)
  27. expect(wrapper.find("select").length).toEqual(1)
  28. expect(wrapper.find("select option").length).toEqual(3)
  29. expect(wrapper.find("select option").eq(0).text()).toEqual("--")
  30. expect(wrapper.find("select option").eq(1).text()).toEqual("one")
  31. expect(wrapper.find("select option").eq(2).text()).toEqual("two")
  32. })
  33. it("should render a string enum as disabled when JsonSchemaForm is disabled", function(){
  34. let props = {
  35. getComponent: getComponentStub,
  36. value: "",
  37. onChange: () => {},
  38. keyName: "",
  39. fn: {},
  40. schema: Immutable.fromJS({
  41. type: "string",
  42. enum: ["one", "two"]
  43. }),
  44. disabled: true
  45. }
  46. let wrapper = render(<JsonSchemaForm {...props}/>)
  47. expect(wrapper.find("select").attr("disabled")).toEqual("disabled")
  48. })
  49. it("should render the correct options for a required string enum parameter", function(){
  50. let props = {
  51. getComponent: getComponentStub,
  52. value: "",
  53. onChange: () => {},
  54. keyName: "",
  55. fn: {},
  56. required: true,
  57. schema: Immutable.fromJS({
  58. type: "string",
  59. enum: ["one", "two"]
  60. })
  61. }
  62. let wrapper = render(<JsonSchemaForm {...props}/>)
  63. expect(wrapper.find("select").length).toEqual(1)
  64. expect(wrapper.find("select option").length).toEqual(2)
  65. expect(wrapper.find("select option").eq(0).text()).toEqual("one")
  66. expect(wrapper.find("select option").eq(1).text()).toEqual("two")
  67. })
  68. })
  69. describe("booleans", function() {
  70. it("should render the correct options for a boolean parameter", function(){
  71. let props = {
  72. getComponent: getComponentStub,
  73. value: "",
  74. onChange: () => {},
  75. keyName: "",
  76. fn: {},
  77. schema: Immutable.fromJS({
  78. type: "boolean"
  79. })
  80. }
  81. let wrapper = render(<JsonSchemaForm {...props}/>)
  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. it("should render the correct options for an enum boolean parameter", function(){
  89. let props = {
  90. getComponent: getComponentStub,
  91. value: "",
  92. onChange: () => {},
  93. keyName: "",
  94. fn: {},
  95. schema: Immutable.fromJS({
  96. type: "boolean",
  97. enum: ["true"]
  98. })
  99. }
  100. let wrapper = render(<JsonSchemaForm {...props}/>)
  101. expect(wrapper.find("select").length).toEqual(1)
  102. expect(wrapper.find("select option").length).toEqual(2)
  103. expect(wrapper.find("select option").eq(0).text()).toEqual("--")
  104. expect(wrapper.find("select option").eq(1).text()).toEqual("true")
  105. expect(wrapper.find("select option:checked").first().text()).toEqual("--")
  106. })
  107. it("should render the correct options for a required boolean parameter", function(){
  108. let props = {
  109. getComponent: getComponentStub,
  110. value: "",
  111. onChange: () => {},
  112. keyName: "",
  113. fn: {},
  114. schema: Immutable.fromJS({
  115. type: "boolean",
  116. required: true
  117. })
  118. }
  119. let wrapper = render(<JsonSchemaForm {...props}/>)
  120. expect(wrapper.find("select").length).toEqual(1)
  121. expect(wrapper.find("select option").length).toEqual(3)
  122. expect(wrapper.find("select option").eq(0).text()).toEqual("--")
  123. expect(wrapper.find("select option").eq(1).text()).toEqual("true")
  124. expect(wrapper.find("select option").eq(2).text()).toEqual("false")
  125. expect(wrapper.find("select option:checked").first().text()).toEqual("--")
  126. })
  127. it("should render the correct options for a required enum boolean parameter", function(){
  128. let props = {
  129. getComponent: getComponentStub,
  130. value: "",
  131. onChange: () => {},
  132. keyName: "",
  133. fn: {},
  134. required: true,
  135. schema: Immutable.fromJS({
  136. type: "boolean",
  137. enum: ["true"]
  138. })
  139. }
  140. let wrapper = render(<JsonSchemaForm {...props}/>)
  141. expect(wrapper.find("select").length).toEqual(1)
  142. expect(wrapper.find("select option").length).toEqual(1)
  143. expect(wrapper.find("select option").eq(0).text()).toEqual("true")
  144. expect(wrapper.find("select option:checked").first().text()).toEqual("true")
  145. })
  146. })
  147. describe("objects", function() {
  148. it("should render the correct editor for an OAS3 object parameter", function(){
  149. let updateQueue = []
  150. let props = {
  151. getComponent: getComponentStub,
  152. value: `{\n "id": "abc123"\n}`,
  153. onChange: (value) => {
  154. updateQueue.push({ value })
  155. },
  156. keyName: "",
  157. fn: {},
  158. errors: List(),
  159. schema: Immutable.fromJS({
  160. type: "object",
  161. properties: {
  162. id: {
  163. type: "string",
  164. example: "abc123"
  165. }
  166. }
  167. })
  168. }
  169. let wrapper = mount(<JsonSchemaForm {...props}/>)
  170. updateQueue.forEach(newProps => wrapper.setProps(newProps))
  171. expect(wrapper.find("textarea").length).toEqual(1)
  172. expect(wrapper.find("textarea").text()).toEqual(`{\n "id": "abc123"\n}`)
  173. })
  174. })
  175. describe("unknown types", function() {
  176. it("should render unknown types as strings", function(){
  177. let props = {
  178. getComponent: getComponentStub,
  179. value: "yo",
  180. onChange: () => {},
  181. keyName: "",
  182. fn: {},
  183. schema: Immutable.fromJS({
  184. type: "NotARealType"
  185. })
  186. }
  187. let wrapper = render(<JsonSchemaForm {...props}/>)
  188. expect(wrapper.find("input").length).toEqual(1)
  189. // expect(wrapper.find("select input").length).toEqual(1)
  190. // expect(wrapper.find("select option").first().text()).toEqual("true")
  191. })
  192. it("should render unknown types as strings when a format is passed", function(){
  193. let props = {
  194. getComponent: getComponentStub,
  195. value: "yo",
  196. onChange: () => {},
  197. keyName: "",
  198. fn: {},
  199. schema: Immutable.fromJS({
  200. type: "NotARealType",
  201. format: "NotARealFormat"
  202. })
  203. }
  204. let wrapper = render(<JsonSchemaForm {...props}/>)
  205. expect(wrapper.find("input").length).toEqual(1)
  206. // expect(wrapper.find("select input").length).toEqual(1)
  207. // expect(wrapper.find("select option").first().text()).toEqual("true")
  208. })
  209. })
  210. })