import React from "react";
import { render, RenderResult } from "@testing-library/react";
import "@testing-library/jest-dom";
import { utils } from "../../../common";
import {
StringPreviewContentBlobToggler,
StringPreviewContentBlobTogglerProps,
} from "../StringPreviewContentBlobToggler";
import { Default as StringPreviewContentBlobTogglerStory } from "./../stories/StringPreviewContentBlobToggler.stories";
describe("StringPreviewContentBlobToggler", () => {
const textMustExist = (queryByText: RenderResult["queryByText"], text: string) => {
expect(queryByText(text, { exact: false })).not.toBeNull();
};
const textMustNotExist = (queryByText: RenderResult["queryByText"], text: string) => {
expect(queryByText(text, { exact: false })).toBeNull();
};
it("should cut preview and show toggler to extend", () => {
const { queryByText } = render(
,
);
textMustExist(queryByText, "A library for");
textMustNotExist(queryByText, "documentation at");
textMustExist(queryByText, "show more");
});
it("should display full view if `startExtended` is enabled, and show toggler to reduce", () => {
const { queryByText } = render(
,
);
textMustExist(queryByText, "In order to create graphical user interfaces, please");
textMustExist(queryByText, "have look at the documentation at");
textMustExist(queryByText, "show less");
});
it('should display only first content line on `useOnly={"firstNonEmptyLine"}`', () => {
const { queryByText } = render(
,
);
textMustExist(queryByText, "A library for");
textMustNotExist(queryByText, "In order to create");
});
it('should use first Markdown paragraph as preview content on `useOnly={"firstMarkdownSection"}` but shorten it', () => {
const { queryByText } = render(
,
);
textMustExist(queryByText, "A library for");
textMustExist(queryByText, "In order to create");
textMustNotExist(queryByText, "documentation at");
});
it("should display full preview and no toggler if content is short enough", () => {
const { queryByText } = render(
,
);
textMustExist(queryByText, "A library for");
textMustExist(queryByText, "In order to create graphical user interfaces, please");
textMustExist(queryByText, "have look at the documentation at");
textMustNotExist(queryByText, "https://github.com/"); // test if Markdown was rendered
textMustNotExist(queryByText, "show more");
});
it("should not truncate the Markdown preview again when re-rendered with unchanged content", () => {
const truncateSpy = jest.spyOn(utils, "truncateMarkdownDisplay");
const props = StringPreviewContentBlobTogglerStory.args as StringPreviewContentBlobTogglerProps;
try {
const { rerender, queryByText } = render();
rerender();
expect(truncateSpy).toHaveBeenCalledTimes(1);
textMustExist(queryByText, "show more");
rerender();
expect(truncateSpy).toHaveBeenCalledTimes(2);
textMustNotExist(queryByText, "show more");
} finally {
truncateSpy.mockRestore();
}
});
it("should not use Markdown rendering on `renderPreviewAsMarkdown={false}`", () => {
const { queryByText } = render(
,
);
textMustExist(queryByText, "A library for [GUI elements]"); // raw Markdown link syntax visible
textMustExist(queryByText, "https://github.com/"); // test if Markdown was rendered
textMustExist(queryByText, "show more");
});
});