-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c14cc36
commit 8349597
Showing
2 changed files
with
99 additions
and
25 deletions.
There are no files selected for viewing
88 changes: 79 additions & 9 deletions
88
...tpage/app/(app)/post/[postAuthor]/[postRkey]/_lib/editor-state-to-comment-content.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,87 @@ | ||
import { createHeadlessEditor } from "@lexical/headless"; | ||
import { $createParagraphNode, $createTextNode, $getRoot } from "lexical"; | ||
import { expect, test } from "vitest"; | ||
import { | ||
$createParagraphNode, | ||
$createTextNode, | ||
$getRoot, | ||
LexicalEditor, | ||
} from "lexical"; | ||
import { beforeEach, expect, test } from "vitest"; | ||
import { editorStateToCommentContent } from "./editor-state-to-comment-content"; | ||
|
||
test("simple string", () => { | ||
const editor = createHeadlessEditor(); | ||
editor.update(() => | ||
let editor: LexicalEditor; | ||
beforeEach(() => { | ||
editor = createHeadlessEditor({ | ||
namespace: "", | ||
onError: (error) => { | ||
throw error; | ||
}, | ||
}); | ||
}); | ||
|
||
async function update(updateFn: () => void) { | ||
editor.update(updateFn); | ||
await Promise.resolve(); | ||
} | ||
|
||
test("simple string", async () => { | ||
await update(() => { | ||
$getRoot().append( | ||
$createParagraphNode().append($createTextNode("Hello, world!")), | ||
), | ||
); | ||
); | ||
}); | ||
|
||
expect(editorStateToCommentContent(editor.getEditorState())).toEqual( | ||
expect(editorStateToCommentContent(editor.getEditorState())).toEqual([ | ||
"Hello, world!", | ||
); | ||
]); | ||
}); | ||
|
||
test("empty state", () => { | ||
expect(editorStateToCommentContent(editor.getEditorState())).toEqual([]); | ||
}); | ||
|
||
test("bold", async () => { | ||
await update(() => { | ||
$getRoot().append( | ||
$createParagraphNode().append( | ||
$createTextNode("Hello, "), | ||
$createTextNode("world").toggleFormat("bold"), | ||
$createTextNode("!"), | ||
), | ||
); | ||
}); | ||
|
||
expect(editorStateToCommentContent(editor.getEditorState())).toEqual([ | ||
"Hello, ", | ||
{ | ||
content: "world", | ||
facets: [ | ||
{ $type: "fyi.frontpage.richtext.facet#format", format: "bold" }, | ||
], | ||
}, | ||
"!", | ||
]); | ||
}); | ||
|
||
test("bold and italic", async () => { | ||
await update(() => { | ||
$getRoot().append( | ||
$createParagraphNode().append( | ||
$createTextNode("Hello, "), | ||
$createTextNode("world").toggleFormat("bold").toggleFormat("italic"), | ||
$createTextNode("!"), | ||
), | ||
); | ||
}); | ||
|
||
expect(editorStateToCommentContent(editor.getEditorState())).toEqual([ | ||
"Hello, ", | ||
{ | ||
content: "world", | ||
facets: [ | ||
{ $type: "fyi.frontpage.richtext.facet#format", format: "bold" }, | ||
{ $type: "fyi.frontpage.richtext.facet#format", format: "italic" }, | ||
], | ||
}, | ||
"!", | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters