|
| 1 | +import { render } from "@testing-library/react"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { Streamdown } from "../index"; |
| 4 | + |
| 5 | +// Mock the dependencies |
| 6 | +vi.mock("react-markdown", () => ({ |
| 7 | + default: ({ children, rehypePlugins, ...props }: any) => { |
| 8 | + if (!children) { |
| 9 | + return null; |
| 10 | + } |
| 11 | + // Store rehypePlugins in a data attribute for testing |
| 12 | + return ( |
| 13 | + <div |
| 14 | + data-testid="markdown" |
| 15 | + data-rehype-plugins={JSON.stringify( |
| 16 | + rehypePlugins?.map((plugin: any) => { |
| 17 | + if (Array.isArray(plugin)) { |
| 18 | + return { |
| 19 | + name: plugin[0]?.name || "unknown", |
| 20 | + options: plugin[1], |
| 21 | + }; |
| 22 | + } |
| 23 | + return { name: plugin?.name || "unknown" }; |
| 24 | + }) |
| 25 | + )} |
| 26 | + {...props} |
| 27 | + > |
| 28 | + {children} |
| 29 | + </div> |
| 30 | + ); |
| 31 | + }, |
| 32 | +})); |
| 33 | + |
| 34 | +vi.mock("rehype-katex", () => ({ |
| 35 | + default: () => {}, |
| 36 | +})); |
| 37 | + |
| 38 | +vi.mock("remark-gfm", () => ({ |
| 39 | + default: () => {}, |
| 40 | +})); |
| 41 | + |
| 42 | +vi.mock("remark-math", () => ({ |
| 43 | + default: () => {}, |
| 44 | +})); |
| 45 | + |
| 46 | +vi.mock("rehype-harden", () => ({ |
| 47 | + harden: vi.fn((options) => { |
| 48 | + const fn = () => {}; |
| 49 | + Object.assign(fn, { options }); |
| 50 | + return fn; |
| 51 | + }), |
| 52 | +})); |
| 53 | + |
| 54 | +describe("Streamdown hardenOptions", () => { |
| 55 | + it("should use default hardenOptions when not specified", () => { |
| 56 | + const { container } = render(<Streamdown>Test content</Streamdown>); |
| 57 | + const markdown = container.querySelector('[data-testid="markdown"]'); |
| 58 | + expect(markdown).toBeTruthy(); |
| 59 | + |
| 60 | + const rehypePlugins = JSON.parse( |
| 61 | + markdown?.getAttribute("data-rehype-plugins") || "[]" |
| 62 | + ); |
| 63 | + expect(rehypePlugins).toBeDefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should accept custom hardenOptions", () => { |
| 67 | + const customOptions = { |
| 68 | + allowedImagePrefixes: ["https://example.com"], |
| 69 | + allowedLinkPrefixes: ["https://example.com", "mailto:"], |
| 70 | + defaultOrigin: "https://example.com", |
| 71 | + }; |
| 72 | + |
| 73 | + const { container } = render( |
| 74 | + <Streamdown hardenOptions={customOptions}>Test content</Streamdown> |
| 75 | + ); |
| 76 | + const markdown = container.querySelector('[data-testid="markdown"]'); |
| 77 | + expect(markdown).toBeTruthy(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should accept partial hardenOptions", () => { |
| 81 | + const partialOptions = { |
| 82 | + allowedLinkPrefixes: ["https://trusted.com"], |
| 83 | + }; |
| 84 | + |
| 85 | + const { container } = render( |
| 86 | + <Streamdown hardenOptions={partialOptions}>Test content</Streamdown> |
| 87 | + ); |
| 88 | + const markdown = container.querySelector('[data-testid="markdown"]'); |
| 89 | + expect(markdown).toBeTruthy(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should accept empty array for allowedImagePrefixes to block all images", () => { |
| 93 | + const strictOptions = { |
| 94 | + allowedImagePrefixes: [], |
| 95 | + allowedLinkPrefixes: ["*"], |
| 96 | + }; |
| 97 | + |
| 98 | + const { container } = render( |
| 99 | + <Streamdown hardenOptions={strictOptions}> |
| 100 | +  |
| 101 | + </Streamdown> |
| 102 | + ); |
| 103 | + const markdown = container.querySelector('[data-testid="markdown"]'); |
| 104 | + expect(markdown).toBeTruthy(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should accept empty array for allowedLinkPrefixes to block all links", () => { |
| 108 | + const strictOptions = { |
| 109 | + allowedImagePrefixes: ["*"], |
| 110 | + allowedLinkPrefixes: [], |
| 111 | + }; |
| 112 | + |
| 113 | + const { container } = render( |
| 114 | + <Streamdown hardenOptions={strictOptions}> |
| 115 | + [Link](https://example.com) |
| 116 | + </Streamdown> |
| 117 | + ); |
| 118 | + const markdown = container.querySelector('[data-testid="markdown"]'); |
| 119 | + expect(markdown).toBeTruthy(); |
| 120 | + }); |
| 121 | + |
| 122 | + it("should accept wildcard for allowedImagePrefixes", () => { |
| 123 | + const wildcardOptions = { |
| 124 | + allowedImagePrefixes: ["*"], |
| 125 | + allowedLinkPrefixes: ["https://"], |
| 126 | + }; |
| 127 | + |
| 128 | + const { container } = render( |
| 129 | + <Streamdown hardenOptions={wildcardOptions}> |
| 130 | +  |
| 131 | + </Streamdown> |
| 132 | + ); |
| 133 | + const markdown = container.querySelector('[data-testid="markdown"]'); |
| 134 | + expect(markdown).toBeTruthy(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("should accept multiple prefixes for allowedImagePrefixes", () => { |
| 138 | + const multiPrefixOptions = { |
| 139 | + allowedImagePrefixes: [ |
| 140 | + "https://cdn.example.com", |
| 141 | + "https://images.example.com", |
| 142 | + "data:", |
| 143 | + ], |
| 144 | + allowedLinkPrefixes: ["*"], |
| 145 | + }; |
| 146 | + |
| 147 | + const { container } = render( |
| 148 | + <Streamdown hardenOptions={multiPrefixOptions}> |
| 149 | +  |
| 150 | + </Streamdown> |
| 151 | + ); |
| 152 | + const markdown = container.querySelector('[data-testid="markdown"]'); |
| 153 | + expect(markdown).toBeTruthy(); |
| 154 | + }); |
| 155 | + |
| 156 | + it("should accept multiple prefixes for allowedLinkPrefixes", () => { |
| 157 | + const multiPrefixOptions = { |
| 158 | + allowedImagePrefixes: ["*"], |
| 159 | + allowedLinkPrefixes: [ |
| 160 | + "https://example.com", |
| 161 | + "https://trusted.com", |
| 162 | + "mailto:", |
| 163 | + "tel:", |
| 164 | + ], |
| 165 | + }; |
| 166 | + |
| 167 | + const { container } = render( |
| 168 | + <Streamdown hardenOptions={multiPrefixOptions}> |
| 169 | + [Link](https://example.com) |
| 170 | + </Streamdown> |
| 171 | + ); |
| 172 | + const markdown = container.querySelector('[data-testid="markdown"]'); |
| 173 | + expect(markdown).toBeTruthy(); |
| 174 | + }); |
| 175 | + |
| 176 | + it("should accept defaultOrigin for relative URLs", () => { |
| 177 | + const originOptions = { |
| 178 | + allowedImagePrefixes: ["*"], |
| 179 | + allowedLinkPrefixes: ["*"], |
| 180 | + defaultOrigin: "https://example.com", |
| 181 | + }; |
| 182 | + |
| 183 | + const { container } = render( |
| 184 | + <Streamdown hardenOptions={originOptions}> |
| 185 | + [Relative Link](/path/to/page) |
| 186 | + </Streamdown> |
| 187 | + ); |
| 188 | + const markdown = container.querySelector('[data-testid="markdown"]'); |
| 189 | + expect(markdown).toBeTruthy(); |
| 190 | + }); |
| 191 | + |
| 192 | + it("should work with hardenOptions and other props together", () => { |
| 193 | + const customOptions = { |
| 194 | + allowedImagePrefixes: ["https://"], |
| 195 | + allowedLinkPrefixes: ["https://", "mailto:"], |
| 196 | + defaultOrigin: "https://example.com", |
| 197 | + }; |
| 198 | + |
| 199 | + const { container } = render( |
| 200 | + <Streamdown |
| 201 | + hardenOptions={customOptions} |
| 202 | + className="custom-class" |
| 203 | + parseIncompleteMarkdown={true} |
| 204 | + > |
| 205 | + # Test Content |
| 206 | + </Streamdown> |
| 207 | + ); |
| 208 | + |
| 209 | + const wrapper = container.firstElementChild; |
| 210 | + expect(wrapper).toBeTruthy(); |
| 211 | + expect(wrapper?.getAttribute("class")).toContain("custom-class"); |
| 212 | + |
| 213 | + const markdown = container.querySelector('[data-testid="markdown"]'); |
| 214 | + expect(markdown).toBeTruthy(); |
| 215 | + }); |
| 216 | + |
| 217 | + it("should handle hardenOptions with special protocol prefixes", () => { |
| 218 | + const specialProtocolOptions = { |
| 219 | + allowedImagePrefixes: ["data:", "blob:", "https://"], |
| 220 | + allowedLinkPrefixes: ["https://", "http://", "mailto:", "tel:", "#"], |
| 221 | + }; |
| 222 | + |
| 223 | + const { container } = render( |
| 224 | + <Streamdown hardenOptions={specialProtocolOptions}> |
| 225 | +  |
| 226 | + [Email](mailto:test@example.com) |
| 227 | + [Phone](tel:+1234567890) |
| 228 | + [Anchor](#section) |
| 229 | + </Streamdown> |
| 230 | + ); |
| 231 | + const markdown = container.querySelector('[data-testid="markdown"]'); |
| 232 | + expect(markdown).toBeTruthy(); |
| 233 | + }); |
| 234 | +}); |
0 commit comments