info.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import React from "react"
  2. import { render } from "enzyme"
  3. import { fromJS } from "immutable"
  4. import Info, { InfoUrl } from "components/info"
  5. import { Link } from "components/layout-utils"
  6. import Markdown from "components/providers/markdown"
  7. describe("<Info/> Anchor Target Safety", function(){
  8. const dummyComponent = () => null
  9. const components = {
  10. Markdown,
  11. InfoUrl,
  12. Link
  13. }
  14. const baseProps = {
  15. getComponent: c => components[c] || dummyComponent,
  16. host: "example.test",
  17. basePath: "/api",
  18. info: fromJS({
  19. title: "Hello World"
  20. })
  21. }
  22. it("renders externalDocs links with safe `rel` attributes", function () {
  23. const props = {
  24. ...baseProps,
  25. externalDocs: fromJS({
  26. url: "http://google.com/"
  27. })
  28. }
  29. let wrapper = render(<Info {...props} />)
  30. const anchor = wrapper.find("a")
  31. expect(anchor.html()).toEqual("http://google.com/")
  32. expect(anchor.attr("target")).toEqual("_blank")
  33. expect(anchor.attr("rel") || "").toMatch("noopener")
  34. expect(anchor.attr("rel") || "").toMatch("noreferrer")
  35. })
  36. it("renders Contact links with safe `rel` attributes", function () {
  37. const props = {
  38. ...baseProps,
  39. info: fromJS({
  40. contact: {
  41. url: "http://google.com/",
  42. name: "My Site"
  43. }
  44. })
  45. }
  46. let wrapper = render(<Info {...props} />)
  47. const anchor = wrapper.find("a")
  48. expect(anchor.attr("href")).toEqual("http://google.com/")
  49. expect(anchor.attr("target")).toEqual("_blank")
  50. expect(anchor.attr("rel") || "").toMatch("noopener")
  51. expect(anchor.attr("rel") || "").toMatch("noreferrer")
  52. })
  53. it("renders License links with safe `rel` attributes", function () {
  54. const props = {
  55. ...baseProps,
  56. info: fromJS({
  57. license: {
  58. url: "http://mit.edu/"
  59. }
  60. })
  61. }
  62. let wrapper = render(<Info {...props} />)
  63. const anchor = wrapper.find("a")
  64. expect(anchor.attr("href")).toEqual("http://mit.edu/")
  65. expect(anchor.attr("target")).toEqual("_blank")
  66. expect(anchor.attr("rel") || "").toMatch("noopener")
  67. expect(anchor.attr("rel") || "").toMatch("noreferrer")
  68. })
  69. it("renders termsOfService links with safe `rel` attributes", function () {
  70. const props = {
  71. ...baseProps,
  72. info: fromJS({
  73. termsOfService: "http://smartbear.com/"
  74. })
  75. }
  76. let wrapper = render(<Info {...props} />)
  77. const anchor = wrapper.find("a")
  78. expect(anchor.attr("href")).toEqual("http://smartbear.com/")
  79. expect(anchor.attr("target")).toEqual("_blank")
  80. expect(anchor.attr("rel") || "").toMatch("noopener")
  81. expect(anchor.attr("rel") || "").toMatch("noreferrer")
  82. })
  83. it("renders definition URL links with safe `rel` attributes", function () {
  84. const props = {
  85. ...baseProps,
  86. url: "http://petstore.swagger.io/v2/petstore.json"
  87. }
  88. let wrapper = render(<Info {...props} />)
  89. const anchor = wrapper.find("a")
  90. expect(anchor.attr("href")).toEqual("http://petstore.swagger.io/v2/petstore.json")
  91. expect(anchor.attr("target")).toEqual("_blank")
  92. expect(anchor.attr("rel") || "").toMatch("noopener")
  93. expect(anchor.attr("rel") || "").toMatch("noreferrer")
  94. })
  95. })