Skip to content

A platform for community discussion. Free, open, simple.

3.1 KB
Raw Blame
import { module, test } from "qunit";
import {
  registerRichEditorExtension,
  resetRichEditorExtensions,
} from "discourse/lib/composer/rich-editor-extensions";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { testMarkdown } from "discourse/tests/helpers/rich-editor-helper";
import richEditorExtension from "discourse/plugins/checklist/lib/rich-editor-extension";

module(
  "Integration | Component | prosemirror-editor - checklist plugin extension",
  function (hooks) {
    setupRenderingTest(hooks);

    hooks.beforeEach(function () {
      resetRichEditorExtensions().then(() => {
        registerRichEditorExtension(richEditorExtension);
      });
    });

    const checked =
      '<span class="chcklst-box checked fa fa-square-check-o" contenteditable="false" draggable="true"></span>';
    const unchecked =
      '<span class="chcklst-box fa fa-square-o" contenteditable="false" draggable="true"></span>';

    Object.entries({
      "renders unchecked checkbox correctly": [
        "[ ] todo item",
        `<p>${unchecked} todo item</p>`,
        "[ ] todo item",
      ],
      "renders checked checkbox correctly": [
        "[x] completed item",
        `<p>${checked} completed item</p>`,
        "[x] completed item",
      ],
      "handles multiple checkboxes in a single paragraph": [
        "[] first task [x] second task",
        `<p>${unchecked} first task ${checked} second task</p>`,
        "[ ] first task [x] second task",
      ],
      "handles checkboxes in lists": [
        "* [ ] unchecked list item\n* [x] checked list item",
        `<ul data-tight="true"><li class="has-checkbox"><p>${unchecked} unchecked list item</p></li><li class="has-checkbox"><p>${checked} checked list item</p></li></ul>`,
        "* [ ] unchecked list item\n* [x] checked list item",
      ],
      "handles checkboxes with formatting": [
        "[ ] *italics* and [x] **bold**",
        `<p>${unchecked} <em>italics</em> and ${checked} <strong>bold</strong></p>`,
        "[ ] *italics* and [x] **bold**",
      ],
      "does not render escaped checkbox": [
        "\\[x] not a checkbox",
        "<p>[x] not a checkbox</p>",
        "\\[x\\] not a checkbox",
      ],
      "handles escaped checkbox followed by real checkbox": [
        "\\[x] escaped [x] real",
        `<p>[x] escaped ${checked} real</p>`,
        "\\[x\\] escaped [x] real",
      ],
      "preserves checkbox state in ordered lists": [
        "1. [ ] unchecked\n2. [x] checked",
        `<ol data-tight="true"><li><p>${unchecked} unchecked</p></li><li><p>${checked} checked</p></li></ol>`,
        "1. [ ] unchecked\n2. [x] checked",
      ],
      "handles nested list with checkboxes": [
        "* [ ] parent\n  * [x] child",
        `<ul data-tight="true"><li class="has-checkbox"><p>${unchecked} parent</p><ul data-tight="true"><li class="has-checkbox"><p>${checked} child</p></li></ul></li></ul>`,
        "* [ ] parent\n  * [x] child",
      ],
    }).forEach(([name, [markdown, html, expectedMarkdown]]) => {
      test(name, async function (assert) {
        await testMarkdown(assert, markdown, html, expectedMarkdown);
      });
    });
  }
);