| 1 | import { describe, expect, it } from "bun:test"; |
| 2 | |
| 3 | import { |
| 4 | attachmentMarkdown, |
| 5 | completeMention, |
| 6 | type Edit, |
| 7 | type Format, |
| 8 | formatEdit, |
| 9 | mentionAt, |
| 10 | uploadPlaceholder, |
| 11 | } from "./markdown-editor"; |
| 12 | |
| 13 | /** The text after `edit`, with `|` marking the selection's ends. */ |
| 14 | function apply(value: |
| 15 | const text = |
| 16 | return `${text. |
| 17 | } |
| 18 | |
| 19 | /** Formats the part of `marked` between the two `|`. */ |
| 20 | function format(marked: |
| 21 | const start = |
| 22 | const end = |
| 23 | const value = |
| 24 | return apply(value |
| 25 | } |
| 26 | |
| 27 | describe("formatEdit", () |
| 28 | it("wraps the selection, keeping it selected", |
| 29 | expect(format("a |word| b |
| 30 | expect(format("a |word| b |
| 31 | expect(format("a |word| b |
| 32 | }); |
| 33 | |
| 34 | it("leaves the caret between the markers when nothing is selected", |
| 35 | expect(format("a || b |
| 36 | }); |
| 37 | |
| 38 | it("selects a link's URL, ready to be typed over", |
| 39 | expect(format("see |docs| |
| 40 | }); |
| 41 | |
| 42 | it("fences code that spans lines", |
| 43 | expect(format("|a |
| 44 | }); |
| 45 | |
| 46 | it("prefixes every line the selection touches", |
| 47 | expect(format("x |
| 48 | "x\n|- first\n- second|\ny" |
| 49 | ); |
| 50 | expect(format("|one |
| 51 | expect(format("|a| |
| 52 | expect(format("|a| |
| 53 | }); |
| 54 | |
| 55 | it("prefixes the caret's line and keeps typing at its end", |
| 56 | expect(format("one |
| 57 | }); |
| 58 | |
| 59 | it("leaves the next line alone when the selection ends at a line break", |
| 60 | expect(format("|a |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | describe("mentionAt", () |
| 65 | it("finds the mention or reference ending at the caret", |
| 66 | expect(mentionAt("hi @oc |
| 67 | trigger: "@", |
| 68 | query: "oc", |
| 69 | start: 3, |
| 70 | }); |
| 71 | expect(mentionAt("fixes #1 |
| 72 | trigger: "#", |
| 73 | query: "1", |
| 74 | start: 6, |
| 75 | }); |
| 76 | expect(mentionAt("@ |
| 77 | expect(mentionAt("(#4 |
| 78 | }); |
| 79 | |
| 80 | it("ignores triggers inside words and finished mentions", |
| 81 | expect(mentionAt("mail@example |
| 82 | expect(mentionAt("@octocat done |
| 83 | expect(mentionAt("# Heading |
| 84 | }); |
| 85 | }); |
| 86 | |
| 87 | describe("completeMention", () |
| 88 | it("replaces what was typed with the choice, spaced from what follows", |
| 89 | const value = |
| 90 | const mention = |
| 91 | expect(mention).not |
| 92 | expect(apply(value |
| 93 | "cc @octocat ||thanks", |
| 94 | ); |
| 95 | expect( |
| 96 | apply("@oc", |
| 97 | ).toBe("@octocat ||" |
| 98 | }); |
| 99 | }); |
| 100 | |
| 101 | describe("attachments", () |
| 102 | it("embeds images and links anything else, escaping brackets in names", |
| 103 | expect(attachmentMarkdown("shot.png |
| 104 | "", |
| 105 | ); |
| 106 | expect(attachmentMarkdown("[1].log |
| 107 | "[\\[1\\].log](https://x/b)", |
| 108 | ); |
| 109 | expect(uploadPlaceholder("a.zip |
| 110 | }); |
| 111 | }); |