Skip to content

Commit

Permalink
test: BBCodeListItem
Browse files Browse the repository at this point in the history
  • Loading branch information
mmichaelis committed Oct 16, 2023
1 parent d5fbcbb commit 5695e1d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
26 changes: 26 additions & 0 deletions packages/ckeditor5-bbcode/__tests__/BBCodeListItem.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { asHTMLElement, requireHTMLElement } from "./DOMUtils";
import { bbCodeListItem } from "../src";

describe("BBCodeListItem", () => {
describe("Default Configuration", () => {
const rule = bbCodeListItem;

it.each`
dataView | expected
${`<li>TEXT</li>`} | ${`[*] TEXT\n`}
`(
"$[$#] Should process '$dataView' to '$expected'",
({ dataView, expected }: { dataView: string; expected: string | undefined }) => {
const embeddedInListDataView = `<ul>${dataView}</ul>`;
const listElement = requireHTMLElement(embeddedInListDataView);
const element = asHTMLElement(listElement.firstElementChild);
if (!element) {
throw new Error(`Test setup error: Could not find the required <li> element: ${embeddedInListDataView}.`);
}
const content = element.textContent ?? "";
const bbCode = rule.toData(element, content);
expect(bbCode).toEqual(expected);
},
);
});
});
2 changes: 1 addition & 1 deletion packages/ckeditor5-bbcode/__tests__/DOMUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isHTMLElement } from "@coremedia/ckeditor5-dom-support";
const parser = new DOMParser();
const parseHtml = (html: string): Document => parser.parseFromString(html, "text/html");
const parseFirstElement = (html: string): Element | undefined => parseHtml(html).body.firstElementChild ?? undefined;
const asHTMLElement = (value: unknown): HTMLElement | undefined => (isHTMLElement(value) ? value : undefined);
export const asHTMLElement = (value: unknown): HTMLElement | undefined => (isHTMLElement(value) ? value : undefined);

/**
* Parses the given HTML and returns contained `HTMLElement`.
Expand Down
5 changes: 4 additions & 1 deletion packages/ckeditor5-bbcode/src/rules/BBCodeListItem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BBCodeProcessingRule } from "./BBCodeProcessingRule";

/**
* Maps `<li>` to `[*]`.
* Rule that maps `<li>` to `[*]`.
*/
export class BBCodeListItem implements BBCodeProcessingRule {
readonly id = "list-item";
Expand All @@ -13,4 +13,7 @@ export class BBCodeListItem implements BBCodeProcessingRule {
}
}

/**
* Rule instance that maps `<li>` to `[*]`.
*/
export const bbCodeListItem = new BBCodeListItem();

0 comments on commit 5695e1d

Please sign in to comment.