From 5695e1d97c7f8dc0359fe766e66cc687b353842f Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Mon, 16 Oct 2023 08:51:55 +0200 Subject: [PATCH] test: BBCodeListItem --- .../__tests__/BBCodeListItem.test.ts | 26 +++++++++++++++++++ .../ckeditor5-bbcode/__tests__/DOMUtils.ts | 2 +- .../src/rules/BBCodeListItem.ts | 5 +++- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 packages/ckeditor5-bbcode/__tests__/BBCodeListItem.test.ts diff --git a/packages/ckeditor5-bbcode/__tests__/BBCodeListItem.test.ts b/packages/ckeditor5-bbcode/__tests__/BBCodeListItem.test.ts new file mode 100644 index 0000000000..755f7a63cb --- /dev/null +++ b/packages/ckeditor5-bbcode/__tests__/BBCodeListItem.test.ts @@ -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 + ${`
  • TEXT
  • `} | ${`[*] TEXT\n`} + `( + "$[$#] Should process '$dataView' to '$expected'", + ({ dataView, expected }: { dataView: string; expected: string | undefined }) => { + const embeddedInListDataView = ``; + const listElement = requireHTMLElement(embeddedInListDataView); + const element = asHTMLElement(listElement.firstElementChild); + if (!element) { + throw new Error(`Test setup error: Could not find the required
  • element: ${embeddedInListDataView}.`); + } + const content = element.textContent ?? ""; + const bbCode = rule.toData(element, content); + expect(bbCode).toEqual(expected); + }, + ); + }); +}); diff --git a/packages/ckeditor5-bbcode/__tests__/DOMUtils.ts b/packages/ckeditor5-bbcode/__tests__/DOMUtils.ts index 43cc29b25d..2eec7ffd19 100644 --- a/packages/ckeditor5-bbcode/__tests__/DOMUtils.ts +++ b/packages/ckeditor5-bbcode/__tests__/DOMUtils.ts @@ -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`. diff --git a/packages/ckeditor5-bbcode/src/rules/BBCodeListItem.ts b/packages/ckeditor5-bbcode/src/rules/BBCodeListItem.ts index 4a5d007cda..1c538116c7 100644 --- a/packages/ckeditor5-bbcode/src/rules/BBCodeListItem.ts +++ b/packages/ckeditor5-bbcode/src/rules/BBCodeListItem.ts @@ -1,7 +1,7 @@ import { BBCodeProcessingRule } from "./BBCodeProcessingRule"; /** - * Maps `
  • ` to `[*]`. + * Rule that maps `
  • ` to `[*]`. */ export class BBCodeListItem implements BBCodeProcessingRule { readonly id = "list-item"; @@ -13,4 +13,7 @@ export class BBCodeListItem implements BBCodeProcessingRule { } } +/** + * Rule instance that maps `
  • ` to `[*]`. + */ export const bbCodeListItem = new BBCodeListItem();