Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

155 linhas
4.4 KiB

  1. /* eslint-env mocha */
  2. import React from "react"
  3. import expect, { createSpy } from "expect"
  4. import { Select, Input } 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. const components = {...JsonSchemaComponents, Select, Input}
  9. const getComponentStub = (name) => {
  10. if(components[name]) return components[name]
  11. return null
  12. }
  13. describe("<JsonSchemaForm/>", function(){
  14. describe("strings", function() {
  15. it("should render the correct options for a string enum parameter", function(){
  16. let props = {
  17. getComponent: getComponentStub,
  18. value: "",
  19. onChange: () => {},
  20. keyName: "",
  21. fn: {},
  22. schema: {
  23. type: "string",
  24. enum: ["one", "two"]
  25. }
  26. }
  27. let wrapper = render(<JsonSchemaForm {...props}/>)
  28. expect(wrapper.find("select").length).toEqual(1)
  29. expect(wrapper.find("select option").length).toEqual(3)
  30. expect(wrapper.find("select option").eq(0).text()).toEqual("--")
  31. expect(wrapper.find("select option").eq(1).text()).toEqual("one")
  32. expect(wrapper.find("select option").eq(2).text()).toEqual("two")
  33. })
  34. it("should render the correct options for a required string enum parameter", function(){
  35. let props = {
  36. getComponent: getComponentStub,
  37. value: "",
  38. onChange: () => {},
  39. keyName: "",
  40. fn: {},
  41. required: true,
  42. schema: {
  43. type: "string",
  44. enum: ["one", "two"]
  45. }
  46. }
  47. let wrapper = render(<JsonSchemaForm {...props}/>)
  48. expect(wrapper.find("select").length).toEqual(1)
  49. expect(wrapper.find("select option").length).toEqual(2)
  50. expect(wrapper.find("select option").eq(0).text()).toEqual("one")
  51. expect(wrapper.find("select option").eq(1).text()).toEqual("two")
  52. })
  53. })
  54. describe("booleans", function() {
  55. it("should render the correct options for a boolean parameter", function(){
  56. let props = {
  57. getComponent: getComponentStub,
  58. value: "",
  59. onChange: () => {},
  60. keyName: "",
  61. fn: {},
  62. schema: {
  63. type: "boolean"
  64. }
  65. }
  66. let wrapper = render(<JsonSchemaForm {...props}/>)
  67. expect(wrapper.find("select").length).toEqual(1)
  68. expect(wrapper.find("select option").length).toEqual(3)
  69. expect(wrapper.find("select option").eq(0).text()).toEqual("--")
  70. expect(wrapper.find("select option").eq(1).text()).toEqual("true")
  71. expect(wrapper.find("select option").eq(2).text()).toEqual("false")
  72. })
  73. it("should render the correct options for a required enum boolean parameter", function(){
  74. let props = {
  75. getComponent: getComponentStub,
  76. value: "",
  77. onChange: () => {},
  78. keyName: "",
  79. fn: {},
  80. required: true,
  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(1)
  89. expect(wrapper.find("select option").first().text()).toEqual("true")
  90. })
  91. })
  92. describe("unknown types", function() {
  93. it("should render unknown types as strings", function(){
  94. let props = {
  95. getComponent: getComponentStub,
  96. value: "yo",
  97. onChange: () => {},
  98. keyName: "",
  99. fn: {},
  100. schema: {
  101. type: "NotARealType"
  102. }
  103. }
  104. let wrapper = render(<JsonSchemaForm {...props}/>)
  105. expect(wrapper.find("input").length).toEqual(1)
  106. // expect(wrapper.find("select input").length).toEqual(1)
  107. // expect(wrapper.find("select option").first().text()).toEqual("true")
  108. })
  109. it("should render unknown types as strings when a format is passed", function(){
  110. let props = {
  111. getComponent: getComponentStub,
  112. value: "yo",
  113. onChange: () => {},
  114. keyName: "",
  115. fn: {},
  116. schema: {
  117. type: "NotARealType",
  118. format: "NotARealFormat"
  119. }
  120. }
  121. let wrapper = render(<JsonSchemaForm {...props}/>)
  122. expect(wrapper.find("input").length).toEqual(1)
  123. // expect(wrapper.find("select input").length).toEqual(1)
  124. // expect(wrapper.find("select option").first().text()).toEqual("true")
  125. })
  126. })
  127. })