github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/site/static/js/plugins/book_page_title.test.js (about)

     1  /**
     2   * @jest-environment jsdom
     3   */
     4  
     5  const plugins = require("./book_page_title");
     6  require("@gouch/to-title-case");
     7  
     8  test.each([
     9    ["1 The Chapter Title", "book/01-the-chapter-title/"],
    10    ["1.1 First Title", "book/01-anything/01-first-title"],
    11    [
    12      "1.2 A Much Longer Page Title",
    13      "book/01-anything/02-a-much-longer-page-title",
    14    ],
    15  ])("title is correct on book pages", (expectedTitle, path) => {
    16    delete window.location;
    17    window.location = new URL(path, "https://test.test");
    18    const transformedContent = plugins.processBookPageTitle(
    19      "Placeholder content"
    20    );
    21    expect(transformedContent.split("\n")[0]).toBe(`# ${expectedTitle}`);
    22  });
    23  
    24  test("title is not set on non-book pages", () => {
    25    delete window.location;
    26    window.location = new URL("non-book/url", "https://test.test");
    27    const originalContent = "Placeholder content";
    28    const transformedContent = plugins.processBookPageTitle(
    29      originalContent
    30    );
    31    expect(transformedContent).toBe(originalContent);
    32  });
    33  
    34  
    35  test("title is not set on pages without content", () => {
    36    delete window.location;
    37    window.location = new URL("book/01-book-chapter/01-book-page", "https://test.test");
    38    const originalContent = "";
    39    const transformedContent = plugins.processBookPageTitle(
    40      originalContent
    41    );
    42    expect(transformedContent).toBe(originalContent);
    43  });
    44  
    45  
    46  
    47  test("title is not set on pages that return default HTML", () => {
    48    delete window.location;
    49    window.location = new URL("book/01-book-chapter/01-book-page", "https://test.test");
    50    const originalContent = "<!DOCTYPE html><html></html>";
    51    const transformedContent = plugins.processBookPageTitle(
    52      originalContent
    53    );
    54    expect(transformedContent).toBe(originalContent);
    55  });