|
| 1 | +// "helpers/*" is remapped by jest.config.js's moduleNameMapper to |
| 2 | +// __mocks__/helpers/*, which has no entry for linkedText - import the real |
| 3 | +// module directly to get the actual implementation under test. |
| 4 | +import { parseLinkedText } from "../../src/helpers/linkedText"; |
| 5 | + |
| 6 | +describe("parseLinkedText", () => { |
| 7 | + it("returns a single plain-text segment when there are no links", () => { |
| 8 | + expect(parseLinkedText("just plain text")).toEqual([ |
| 9 | + { text: "just plain text" }, |
| 10 | + ]); |
| 11 | + }); |
| 12 | + |
| 13 | + it("parses a markdown-style link", () => { |
| 14 | + expect( |
| 15 | + parseLinkedText( |
| 16 | + "See the [incident update](https://example.com/x) now.", |
| 17 | + ), |
| 18 | + ).toEqual([ |
| 19 | + { text: "See the " }, |
| 20 | + { text: "incident update", url: "https://example.com/x" }, |
| 21 | + { text: " now." }, |
| 22 | + ]); |
| 23 | + }); |
| 24 | + |
| 25 | + it("linkifies a bare URL", () => { |
| 26 | + expect(parseLinkedText("Visit https://example.com/x today")).toEqual([ |
| 27 | + { text: "Visit " }, |
| 28 | + { text: "https://example.com/x", url: "https://example.com/x" }, |
| 29 | + { text: " today" }, |
| 30 | + ]); |
| 31 | + }); |
| 32 | + |
| 33 | + it("strips trailing punctuation from a bare URL", () => { |
| 34 | + expect(parseLinkedText("See https://example.com/x.")).toEqual([ |
| 35 | + { text: "See " }, |
| 36 | + { text: "https://example.com/x", url: "https://example.com/x" }, |
| 37 | + { text: "." }, |
| 38 | + ]); |
| 39 | + }); |
| 40 | + |
| 41 | + it("handles multiple links in the same string", () => { |
| 42 | + expect( |
| 43 | + parseLinkedText( |
| 44 | + "First https://a.com then [second](https://b.com) link.", |
| 45 | + ), |
| 46 | + ).toEqual([ |
| 47 | + { text: "First " }, |
| 48 | + { text: "https://a.com", url: "https://a.com" }, |
| 49 | + { text: " then " }, |
| 50 | + { text: "second", url: "https://b.com" }, |
| 51 | + { text: " link." }, |
| 52 | + ]); |
| 53 | + }); |
| 54 | + |
| 55 | + it("does not linkify non-https schemes", () => { |
| 56 | + expect(parseLinkedText("Run javascript:alert(1) please")).toEqual([ |
| 57 | + { text: "Run javascript:alert(1) please" }, |
| 58 | + ]); |
| 59 | + }); |
| 60 | + |
| 61 | + it("does not linkify a bare http (non-https) URL", () => { |
| 62 | + expect(parseLinkedText("Visit http://example.com/x today")).toEqual([ |
| 63 | + { text: "Visit http://example.com/x today" }, |
| 64 | + ]); |
| 65 | + }); |
| 66 | + |
| 67 | + it("keeps balanced parentheses inside a bare URL", () => { |
| 68 | + expect( |
| 69 | + parseLinkedText( |
| 70 | + "See https://en.wikipedia.org/wiki/Function_(mathematics) for details.", |
| 71 | + ), |
| 72 | + ).toEqual([ |
| 73 | + { text: "See " }, |
| 74 | + { |
| 75 | + text: "https://en.wikipedia.org/wiki/Function_(mathematics)", |
| 76 | + url: "https://en.wikipedia.org/wiki/Function_(mathematics)", |
| 77 | + }, |
| 78 | + { text: " for details." }, |
| 79 | + ]); |
| 80 | + }); |
| 81 | + |
| 82 | + it("keeps balanced parentheses inside a markdown-style URL", () => { |
| 83 | + expect( |
| 84 | + parseLinkedText( |
| 85 | + "See [Function](https://en.wikipedia.org/wiki/Function_(mathematics)) for details.", |
| 86 | + ), |
| 87 | + ).toEqual([ |
| 88 | + { text: "See " }, |
| 89 | + { |
| 90 | + text: "Function", |
| 91 | + url: "https://en.wikipedia.org/wiki/Function_(mathematics)", |
| 92 | + }, |
| 93 | + { text: " for details." }, |
| 94 | + ]); |
| 95 | + }); |
| 96 | + |
| 97 | + it("does not let a stray unmatched bracket swallow the real link", () => { |
| 98 | + expect( |
| 99 | + parseLinkedText("Rates [APY vary. See the [docs](https://b.com)."), |
| 100 | + ).toEqual([ |
| 101 | + { text: "Rates [APY vary. See the " }, |
| 102 | + { text: "docs", url: "https://b.com" }, |
| 103 | + { text: "." }, |
| 104 | + ]); |
| 105 | + }); |
| 106 | + |
| 107 | + it("does not double-linkify a markdown label that is itself a URL", () => { |
| 108 | + expect( |
| 109 | + parseLinkedText("[https://label.example](https://target.example)"), |
| 110 | + ).toEqual([ |
| 111 | + { text: "https://label.example", url: "https://target.example" }, |
| 112 | + ]); |
| 113 | + }); |
| 114 | + |
| 115 | + it("does not absorb a quote that closes a quoted bare URL", () => { |
| 116 | + expect(parseLinkedText('See "https://example.com/x" now.')).toEqual([ |
| 117 | + { text: 'See "' }, |
| 118 | + { text: "https://example.com/x", url: "https://example.com/x" }, |
| 119 | + { text: '" now.' }, |
| 120 | + ]); |
| 121 | + }); |
| 122 | + |
| 123 | + it("does not linkify a malformed bare URL with no host", () => { |
| 124 | + expect(parseLinkedText("See https:// for syntax")).toEqual([ |
| 125 | + { text: "See https:// for syntax" }, |
| 126 | + ]); |
| 127 | + expect(parseLinkedText("See https://?query for syntax")).toEqual([ |
| 128 | + { text: "See https://?query for syntax" }, |
| 129 | + ]); |
| 130 | + }); |
| 131 | + |
| 132 | + it("does not linkify a malformed markdown URL with no host", () => { |
| 133 | + expect(parseLinkedText("[bad](https://)")).toEqual([ |
| 134 | + { text: "[bad](https://)" }, |
| 135 | + ]); |
| 136 | + }); |
| 137 | + |
| 138 | + it("keeps square brackets inside a markdown destination", () => { |
| 139 | + expect( |
| 140 | + parseLinkedText( |
| 141 | + "Search [tags](https://example.com/search?tag[]=security) here.", |
| 142 | + ), |
| 143 | + ).toEqual([ |
| 144 | + { text: "Search " }, |
| 145 | + { text: "tags", url: "https://example.com/search?tag[]=security" }, |
| 146 | + { text: " here." }, |
| 147 | + ]); |
| 148 | + }); |
| 149 | +}); |
0 commit comments