RichText.test.jsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {convertLinks, RichText} from "content-src/asrouter/components/RichText/RichText";
  2. import {Localized} from "fluent-react";
  3. import {mount} from "enzyme";
  4. import React from "react";
  5. describe("convertLinks", () => {
  6. let sandbox;
  7. beforeEach(() => {
  8. sandbox = sinon.createSandbox();
  9. });
  10. afterEach(() => {
  11. sandbox.restore();
  12. });
  13. it("should return an object with anchor elements", () => {
  14. const cta = {
  15. url: "https://foo.com",
  16. metric: "foo",
  17. };
  18. const stub = sandbox.stub();
  19. const result = convertLinks({cta}, stub);
  20. assert.property(result, "cta");
  21. assert.propertyVal(result.cta, "type", "a");
  22. assert.propertyVal(result.cta.props, "href", cta.url);
  23. assert.propertyVal(result.cta.props, "data-metric", cta.metric);
  24. assert.propertyVal(result.cta.props, "onClick", stub);
  25. });
  26. it("should return an anchor element without href", () => {
  27. const cta = {
  28. url: "https://foo.com",
  29. metric: "foo",
  30. action: "OPEN_MENU",
  31. args: "appMenu",
  32. };
  33. const stub = sandbox.stub();
  34. const result = convertLinks({cta}, stub);
  35. assert.property(result, "cta");
  36. assert.propertyVal(result.cta, "type", "a");
  37. assert.propertyVal(result.cta.props, "href", false);
  38. assert.propertyVal(result.cta.props, "data-metric", cta.metric);
  39. assert.propertyVal(result.cta.props, "data-action", cta.action);
  40. assert.propertyVal(result.cta.props, "data-args", cta.args);
  41. assert.propertyVal(result.cta.props, "onClick", stub);
  42. });
  43. it("should follow openNewWindow prop", () => {
  44. const cta = {url: "https://foo.com"};
  45. const newWindow = convertLinks({cta}, sandbox.stub(), false, true);
  46. const sameWindow = convertLinks({cta}, sandbox.stub(), false);
  47. assert.propertyVal(newWindow.cta.props, "target", "_blank");
  48. assert.propertyVal(sameWindow.cta.props, "target", "");
  49. });
  50. it("should allow for custom elements & styles", () => {
  51. const wrapper = mount(<RichText customElements={{em: <em style={{color: "#f05"}} />}} text="<em>foo</em>" localization_id="text" />);
  52. const localized = wrapper.find(Localized);
  53. assert.propertyVal(localized.props().em.props.style, "color", "#f05");
  54. });
  55. });