markdown.jsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import React from "react"
  2. import { render } from "enzyme"
  3. import Markdown from "components/providers/markdown"
  4. import { Markdown as OAS3Markdown } from "corePlugins/oas3/wrap-components/markdown.jsx"
  5. describe("Markdown component", function () {
  6. describe("Swagger 2.0", function () {
  7. it("allows elements with class, style and data-* attribs", function () {
  8. const getConfigs = () => ({ useUnsafeMarkdown: true })
  9. const str = `<span class="method" style="border-width: 1px" data-attr="value">ONE</span>`
  10. const el = render(<Markdown source={str} getConfigs={getConfigs} />)
  11. expect(el.html()).toEqual(`<div class="markdown"><p><span data-attr="value" style="border-width: 1px" class="method">ONE</span></p>\n</div>`)
  12. })
  13. it("strips class, style and data-* attribs from elements", function () {
  14. const getConfigs = () => ({ useUnsafeMarkdown: false })
  15. const str = `<span class="method" style="border-width: 1px" data-attr="value">ONE</span>`
  16. const el = render(<Markdown source={str} getConfigs={getConfigs} />)
  17. expect(el.html()).toEqual(`<div class="markdown"><p><span>ONE</span></p>\n</div>`)
  18. })
  19. it("allows td elements with colspan attrib", function () {
  20. const str = `<table><tr><td>ABC</td></tr></table>`
  21. const el = render(<Markdown source={str} />)
  22. expect(el.html()).toEqual(`<div class="markdown"><table><tbody><tr><td>ABC</td></tr></tbody></table></div>`)
  23. })
  24. it("allows image elements", function () {
  25. const str = `![Image alt text](http://image.source "Image title")`
  26. const el = render(<Markdown source={str} />)
  27. expect(el.html()).toEqual(`<div class="markdown"><p><img title="Image title" alt="Image alt text" src="http://image.source"></p>\n</div>`)
  28. })
  29. it("allows image elements with https scheme", function () {
  30. const str = `![Image alt text](https://image.source "Image title")`
  31. const el = render(<Markdown source={str} />)
  32. expect(el.html()).toEqual(`<div class="markdown"><p><img title="Image title" alt="Image alt text" src="https://image.source"></p>\n</div>`)
  33. })
  34. it("allows image elements with data scheme", function () {
  35. const str = `<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==">`
  36. const el = render(<Markdown source={str} />)
  37. expect(el.html()).toEqual(`<div class="markdown"><p>` + str + `</p>\n</div>`)
  38. })
  39. it("allows heading elements", function () {
  40. const str = `
  41. # h1
  42. ## h2
  43. ### h3
  44. #### h4
  45. ##### h5
  46. ###### h6`
  47. const el = render(<Markdown source={str} />)
  48. expect(el.html()).toEqual(`<div class="markdown"><h1>h1</h1>\n<h2>h2</h2>\n<h3>h3</h3>\n<h4>h4</h4>\n<h5>h5</h5>\n<h6>h6</h6>\n</div>`)
  49. })
  50. it("allows links", function () {
  51. const str = `[Link](https://example.com/)`
  52. const el = render(<Markdown source={str} />)
  53. expect(el.html()).toEqual(`<div class="markdown"><p><a rel="noopener noreferrer" target="_blank" href="https://example.com/">Link</a></p>\n</div>`)
  54. })
  55. })
  56. describe("OAS 3", function () {
  57. it("allows elements with class, style and data-* attribs", function () {
  58. const getConfigs = () => ({ useUnsafeMarkdown: true })
  59. const str = `<span class="method" style="border-width: 1px" data-attr="value">ONE</span>`
  60. const el = render(<OAS3Markdown source={str} getConfigs={getConfigs} />)
  61. expect(el.html()).toEqual(`<div class="renderedMarkdown"><p><span data-attr="value" style="border-width: 1px" class="method">ONE</span></p></div>`)
  62. })
  63. it("strips class, style and data-* attribs from elements", function () {
  64. const getConfigs = () => ({ useUnsafeMarkdown: false })
  65. const str = `<span class="method" style="border-width: 1px" data-attr="value">ONE</span>`
  66. const el = render(<OAS3Markdown source={str} getConfigs={getConfigs} />)
  67. expect(el.html()).toEqual(`<div class="renderedMarkdown"><p><span>ONE</span></p></div>`)
  68. })
  69. it("allows image elements", function () {
  70. const str = `![Image alt text](http://image.source "Image title")`
  71. const el = render(<OAS3Markdown source={str} />)
  72. expect(el.html()).toEqual(`<div class="renderedMarkdown"><p><img title="Image title" alt="Image alt text" src="http://image.source"></p></div>`)
  73. })
  74. it("allows image elements with https scheme", function () {
  75. const str = `![Image alt text](https://image.source "Image title")`
  76. const el = render(<OAS3Markdown source={str} />)
  77. expect(el.html()).toEqual(`<div class="renderedMarkdown"><p><img title="Image title" alt="Image alt text" src="https://image.source"></p></div>`)
  78. })
  79. it("allows image elements with data scheme", function () {
  80. const str = `<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==">`
  81. const el = render(<OAS3Markdown source={str} />)
  82. expect(el.html()).toEqual(`<div class="renderedMarkdown"><p>` + str + `</p></div>`)
  83. })
  84. it("allows heading elements", function () {
  85. const str = `
  86. # h1
  87. ## h2
  88. ### h3
  89. #### h4
  90. ##### h5
  91. ###### h6`
  92. const el = render(<OAS3Markdown source={str} />)
  93. expect(el.html()).toEqual(`<div class="renderedMarkdown"><h1>h1</h1>\n<h2>h2</h2>\n<h3>h3</h3>\n<h4>h4</h4>\n<h5>h5</h5>\n<h6>h6</h6></div>`)
  94. })
  95. })
  96. })