Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

188 wiersze
5.3 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 a required enum boolean parameter", function(){
  75. let props = {
  76. getComponent: getComponentStub,
  77. value: "",
  78. onChange: () => {},
  79. keyName: "",
  80. fn: {},
  81. required: true,
  82. schema: {
  83. type: "boolean",
  84. enum: ["true"]
  85. }
  86. }
  87. let wrapper = render(<JsonSchemaForm {...props}/>)
  88. expect(wrapper.find("select").length).toEqual(1)
  89. expect(wrapper.find("select option").length).toEqual(1)
  90. expect(wrapper.find("select option").first().text()).toEqual("true")
  91. })
  92. })
  93. describe("objects", function() {
  94. it("should render the correct editor for an OAS3 object parameter", function(){
  95. let updateQueue = []
  96. let props = {
  97. getComponent: getComponentStub,
  98. value: "",
  99. onChange: (value) => {
  100. updateQueue.push({ value })
  101. },
  102. keyName: "",
  103. fn: {},
  104. errors: List(),
  105. schema: {
  106. type: "object",
  107. properties: {
  108. id: {
  109. type: "string",
  110. example: "abc123"
  111. }
  112. }
  113. }
  114. }
  115. let wrapper = mount(<JsonSchemaForm {...props}/>)
  116. updateQueue.forEach(newProps => wrapper.setProps(newProps))
  117. expect(wrapper.find("textarea").length).toEqual(1)
  118. expect(wrapper.find("textarea").text()).toEqual(`{\n "id": "abc123"\n}`)
  119. })
  120. })
  121. describe("unknown types", function() {
  122. it("should render unknown types as strings", function(){
  123. let props = {
  124. getComponent: getComponentStub,
  125. value: "yo",
  126. onChange: () => {},
  127. keyName: "",
  128. fn: {},
  129. schema: {
  130. type: "NotARealType"
  131. }
  132. }
  133. let wrapper = render(<JsonSchemaForm {...props}/>)
  134. expect(wrapper.find("input").length).toEqual(1)
  135. // expect(wrapper.find("select input").length).toEqual(1)
  136. // expect(wrapper.find("select option").first().text()).toEqual("true")
  137. })
  138. it("should render unknown types as strings when a format is passed", function(){
  139. let props = {
  140. getComponent: getComponentStub,
  141. value: "yo",
  142. onChange: () => {},
  143. keyName: "",
  144. fn: {},
  145. schema: {
  146. type: "NotARealType",
  147. format: "NotARealFormat"
  148. }
  149. }
  150. let wrapper = render(<JsonSchemaForm {...props}/>)
  151. expect(wrapper.find("input").length).toEqual(1)
  152. // expect(wrapper.find("select input").length).toEqual(1)
  153. // expect(wrapper.find("select option").first().text()).toEqual("true")
  154. })
  155. })
  156. })