link.jsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import React from "react"
  2. import { render } from "enzyme"
  3. import { Link } from "components/layout-utils"
  4. describe("<Link/> Anchor Target Safety", function () {
  5. const dummyComponent = () => null
  6. const components = {
  7. Link
  8. }
  9. const baseProps = {
  10. getComponent: c => components[c] || dummyComponent
  11. }
  12. it("renders regular links with `noreferrer` and `noopener`", function () {
  13. const props = {
  14. ...baseProps,
  15. href: "http://google.com/"
  16. }
  17. let wrapper = render(<Link {...props} />)
  18. const anchor = wrapper.find("a")
  19. expect(anchor.attr("href")).toEqual("http://google.com/")
  20. expect(anchor.attr("rel") || "").toMatch("noopener")
  21. expect(anchor.attr("rel") || "").toMatch("noreferrer")
  22. })
  23. it("enforces `noreferrer` and `noopener` on target=_blank links", function () {
  24. const props = {
  25. ...baseProps,
  26. href: "http://google.com/",
  27. target: "_blank"
  28. }
  29. let wrapper = render(<Link {...props} />)
  30. const anchor = wrapper.find("a")
  31. expect(anchor.attr("href")).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. })