Skip to content
Source
Sign inSign up

composer-text-direction.test.ts

62 lines1.9 KB

1import { getSchemaByResolvedExtensions, resolveExtensions } from "@tiptap/core";
2import { Node as ProseMirrorNode } from "@tiptap/pm/model";
3import StarterKit from "@tiptap/starter-kit";
4import { describe, expect, it } from "vite-plus/test";
5​
6import {
7 buildDocJson,
8 ComposerBlockExtensions,
9 ComposerCodeBlockExtension,
10 ComposerCodeExtension,
11 ComposerListExtensions,
12} from "./composer-rich-text-doc";
13import { composerTextDirections } from "./composer-text-direction";
14​
15const schema = getSchemaByResolvedExtensions(
16 resolveExtensions([
17 StarterKit.configure({
18 blockquote: false,
19 bulletList: false,
20 codeBlock: false,
21 heading: false,
22 horizontalRule: false,
23 listItem: false,
24 orderedList: false,
25 code: false,
26 }),
27 ComposerCodeExtension,
28 ComposerCodeBlockExtension,
29 ...ComposerListExtensions,
30 ...ComposerBlockExtensions,
31 ]),
32);
33​
34function directions(value: string) {
35 const doc = ProseMirrorNode.fromJSON(
36 schema,
37 buildDocJson(value, (name) => ({ label: name, description: null })),
38 );
39 return composerTextDirections(doc).blocks;
41​
42describe("composerTextDirections", () => {
43 it("does not count inline code toward a line's direction", () => {
44 expect(directions("`useEffect` و `useLayoutEffect` في `React`")).toEqual(["rtl"]);
45 });
46​
47 it("gives a list one direction, from all of its items", () => {
48 expect(directions("- Vitest\n- اختبر الدالة الجديدة\n- وأضف تعليق")).toEqual(["rtl"]);
49 });
50​
51 it("continues the line above until a line has letters", () => {
52 expect(directions("اكتب الدالة\n\n42\nfix it")).toEqual(["rtl", "rtl", "rtl", "ltr"]);
53 });
54​
55 it("keeps a code block left to right inside Arabic text", () => {
56 expect(directions("اكتب الدالة\n```\n// احسب مجموع القيم هنا\n```\nثم اختبرها")).toEqual([
57 "rtl",
58 "ltr",
59 "rtl",
60 ]);
61 });
62});