// import Chart from "../../lib/ReactViews/Custom/Chart"; import { render, screen } from "@testing-library/react"; import Collapsible from "../../lib/ReactViews/Custom/Collapsible/Collapsible"; import parseCustomHtmlToReact from "../../lib/ReactViews/Custom/parseCustomHtmlToReact"; import registerCustomComponentTypes from "../../lib/ReactViews/Custom/registerCustomComponentTypes"; describe("parseCustomHtmlToReact and registerCustomComponentTypes", function () { beforeEach(function () { registerCustomComponentTypes(); }); it("parses a div", function () { const result = parseCustomHtmlToReact("
Foo
"); expect(result.type).toEqual("div"); render(result); expect(screen.getByText("Foo")).toBeVisible(); }); it("parses a collapsible", function () { const result = parseCustomHtmlToReact( 'Bar', {}, false, { ADD_TAGS: ["collapsible"] } ); expect(result.props.title).toEqual("Untitled"); expect(result.type).toEqual(Collapsible); expect(result.props.children[0]).toEqual("Bar"); }); it("decodes HTML entities in text nodes and in attributes", function () { const result = parseCustomHtmlToReact( 'https://programs.communications.gov.au/geoserver/ows?service=WMS&version=1.3.0&request=GetCapabilities' ); render(result); const link = screen.getByRole("link", { name: "https://programs.communications.gov.au/geoserver/ows?service=WMS&version=1.3.0&request=GetCapabilities" }); expect(link).toBeVisible(); expect(link).toHaveAttribute( "href", "https://programs.communications.gov.au/geoserver/ows?service=WMS&version=1.3.0&request=GetCapabilities" ); }); }); describe("Parse html to react", () => { it("should open link in new tab", function () { const html = 'csiro'; const reactComponent = parseCustomHtmlToReact(html); render(reactComponent); expect(screen.getByRole("link", { name: "csiro" })).toBeVisible(); expect(screen.getByRole("link", { name: "csiro" })).toHaveAttribute( "target", "_blank" ); expect(screen.getByRole("link", { name: "csiro" })).toHaveAttribute( "rel", "noreferrer noopener" ); expect(screen.getByRole("link", { name: "csiro" })).toHaveAttribute( "href", "https://www.csiro.au/" ); expect( screen.getByRole("link", { name: "csiro" }).getElementsByClassName("icon") .length ).toBe(1); }); it("should correctly parse style attributes on a tag", function () { const html = 'csiro'; const reactComponent = parseCustomHtmlToReact(html); render(reactComponent); expect(screen.getByRole("link", { name: "csiro" })).toBeVisible(); expect(screen.getByRole("link", { name: "csiro" })).toHaveAttribute( "style", "color: yellow;" ); expect(screen.getByRole("link", { name: "csiro" })).toHaveAttribute( "target", "_blank" ); expect(screen.getByRole("link", { name: "csiro" })).toHaveAttribute( "rel", "noreferrer noopener" ); expect( screen.getByRole("link", { name: "csiro" }).getElementsByClassName("icon") .length ).toBe(1); }); it("should correctly parse empty style attributes on a tag", function () { const html = 'csiro'; const reactComponent = parseCustomHtmlToReact(html); render(reactComponent); expect(screen.getByRole("link", { name: "csiro" })).toBeVisible(); expect( screen.getByRole("link", { name: "csiro" }).getAttribute("style") ).toBeNull(); expect(screen.getByRole("link", { name: "csiro" })).toHaveAttribute( "target", "_blank" ); expect(screen.getByRole("link", { name: "csiro" })).toHaveAttribute( "rel", "noreferrer noopener" ); }); });