Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

237 řádky
6.8 KiB

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